Skip to content

Commit

Permalink
Merge pull request #25 from FlockiiX/master
Browse files Browse the repository at this point in the history
Fix a typo in RequestUtility
  • Loading branch information
DxsSucuk authored Jun 9, 2022
2 parents d42759e + 38f6b9e commit 8487bfc
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/main/java/de/presti/ree6/utils/external/RequestUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class RequestUtility {

// HTTP Client used to send the Requests.
static HttpClient client = HttpClient.newHttpClient();
private static final HttpClient CLIENT = HttpClient.newHttpClient();

/**
* Send a Request.
Expand All @@ -29,40 +29,39 @@ public class RequestUtility {
*/
public static JsonElement request(Request request) {

HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder().uri(request.getUri())
HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder()
.uri(request.getUri())
.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 Ree6/" + BotWorker.getBuild())
.header("Content-Type", "application/json-rpc");

if (request.bearerAuth != null) {
httpRequestBuilder = httpRequestBuilder.header("Authorization", request.getBearerAuth());
}

if (request.getMethode() == Methode.GET) {
if (request.getMethod() == Method.GET) {
httpRequestBuilder = httpRequestBuilder.GET();
} else if (request.getMethode() == Methode.POST) {
} else if (request.getMethod() == Method.POST) {
httpRequestBuilder = httpRequestBuilder.POST(request.bodyPublisher);
} else if (request.getMethode() == Methode.PUT) {
} else if (request.getMethod() == Method.PUT) {
httpRequestBuilder = httpRequestBuilder.PUT(request.bodyPublisher);
}

HttpRequest httpRequest;

JsonElement jsonObject = new JsonObject();

if (httpRequestBuilder == null) {
jsonObject.getAsJsonObject().addProperty("success", false);
return jsonObject;
}

httpRequest = httpRequestBuilder.build();
HttpRequest httpRequest = httpRequestBuilder.build();

if (httpRequest == null) {
jsonObject.getAsJsonObject().addProperty("success", false);
return jsonObject;
}

try {
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> httpResponse = CLIENT.send(httpRequest, HttpResponse.BodyHandlers.ofString());

if (httpResponse.statusCode() == 200) {
jsonObject = new GsonBuilder().create().fromJson(httpResponse.body(), JsonElement.class);
Expand All @@ -82,8 +81,8 @@ public static class Request {
// The URL and Auth Token for the Request.
String url, bearerAuth;

// The Request Methode.
Methode methode;
// The Request Method.
Method method;

// The Body Publisher used for PUT and POST Requests.
HttpRequest.BodyPublisher bodyPublisher;
Expand All @@ -95,7 +94,7 @@ public static class Request {
*/
public Request(String url) {
this.url = url;
methode = Methode.GET;
method = Method.GET;
}

/**
Expand All @@ -107,32 +106,32 @@ public Request(String url) {
public Request(String url, String bearerAuth) {
this.url = url;
this.bearerAuth = bearerAuth;
methode = Methode.GET;
method = Method.GET;
}

/**
* Create a simple HTTP Requests with a AuthToken.
*
* @param url the Request URL.
* @param bearerAuth the AuthToken.
* @param methode the wanted Methode.
* @param method the wanted Method.
*/
public Request(String url, String bearerAuth, Methode methode) {
public Request(String url, String bearerAuth, Method method) {
this.url = url;
this.bearerAuth = bearerAuth;
this.methode = methode;
this.method = method;
}

/**
* Create a simple HTTP Requests.
*
* @param url the Request URL.
* @param methode the wanted Methode.
* @param method the wanted Method.
* @param bodyPublisher the Body Publisher used for PUT and POST Requests.
*/
public Request(String url, Methode methode, HttpRequest.BodyPublisher bodyPublisher) {
public Request(String url, Method method, HttpRequest.BodyPublisher bodyPublisher) {
this.url = url;
this.methode = methode;
this.method = method;
this.bodyPublisher = bodyPublisher;
}

Expand All @@ -142,13 +141,13 @@ public Request(String url, Methode methode, HttpRequest.BodyPublisher bodyPublis
*
* @param url the Request URL.
* @param bearerAuth the AuthToken.
* @param methode the wanted Methode.
* @param method the wanted Method.
* @param bodyPublisher the Body Publisher used for PUT and POST Requests.
*/
public Request(String url, String bearerAuth, Methode methode, HttpRequest.BodyPublisher bodyPublisher) {
public Request(String url, String bearerAuth, Method method, HttpRequest.BodyPublisher bodyPublisher) {
this.url = url;
this.bearerAuth = bearerAuth;
this.methode = methode;
this.method = method;
this.bodyPublisher = bodyPublisher;
}

Expand Down Expand Up @@ -180,12 +179,12 @@ public String getBearerAuth() {
}

/**
* Get the HTTP Methode.
* Get the HTTP Method.
*
* @return the Methode.
* @return the Method.
*/
public Methode getMethode() {
return methode;
public Method getMethod() {
return method;
}

/**
Expand All @@ -201,7 +200,7 @@ public HttpRequest.BodyPublisher getBodyPublisher() {
/**
* Supported Methods.
*/
public enum Methode {
public enum Method {
GET, PUT, POST
}

Expand Down

0 comments on commit 8487bfc

Please sign in to comment.