Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ru.tinkoff.kora.http.client.common.HttpClientException;
import ru.tinkoff.kora.http.client.common.HttpClientResponseException;
import ru.tinkoff.kora.http.client.common.annotation.ResponseCodeMapper;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestMapper;
import ru.tinkoff.kora.http.client.common.response.HttpClientResponse;
import ru.tinkoff.kora.http.client.common.response.HttpClientResponseMapper;
Expand Down Expand Up @@ -91,15 +91,15 @@ private MethodSpec buildMethod(MethodData methodData) {
var httpRoute = method.getAnnotation(HttpRoute.class);
b.addCode("""
var _client = this.$L;
var _requestBuilder = new $T($S, this.$LUrl)
var _requestBuilder = $T.of($S, this.$LUrl)
.requestTimeout(this.$L);
""", methodClientName, HttpClientRequestBuilder.class, httpRoute.method(), method.getSimpleName(), methodRequestTimeout);
""", methodClientName, HttpClientRequest.class, httpRoute.method(), method.getSimpleName(), methodRequestTimeout);
for (var parameter : methodData.parameters()) {
if (parameter instanceof Parameter.PathParameter path) {
if (requiresConverter(path.parameter().asType())) {
b.addCode("_requestBuilder.templateParam($S, $L.convert($L));\n", path.pathParameterName(), getConverterName(methodData, path.parameter()), path.parameter());
b.addCode("_requestBuilder.pathParam($S, $L.convert($L));\n", path.pathParameterName(), getConverterName(methodData, path.parameter()), path.parameter());
} else {
b.addCode("_requestBuilder.templateParam($S, $T.toString($L));\n", path.pathParameterName(), Objects.class, path.parameter());
b.addCode("_requestBuilder.pathParam($S, $T.toString($L));\n", path.pathParameterName(), Objects.class, path.parameter());
}
}
if (parameter instanceof Parameter.HeaderParameter header) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ru.tinkoff.kora.common.Tag;
import ru.tinkoff.kora.http.client.common.annotation.HttpClient;
import ru.tinkoff.kora.http.client.common.annotation.ResponseCodeMapper;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestMapper;
import ru.tinkoff.kora.http.client.common.response.HttpClientResponse;
import ru.tinkoff.kora.http.client.common.response.HttpClientResponseMapper;
Expand Down Expand Up @@ -54,7 +54,7 @@ record Contributor(String login, int contributions) {}
class IssueRequestMapper implements HttpClientRequestMapper<Issue> {

@Override
public HttpClientRequestBuilder apply(Request<Issue> request) {
public HttpClientRequest.Builder apply(Request<Issue> request) {
return request.builder().body("TEST".getBytes(StandardCharsets.UTF_8));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private Mono<HttpClientResponse> processRequest(Context context, HttpClientReque
for (var header : request.headers()) {
clientHeaders.add(header.getKey(), header.getValue());
}
var uri = Uri.create(request.resolvedUri());
var uri = Uri.create(request.uriResolved());
var requestBuilder = new RequestBuilder(request.method())
.setUri(uri)
.setHeaders(clientHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Set;

public class AsyncHttpClientHeaders implements HttpHeaders {

private final io.netty.handler.codec.http.HttpHeaders headers;

public AsyncHttpClientHeaders(io.netty.handler.codec.http.HttpHeaders headers) {
Expand Down Expand Up @@ -62,4 +63,9 @@ public Map.Entry<String, List<String>> next() {
}
};
}

@Override
public String toString() {
return headers.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.tinkoff.kora.http.client.common.form;

import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestMapper;
import ru.tinkoff.kora.http.common.form.FormMultipart;

public final class FormMultipartClientRequestMapper implements HttpClientRequestMapper<FormMultipart> {
@Override
public HttpClientRequestBuilder apply(Request<FormMultipart> request) {
public HttpClientRequest.Builder apply(Request<FormMultipart> request) {
return MultipartWriter.write(request.builder(), request.parameter().parts());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.tinkoff.kora.http.client.common.form;

import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestMapper;
import ru.tinkoff.kora.http.common.form.FormUrlEncoded;

public final class FormUrlEncodedClientRequestMapper implements HttpClientRequestMapper<FormUrlEncoded> {
@Override
public HttpClientRequestBuilder apply(Request<FormUrlEncoded> request) {
public HttpClientRequest.Builder apply(Request<FormUrlEncoded> request) {
var writer = new UrlEncodedWriter();
for (var part : request.parameter()) {
for (var value : part.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import reactor.core.Fuseable;
import reactor.core.publisher.Flux;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;
import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;
import ru.tinkoff.kora.http.common.form.FormMultipart;

import java.nio.ByteBuffer;
Expand All @@ -13,11 +13,11 @@
public class MultipartWriter {
private static final ByteBuffer RN_BUF = StandardCharsets.US_ASCII.encode("\r\n");

public static HttpClientRequestBuilder write(HttpClientRequestBuilder b, List<? extends FormMultipart.FormPart> parts) {
public static HttpClientRequest.Builder write(HttpClientRequest.Builder b, List<? extends FormMultipart.FormPart> parts) {
return write(b, "blob:" + UUID.randomUUID(), parts);
}

public static HttpClientRequestBuilder write(HttpClientRequestBuilder b, String boundary, List<? extends FormMultipart.FormPart> parts) {
public static HttpClientRequest.Builder write(HttpClientRequest.Builder b, String boundary, List<? extends FormMultipart.FormPart> parts) {
var boundaryBuff = StandardCharsets.US_ASCII.encode("--" + boundary);

var body = Flux.fromIterable(parts).concatMap(part -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.tinkoff.kora.http.client.common.form;

import ru.tinkoff.kora.http.client.common.request.HttpClientRequestBuilder;

import ru.tinkoff.kora.http.client.common.request.HttpClientRequest;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
Expand All @@ -18,7 +19,7 @@ public void add(String key, String value) {
this.baos.writeBytes(URLEncoder.encode(value, StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
}

public HttpClientRequestBuilder write(HttpClientRequestBuilder b) {
public HttpClientRequest.Builder write(HttpClientRequest.Builder b) {
var data = this.baos.toByteArray();
return b
.header("content-type", "application/x-www-form-urlencoded")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,127 @@
import ru.tinkoff.kora.http.common.HttpHeaders;
import ru.tinkoff.kora.http.common.HttpMethod;

import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public interface HttpClientRequest {

String method();

String uriTemplate();

List<QueryParam> queryParams();
String uriResolved();

Map<String, List<String>> queryParams();

List<TemplateParam> templateParams();
Map<String, String> pathParams();

HttpHeaders headers();

Flux<ByteBuffer> body();

String resolvedUri();

String authority();

String operation();

/**
* @return request timeout in millis
*/
int requestTimeout();

record TemplateParam(String name, String value) {}

record QueryParam(String name, @Nullable String value) {}

default HttpClientRequestBuilder toBuilder() {
default Builder toBuilder() {
return new HttpClientRequestBuilder(this);
}

static HttpClientRequestBuilder get(String path) {
static Builder get(String path) {
return new HttpClientRequestBuilder(HttpMethod.GET, path);
}

static HttpClientRequestBuilder head(String path) {
static Builder head(String path) {
return new HttpClientRequestBuilder(HttpMethod.HEAD, path);
}

static HttpClientRequestBuilder post(String path) {
static Builder post(String path) {
return new HttpClientRequestBuilder(HttpMethod.POST, path);
}

static HttpClientRequestBuilder put(String path) {
static Builder put(String path) {
return new HttpClientRequestBuilder(HttpMethod.PUT, path);
}

static HttpClientRequestBuilder delete(String path) {
static Builder delete(String path) {
return new HttpClientRequestBuilder(HttpMethod.DELETE, path);
}

static HttpClientRequestBuilder connect(String path) {
static Builder connect(String path) {
return new HttpClientRequestBuilder(HttpMethod.CONNECT, path);
}

static HttpClientRequestBuilder options(String path) {
static Builder options(String path) {
return new HttpClientRequestBuilder(HttpMethod.OPTIONS, path);
}

static HttpClientRequestBuilder trace(String path) {
static Builder trace(String path) {
return new HttpClientRequestBuilder(HttpMethod.TRACE, path);
}

static HttpClientRequestBuilder patch(String path) {
static Builder patch(String path) {
return new HttpClientRequestBuilder(HttpMethod.PATCH, path);
}

static HttpClientRequestBuilder of(String method, String path) {
static Builder of(String method, String path) {
return new HttpClientRequestBuilder(method, path);
}

interface Builder {

Builder uriTemplate(String uriTemplate);

Builder queryParam(String name);

Builder queryParam(String name, String value);

Builder queryParam(String name, Collection<String> value);

Builder queryParam(String name, Integer value);

Builder queryParam(String name, Long value);

Builder queryParam(String name, Boolean value);

Builder queryParam(String name, UUID value);

Builder pathParam(String name, @Nonnull String value);

record Default(
String method,
String uriTemplate,
List<QueryParam> queryParams,
List<TemplateParam> templateParams,
HttpHeaders headers,
Flux<ByteBuffer> body,
int requestTimeout,
String resolvedUri,
String authority,
String operation
) implements HttpClientRequest {}
Builder pathParam(String name, @Nonnull Collection<String> value);

Builder pathParam(String name, int value);

Builder pathParam(String name, long value);

Builder pathParam(String name, boolean value);

Builder pathParam(String name, @Nonnull UUID value);

Builder header(String name, String value);

Builder requestTimeout(int timeoutInMillis);

Builder requestTimeout(Duration requestTimeout);

Builder body(Flux<ByteBuffer> body);

Builder body(ByteBuffer body);

Builder body(byte[] body);

Builder headers(HttpHeaders headers);

HttpClientRequest build();
}
}
Loading