-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Http Method configuration. That will be used for sending data to target. #198
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2019 Aiven Oy and http-connector-for-apache-kafka project contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.aiven.kafka.connect.http.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public enum HttpMethodsType { | ||
POST("POST"), | ||
PUT("PUT"); | ||
|
||
public final String name; | ||
|
||
HttpMethodsType(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public static HttpMethodsType forName(final String name) { | ||
Objects.requireNonNull(name); | ||
return Arrays.stream(values()) | ||
.filter(v -> v.name.equalsIgnoreCase(name)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("HTTP Method type: " + name)); | ||
} | ||
|
||
public static final Collection<String> NAMES = | ||
Arrays.stream(values()).map(v -> v.name).collect(Collectors.toList()); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -48,12 +48,25 @@ protected AbstractHttpSender( | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public final HttpResponse<String> send(final String body) { | ||||||||||||||||||||||||||||
final var requestBuilder = | ||||||||||||||||||||||||||||
httpRequestBuilder.build(config).POST(HttpRequest.BodyPublishers.ofString(body)); | ||||||||||||||||||||||||||||
final var requestBuilder = prepareRequest(body); | ||||||||||||||||||||||||||||
return sendWithRetries(requestBuilder, HttpResponseHandler.ON_HTTP_ERROR_RESPONSE_HANDLER, | ||||||||||||||||||||||||||||
config.maxRetries()); | ||||||||||||||||||||||||||||
Comment on lines
+51
to
53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can improve the naming here
Suggested change
|
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// seth http bethod based on config | ||||||||||||||||||||||||||||
private Builder prepareRequest(final String body) { | ||||||||||||||||||||||||||||
switch (config.httpMethod()) { | ||||||||||||||||||||||||||||
case POST: | ||||||||||||||||||||||||||||
return httpRequestBuilder | ||||||||||||||||||||||||||||
.build(config).POST(HttpRequest.BodyPublishers.ofString(body)); | ||||||||||||||||||||||||||||
case PUT: | ||||||||||||||||||||||||||||
return httpRequestBuilder | ||||||||||||||||||||||||||||
.build(config).PUT(HttpRequest.BodyPublishers.ofString(body)); | ||||||||||||||||||||||||||||
Comment on lines
+58
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's move the repeated code up
Suggested change
|
||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||
throw new ConnectException("Unsupported HTTP method: " + config.httpMethod()); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||
* Sends an HTTP body using {@code httpSender}, respecting the configured retry policy. | ||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -24,10 +24,11 @@ | |||
import java.util.List; | ||||
import java.util.Map; | ||||
|
||||
import org.apache.kafka.connect.errors.ConnectException; | ||||
|
||||
import io.aiven.kafka.connect.http.config.HttpMethodsType; | ||||
import io.aiven.kafka.connect.http.config.HttpSinkConfig; | ||||
|
||||
import org.apache.kafka.connect.errors.ConnectException; | ||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories; | ||||
import org.junit.jupiter.api.Test; | ||||
import org.junit.jupiter.api.extension.ExtendWith; | ||||
|
@@ -88,7 +89,7 @@ void shouldBuildDefaultHttpRequest() throws Exception { | |||
.isPresent() | ||||
.get(as(InstanceOfAssertFactories.DURATION)) | ||||
.hasSeconds(config.httpTimeout()); | ||||
assertThat(httpRequest.method()).isEqualTo("POST"); | ||||
assertThat(httpRequest.method()).isEqualTo(HttpMethodsType.POST.name()); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can stick to the string values here, so if enum changes, then tests will fail. Same for new put tests |
||||
|
||||
assertThat(httpRequest | ||||
.headers() | ||||
|
@@ -101,6 +102,54 @@ void shouldBuildDefaultHttpRequest() throws Exception { | |||
|
||||
} | ||||
|
||||
@Test | ||||
void shouldBuildDefaultHttpPutRequest() throws Exception { | ||||
final var configBase = new HashMap<>(defaultConfig()); | ||||
configBase.put("http.method", "PUT"); | ||||
|
||||
// Build the configuration | ||||
final HttpSinkConfig config = new HttpSinkConfig(configBase); | ||||
|
||||
// Mock the Http Client and Http Response | ||||
when(mockedClient.send(any(HttpRequest.class), any(BodyHandler.class))).thenReturn(mockedResponse); | ||||
|
||||
// Create a spy on the HttpSender implementation to capture methods parameters | ||||
final var httpSender = Mockito.spy(new DefaultHttpSender(config, mockedClient)); | ||||
|
||||
// Trigger the client | ||||
final List<String> messages = List.of("some message"); | ||||
messages.forEach(httpSender::send); | ||||
|
||||
// Capture the RequestBuilder | ||||
final ArgumentCaptor<Builder> defaultHttpRequestBuilder = ArgumentCaptor.forClass(HttpRequest.Builder.class); | ||||
verify(httpSender, atLeast(messages.size())).sendWithRetries(defaultHttpRequestBuilder.capture(), | ||||
any(HttpResponseHandler.class), anyInt()); | ||||
|
||||
// Retrieve the builders and rebuild the HttpRequests to check the HttpRequest proper configuration | ||||
defaultHttpRequestBuilder | ||||
.getAllValues() | ||||
.stream() | ||||
.map(Builder::build) | ||||
.forEach(httpRequest -> { | ||||
assertThat(httpRequest.uri()).isEqualTo(config.httpUri()); | ||||
assertThat(httpRequest.timeout()) | ||||
.isPresent() | ||||
.get(as(InstanceOfAssertFactories.DURATION)) | ||||
.hasSeconds(config.httpTimeout()); | ||||
assertThat(httpRequest.method()).isEqualTo(HttpMethodsType.PUT.name()); | ||||
|
||||
assertThat(httpRequest | ||||
.headers() | ||||
.firstValue(HttpRequestBuilder.HEADER_CONTENT_TYPE)).isEmpty(); | ||||
}); | ||||
|
||||
// Check the messages have been sent once | ||||
messages.forEach( | ||||
message -> bodyPublishers.verify(() -> HttpRequest.BodyPublishers.ofString(eq(message)), times(1))); | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||
} | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
|
||||
@Test | ||||
void shouldBuildCustomHttpRequest() throws Exception { | ||||
final var configBase = new HashMap<>(defaultConfig()); | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can stick to just use the Enum name, and remove the attribute. See
OAuth2AuthorizationMode
which already support a configuration.