Skip to content

Commit

Permalink
Google Java Format
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 19, 2024
1 parent 695b7bd commit f85ca6d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 51 deletions.
2 changes: 0 additions & 2 deletions src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public AzureApiBuilder sslParameters(SSLParameters sslParameters) {
return this;
}


/**
* Builds and returns an instance of AzureApi with the configured parameters.
*
Expand Down Expand Up @@ -163,7 +162,6 @@ public AzureApi build() {
httpClient.sslParameters(sslParameters);
}


if (connectTimeout != null) {
httpClient.connectTimeout(connectTimeout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,7 +61,7 @@ public class AzureApiImpl<T> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Collection<String>> queryParams) throws URISyntaxException {
public URI getHttpUrl(BaseURL baseURL, Map<String, Collection<String>> 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<String, Collection<String>> 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("&"));
}
}
35 changes: 20 additions & 15 deletions src/main/java/io/github/brenoepics/at4j/util/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public Map<String, String> getHeaders() {
return headers;
}


/**
* Sets the body of the request.
*
Expand Down Expand Up @@ -220,20 +219,19 @@ public RestRequestInformation asRestRequestInformation() {
} catch (URISyntaxException | MalformedURLException e) {
throw new AssertionError(e);
}
}
}

/**
* Executes the request blocking.
*
* @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<T> executeBlocking() throws AzureException, IOException, URISyntaxException {
public RestRequestResult<T> 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) {
Expand All @@ -250,21 +248,25 @@ public RestRequestResult<T> executeBlocking() throws AzureException, IOException
requestBuilder,
body != null ? " with body " + body : "");

CompletableFuture<HttpResponse<String>> response = getApi().getHttpClient().sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
CompletableFuture<HttpResponse<String>> response =
getApi()
.getHttpClient()
.sendAsync(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
RestRequestResult<T> responseResult = handleResponse(fullUrl, response.join());
result.complete(responseResult);
return responseResult;
}
}

private RestRequestResult<T> handleResponse(URI fullUrl, HttpResponse<String> response) throws IOException, AzureException {
private RestRequestResult<T> handleResponse(URI fullUrl, HttpResponse<String> response)
throws IOException, AzureException {
RestRequestResult<T> 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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class RestRequestResult<T> {
* @param response The response of the RestRequest.
* @throws IOException Passed on from {@link HttpResponse#body()}.
*/
public RestRequestResult(RestRequest<T> request, HttpResponse<String> response) throws IOException {
public RestRequestResult(RestRequest<T> request, HttpResponse<String> response)
throws IOException {
this.request = request;
this.response = response;
this.stringBody = response.body();
Expand All @@ -37,16 +38,16 @@ public RestRequestResult(RestRequest<T> request, HttpResponse<String> 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;
}

/**
Expand All @@ -67,7 +68,6 @@ public HttpResponse<String> getResponse() {
return response;
}


/**
* Gets the string body of the response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down

0 comments on commit f85ca6d

Please sign in to comment.