diff --git a/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java b/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java index a64d6bb..74d23d6 100644 --- a/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java +++ b/src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java @@ -135,7 +135,6 @@ public AzureApiBuilder sslParameters(SSLParameters sslParameters) { return this; } - /** * Builds and returns an instance of AzureApi with the configured parameters. * @@ -163,7 +162,6 @@ public AzureApi build() { httpClient.sslParameters(sslParameters); } - if (connectTimeout != null) { httpClient.connectTimeout(connectTimeout); } diff --git a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java index 1ffba78..72233e6 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java +++ b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java @@ -25,7 +25,6 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; - /** * This class is an implementation of the AzureApi interface. It provides methods to interact with * Azure's translation API. @@ -62,7 +61,7 @@ public class AzureApiImpl implements AzureApi { * @param subscriptionRegion The subscription region for this instance. */ public AzureApiImpl( - HttpClient httpClient, BaseURL baseURL, String subscriptionKey, String subscriptionRegion) { + HttpClient httpClient, BaseURL baseURL, String subscriptionKey, String subscriptionRegion) { this.httpClient = httpClient; this.baseURL = baseURL; this.subscriptionKey = subscriptionKey; diff --git a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java index 635bff7..27aea50 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java +++ b/src/main/java/io/github/brenoepics/at4j/core/ratelimit/RateLimitBucket.java @@ -88,7 +88,7 @@ public int getTimeTillSpaceGetsAvailable() { * @return Whether a bucket created with the given parameters would equal this bucket or not. */ public boolean endpointMatches(RestEndpoint endpoint) { - return this.endpoint == endpoint; + return this.endpoint == endpoint; } @Override @@ -110,6 +110,6 @@ public int hashCode() { @Override public String toString() { - return "Endpoint: " + (endpoint == null ? "global" : endpoint.getEndpointUrl()); + return "Endpoint: " + (endpoint == null ? "global" : endpoint.getEndpointUrl()); } } diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestEndpoint.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestEndpoint.java index e99e56f..ceb9247 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestEndpoint.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestEndpoint.java @@ -73,31 +73,27 @@ public String getEndpointUrl() { return endpointUrl; } - /** - * Gets the full {@link URI http url} of the endpoint. - * Parameters which are "too much" are - * added to the end. + * Gets the full {@link URI http url} of the endpoint. Parameters which are "too much" are added + * to the end. * * @param baseURL The base url of the endpoint. * @return The full http url of the endpoint. */ - public URI getHttpUrl(BaseURL baseURL, Map> queryParams) throws URISyntaxException { + public URI getHttpUrl(BaseURL baseURL, Map> queryParams) + throws URISyntaxException { String query = getQuery(queryParams); - return new URI( - "https", - baseURL.getUrl(), - endpointUrl, - query, - null); + return new URI("https", baseURL.getUrl(), endpointUrl, query, null); } private String getQuery(Map> queryParams) { return queryParams.entrySet().stream() - .map(entry -> entry.getValue().stream() + .map( + entry -> + entry.getValue().stream() .map(value -> entry.getKey() + "=" + value) .collect(Collectors.joining("&"))) - .collect(Collectors.joining("&")); + .collect(Collectors.joining("&")); } } diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java index 67dc95d..7e2088a 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java @@ -136,7 +136,6 @@ public Map getHeaders() { return headers; } - /** * Sets the body of the request. * @@ -220,7 +219,7 @@ public RestRequestInformation asRestRequestInformation() { } catch (URISyntaxException | MalformedURLException e) { throw new AssertionError(e); } - } + } /** * Executes the request blocking. @@ -228,12 +227,11 @@ public RestRequestInformation asRestRequestInformation() { * @return The result of the request. * @throws AzureException Thrown in case of an error while requesting azure. * @throws IOException Thrown if an error occurs while reading the response. - * */ - public RestRequestResult executeBlocking() throws AzureException, IOException, URISyntaxException { + public RestRequestResult executeBlocking() + throws AzureException, IOException, URISyntaxException { URI fullUrl = endpoint.getHttpUrl(api.getBaseURL(), queryParameters); - HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() - .uri(fullUrl); + HttpRequest.Builder requestBuilder = HttpRequest.newBuilder().uri(fullUrl); request(requestBuilder); if (includeAuthorizationHeader) { @@ -250,21 +248,25 @@ public RestRequestResult executeBlocking() throws AzureException, IOException requestBuilder, body != null ? " with body " + body : ""); - CompletableFuture> response = getApi().getHttpClient().sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString()); + CompletableFuture> response = + getApi() + .getHttpClient() + .sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString()); RestRequestResult responseResult = handleResponse(fullUrl, response.join()); result.complete(responseResult); return responseResult; - } + } - private RestRequestResult handleResponse(URI fullUrl, HttpResponse response) throws IOException, AzureException { + private RestRequestResult handleResponse(URI fullUrl, HttpResponse response) + throws IOException, AzureException { RestRequestResult requestResult = new RestRequestResult<>(this, response); String bodyString = requestResult.getStringBody().orElse("empty"); logger.debug( - "Sent {} request to {} and received status code {} with body {}", - method.name(), - fullUrl.toURL(), - response.statusCode(), - bodyString); + "Sent {} request to {} and received status code {} with body {}", + method.name(), + fullUrl.toURL(), + response.statusCode(), + bodyString); if (response.statusCode() >= 300 || response.statusCode() < 200) { return handleError(response.statusCode(), requestResult); @@ -348,7 +350,10 @@ private void request(HttpRequest.Builder requestBuilder) { requestBuilder.setHeader("User-Agent", AT4J.USER_AGENT); requestBuilder.setHeader("Accept", "application/json"); requestBuilder.setHeader("Content-Type", "application/json"); - HttpRequest.BodyPublisher bodyPublisher = body == null ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(body); + HttpRequest.BodyPublisher bodyPublisher = + body == null + ? HttpRequest.BodyPublishers.noBody() + : HttpRequest.BodyPublishers.ofString(body); requestBuilder.method(method.name(), bodyPublisher); } } diff --git a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java index 3a8870c..51f1b0d 100644 --- a/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java +++ b/src/main/java/io/github/brenoepics/at4j/util/rest/RestRequestResult.java @@ -28,7 +28,8 @@ public class RestRequestResult { * @param response The response of the RestRequest. * @throws IOException Passed on from {@link HttpResponse#body()}. */ - public RestRequestResult(RestRequest request, HttpResponse response) throws IOException { + public RestRequestResult(RestRequest request, HttpResponse response) + throws IOException { this.request = request; this.response = response; this.stringBody = response.body(); @@ -37,16 +38,16 @@ public RestRequestResult(RestRequest request, HttpResponse response) return; } - ObjectMapper mapper = request.getApi().getObjectMapper(); - JsonNode jsonNode; - try { - jsonNode = mapper.readTree(stringBody); - } catch (JsonParseException e) { - // This can happen if Azure sends garbage - logger.debug("Failed to parse json response", e); - jsonNode = null; - } - this.jsonBody = jsonNode == null ? NullNode.getInstance() : jsonNode; + ObjectMapper mapper = request.getApi().getObjectMapper(); + JsonNode jsonNode; + try { + jsonNode = mapper.readTree(stringBody); + } catch (JsonParseException e) { + // This can happen if Azure sends garbage + logger.debug("Failed to parse json response", e); + jsonNode = null; + } + this.jsonBody = jsonNode == null ? NullNode.getInstance() : jsonNode; } /** @@ -67,7 +68,6 @@ public HttpResponse getResponse() { return response; } - /** * Gets the string body of the response. * diff --git a/src/test/java/io/github/brenoepics/at4j/AzureApiBuilderTest.java b/src/test/java/io/github/brenoepics/at4j/AzureApiBuilderTest.java index c2b092e..ae91601 100644 --- a/src/test/java/io/github/brenoepics/at4j/AzureApiBuilderTest.java +++ b/src/test/java/io/github/brenoepics/at4j/AzureApiBuilderTest.java @@ -3,11 +3,6 @@ import org.junit.jupiter.api.Test; import io.github.brenoepics.at4j.azure.BaseURL; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLParameters; -import java.net.InetAddress; -import java.net.ProxySelector; -import java.time.Duration; import static org.junit.jupiter.api.Assertions.*;