Skip to content

Commit

Permalink
New update v3.0
Browse files Browse the repository at this point in the history
1. Code style
  • Loading branch information
megoRU committed Oct 9, 2023
1 parent df004a4 commit 01de146
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 62 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
<version>5.0.0-alpha.11</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
<version>5.3-alpha1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
Expand Down
115 changes: 55 additions & 60 deletions src/main/java/org/megoru/impl/WgEasyAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.megoru.entity.ErrorResponse;
import org.megoru.entity.ErrorResponseToMany;
import org.megoru.entity.api.*;
import org.megoru.entity.api.Client;
import org.megoru.entity.api.Create;
import org.megoru.entity.api.Session;
import org.megoru.entity.api.Status;
import org.megoru.io.DefaultResponseTransformer;
import org.megoru.io.ResponseTransformer;
import org.megoru.io.UnsuccessfulHttpException;
Expand Down Expand Up @@ -426,79 +428,72 @@ private File execute(ClassicHttpRequest request, String fileName, FileExtension
}

@Nullable
private <E> E execute(ClassicHttpRequest request, ResponseTransformer<E> responseTransformer) throws
UnsuccessfulHttpException {
CloseableHttpClient httpClient = HttpClients
.custom()
.setConnectionReuseStrategy(((requests, response, context) -> false))
.setDefaultCookieStore(cookieStore)
.useSystemProperties()
.build();
try {
private <E> E execute(ClassicHttpRequest request, ResponseTransformer<E> responseTransformer) throws UnsuccessfulHttpException {
try (CloseableHttpClient httpClient = createHttpClient()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getCode();
HttpEntity entity = response.getEntity();
String body = entity != null ? EntityUtils.toString(entity) : null;
if (body == null) body = "{}";
String body = getBodyFromEntity(response.getEntity());

logResponse(response, body);

switch (statusCode) {
case 200:
case 204: {
if (body.equals("{}")) {
body = "{\n" + " \"status\": \"" + response.getCode() + "\"\n" + "}";
}
return responseTransformer.transform(body);
}
case 401:
case 403: {
ErrorResponse result = gson.fromJson(body, ErrorResponse.class);
throw new UnsuccessfulHttpException(response.getCode(), result.getError());
}
case 404: {
ErrorNotFound errorNotFound = gson.fromJson(body, ErrorNotFound.class);
throw new UnsuccessfulHttpException(response.getCode(), errorNotFound.getError());
}
case 429: {
ErrorResponseToMany result = gson.fromJson(body, ErrorResponseToMany.class);
throw new UnsuccessfulHttpException(result.getStatusCode(), result.getMessage());
}
case 502: {
body = "{\n" +
" \"error\": {\n" +
" \"code\": 502,\n" +
" \"message\": \"Bad Gateway\"\n" +
" }\n" +
"}";
ErrorResponse result = gson.fromJson(body, ErrorResponse.class);
throw new UnsuccessfulHttpException(response.getCode(), result.getError());
}
default:
ErrorResponse result = gson.fromJson(body, ErrorResponse.class);
throw new UnsuccessfulHttpException(response.getCode(), result.getError());
}
} catch (ParseException e) {
throw new RuntimeException(e);
return handleResponse(statusCode, body, responseTransformer);
} catch (ParseException | IOException e) {
throw new RuntimeException("Failed to execute HTTP request", e);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
throw new RuntimeException("Failed to close the HTTP client", e);
}
throw new RuntimeException();
}

private CloseableHttpClient createHttpClient() {
return HttpClients.custom()
.setConnectionReuseStrategy(((requests, response, context) -> false))
.setDefaultCookieStore(cookieStore)
.useSystemProperties()
.build();
}

private String getBodyFromEntity(HttpEntity entity) throws IOException, ParseException {
String body = entity != null ? EntityUtils.toString(entity) : "{}";
return body != null ? body : "{}";
}

private <E> E handleResponse(int statusCode, String body, ResponseTransformer<E> transformer) throws UnsuccessfulHttpException {
switch (statusCode) {
case 200:
case 204:
return transformOrUseDefaultBody(body, transformer, statusCode);
case 401:
case 403:
case 404:
case 429:
ErrorResponse errorResponse = gson.fromJson(body, ErrorResponse.class);
throw new UnsuccessfulHttpException(statusCode, errorResponse.getError());
case 502:
body = createDefault502Body();
// Fall through to default case since the handling is the same.
default:
ErrorResponse defaultErrorResponse = gson.fromJson(body, ErrorResponse.class);
throw new UnsuccessfulHttpException(statusCode, defaultErrorResponse.getError());
}
}

private <E> E transformOrUseDefaultBody(String body, ResponseTransformer<E> transformer, int statusCode) {
if (body.equals("{}")) {
body = "{ \"status\": \"" + statusCode + "\" }";
}
return transformer.transform(body);
}

private String createDefault502Body() {
return "{ \"error\": { \"code\": 502, \"message\": \"Bad Gateway\" } }";
}

private void logResponse(ClassicHttpResponse response, String body) {
if (!devMode) {
return;
}


System.out.println("Response: " + response.getVersion());
System.out.println(Arrays.toString(response.getHeaders()));

Expand Down

0 comments on commit 01de146

Please sign in to comment.