Skip to content

Commit

Permalink
Merge pull request #100 from xendit/fix/patchMethodLib
Browse files Browse the repository at this point in the history
Java Library - Invoke HTTP Patch Lib for PATCH API
  • Loading branch information
xen-HendryZheng authored Oct 7, 2021
2 parents 9bd69bd + fd0d97b commit 9ce5f80
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
6 changes: 5 additions & 1 deletion xendit-java-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.xendit'
version '1.16.0'
version '1.17.0'

sourceCompatibility = 1.8

Expand All @@ -19,7 +19,11 @@ repositories {
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.projectlombok:lombok:1.18.8'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore
implementation 'org.apache.httpcomponents:httpcore:4.4.11'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'


annotationProcessor 'org.projectlombok:lombok:1.18.8'

compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
Expand Down
62 changes: 58 additions & 4 deletions xendit-java-lib/src/main/java/com/xendit/network/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class BaseRequest implements NetworkClient {
private static final int DEFAULT_CONNECT_TIMEOUT = 60000;
Expand Down Expand Up @@ -109,14 +118,58 @@ private static XenditResponse rawRequest(
jsonParams = gson.toJson(params);
}

HttpURLConnection connection = null;
if (method == RequestResource.Method.PATCH) {
// new Implementation using org.apache.http to support PATCH Method
return httpPatchXenditConnection(url, apiKey, headers, jsonParams);
} else {
// default Implementation of Xendit Connection
return defaultHttpXenditConnection(method, url, apiKey, headers, jsonParams);
}
}

private static XenditResponse httpPatchXenditConnection(
String url, String apiKey, Map<String, String> headers, String jsonParams)
throws XenditException {

try {
connection = createXenditConnection(url, apiKey, headers);
CloseableHttpClient httpClient =
HttpClients.custom()
.setDefaultRequestConfig(
RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build();
HttpPatch request = new HttpPatch(url);
HttpEntity httpEntity = new ByteArrayEntity(jsonParams.getBytes("UTF-8"));
if (headers.get("Content-Type") == null) {
request.setHeader("Content-Type", "application/json");
}
for (Map.Entry<String, String> header : getHeaders(apiKey, headers).entrySet()) {
request.setHeader(header.getKey(), header.getValue());
}

connection.setRequestMethod(method.getText());
request.setEntity(httpEntity);
CloseableHttpResponse response;
response = httpClient.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());

if (method == RequestResource.Method.POST || method == RequestResource.Method.PATCH) {
return new XenditResponse(responseCode, responseBody);
} catch (IOException e) {
throw new XenditException("Connection error");
}
}

private static XenditResponse defaultHttpXenditConnection(
RequestResource.Method method,
String url,
String apiKey,
Map<String, String> headers,
String jsonParams)
throws XenditException {
HttpURLConnection connection = null;
try {
connection = createXenditConnection(url, apiKey, headers);
connection.setRequestMethod(method.getText());
if (method == RequestResource.Method.POST) {
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
Expand All @@ -136,6 +189,7 @@ private static XenditResponse rawRequest(

return new XenditResponse(responseCode, responseBody);
} catch (IOException e) {

throw new XenditException("Connection error");
} finally {
if (connection != null) {
Expand Down

0 comments on commit 9ce5f80

Please sign in to comment.