Skip to content

Commit

Permalink
Merge pull request #132 from xendit/feat/add-request-id-implementation
Browse files Browse the repository at this point in the history
Add Request Id Implementation
  • Loading branch information
xen-HendryZheng authored Mar 17, 2023
2 parents e5fbd2b + 13fff62 commit e79c5c1
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ bin/
##############################
.DS_Store

*.gpg
*.gpg
.settings/org.eclipse.buildship.core.prefs
.project
*.prefs
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ This library is the abstraction of Xendit API for access from applications writt
- [Get Paylater Charge by ID](#get-paylater-charge-by-id)
- [Refund Paylater Charge](#refund-paylater-charge)
- [Get Refund by Refund ID](#get-refund-by-refund-id)
- [How to get Request Id](#how-to-get-request-id)
- [Contributing](#contributing)
- [Lint](#lint)
- [Tests](#tests)
Expand Down Expand Up @@ -1886,6 +1887,28 @@ PaylaterRefund paylaterRefund = PaylaterRefund.getPaylaterRefundStatus("charge-i
PaylaterRefund paylaterRefund = xenditclient.paylater.getPaylaterRefundStatus("charge-id", "refund-id");
```

### How to get Request ID

Each API request has an asssociated request identifier. You can find this value in the response headers, under Request-ID. You can use Request-ID to find logs in [API Logs](https://dashboard.xendit.co/api-logs) in Dashboard. Learn more about Searching API Logs using Request-ID in [API Logs Docs](https://docs.xendit.co/api-integration/api-logs).

If you need to contact us about a specific request, providing the Request ID will ensure the fastest possible resolution.

The following example will show how to obtain Request-ID when creating QRCode

```java
/* Without client */
QRCode qrCode = QRCode.createQRCode("12", QRCode.QRCodeType.DYNAMIC, "IDR", 10000);
/* Xendit.getResponseHeaders() will contain all response headers after your request is completed, hence you can obtain Request-Id from header by doing the following:*/
System.out.println(Xendit.getResponseHeaders().get("Request-Id"));

/* With client */
QRCode qrCode = xenditClient.qrCode.createQRCode("external_id", QRCode.QRCodeType.DYNAMIC, "IDR", 10000);
/* Xendit.getResponseHeaders() will contain all response headers after your request is completed, hence you can obtain Request-Id from header by doing the following:*/
System.out.println(Xendit.getResponseHeaders().get("Request-Id"));
```
Full Example can be found [here](https://github.com/xendit/xendit-java/blob/9bd69bd6f4061307a5dee30287a1f7712d060527/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleCreateQRCode.java)


## Contributing
You can go to the [contributing guidelines](https://github.com/xendit/xendit-java/blob/master/CONTRIBUTING.md) to learn on how to contribute this project.

Expand Down
2 changes: 1 addition & 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.21.0'
version '1.22.0'

sourceCompatibility = 1.8

Expand Down
13 changes: 12 additions & 1 deletion xendit-java-lib/src/main/java/com/xendit/Xendit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.xendit.network.BaseRequest;
import com.xendit.network.NetworkClient;
import java.util.Map;

public class Xendit {

Expand All @@ -19,6 +20,16 @@ public static void setRequestClient(NetworkClient requestClient) {
Xendit.requestClient = requestClient;
}

protected static Map<String, String> responseHeaders;

public static Map<String, String> getResponseHeaders() {
return responseHeaders;
}

public static void setResponseHeaders(Map<String, String> headers) {
Xendit.responseHeaders = headers;
}

public static class Option {

private String apiKey;
Expand All @@ -37,7 +48,7 @@ public String getXenditURL() {
}

public String getVersion() {
return "1.21.0";
return "1.22.0";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
Expand Down Expand Up @@ -198,6 +199,14 @@ private static XenditResponse httpPatchXenditConnection(
int responseCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());

Header[] allHeaders = response.getAllHeaders();
Map<String, String> responseHeaders = new HashMap<>();
for (Header respHeader : allHeaders) {
responseHeaders.put(respHeader.getName(), respHeader.getValue());
}

Xendit.setResponseHeaders(responseHeaders);

return new XenditResponse(responseCode, responseBody);
} catch (IOException e) {
throw new XenditException("Connection error : " + e.getMessage());
Expand Down Expand Up @@ -233,6 +242,12 @@ private static XenditResponse defaultHttpXenditConnection(
responseBody = getResponseBody(connection.getErrorStream());
}

Map<String, String> responseHeaders = new HashMap<>();
for (Map.Entry<String, List<String>> headerResponse :
connection.getHeaderFields().entrySet()) {
responseHeaders.put(headerResponse.getKey(), headerResponse.getValue().get(0));
}
Xendit.setResponseHeaders(responseHeaders);
return new XenditResponse(responseCode, responseBody);
} catch (IOException e) {

Expand Down Expand Up @@ -268,12 +283,14 @@ private static String encodeBase64(String key) throws XenditException {
}
}

private static String getResponseBody(InputStream responseStream) throws IOException {
private static String getResponseBody(InputStream responseStream) throws XenditException {
try (final Scanner scanner = new Scanner(responseStream, RequestResource.CHARSET)) {
// \A is the beginning of the stream boundary
final String responseBody = scanner.useDelimiter("\\A").next();
responseStream.close();
return responseBody;
} catch (Exception e) {
throw new XenditException("Failed to get response, Please check your connection");
}
}

Expand Down
19 changes: 19 additions & 0 deletions xendit-java-lib/src/test/java/com/xenditclient/BalanceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xenditclient;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -23,12 +24,30 @@ public class BalanceTest {
NetworkClient requestClient = mock(BaseRequest.class);
Xendit.Option opt = mock(Xendit.Option.class);
BalanceClient balanceClient = mock(BalanceClient.class);
private static Map<String, String> responseHeaders =
new HashMap<String, String>() {
{
put("Request-Id", "test_request_id");
}
};

@Before
public void initMocks() {
Xendit.Opt.setApiKey(
"xnd_development_Z568GecuIH66011GIILkDFNJOoR1wFZdGqOOMFBrRVeX64DISB1o7hnNKB");
Xendit.setRequestClient(requestClient);
Xendit.setResponseHeaders(responseHeaders);
}

@Test
public void get_Success_With_RequestId() throws XenditException {
when(this.requestClient.request(
RequestResource.Method.GET, URL, HEADERS, null, opt.getApiKey(), Balance.class))
.thenReturn(VALID_BALANCE);
when(balanceClient.get()).thenReturn(VALID_BALANCE);
Balance balance = balanceClient.get();
assertEquals(balance, VALID_BALANCE);
assertNotNull(Xendit.getResponseHeaders().get("Request-Id"));
}

@Test
Expand Down
26 changes: 26 additions & 0 deletions xendit-java-lib/src/test/java/com/xenditclient/QRCodeTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xenditclient;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -26,6 +27,12 @@ public class QRCodeTest {
private static String TEST_QR_CURRENCY = "IDR";
private static Map<String, Object> PARAMS = new HashMap<>();
private static Map<String, String> HEADERS = new HashMap<>();
private static Map<String, String> responseHeaders =
new HashMap<String, String>() {
{
put("Request-Id", "test_request_id");
}
};
private static QRCode VALID_PAYMENT =
QRCode.builder()
.id(TEST_ID)
Expand All @@ -46,6 +53,7 @@ public void initMocks() {
Xendit.setRequestClient(requestClient);

PARAMS.clear();
Xendit.setResponseHeaders(responseHeaders);
}

private void initCreateParams() {
Expand All @@ -72,6 +80,24 @@ public void createQRCode_Success_IfMethodCalledCorrectly() throws XenditExceptio
assertEquals(qrCode, VALID_PAYMENT);
}

@Test
public void createQRCode_And_RequestId_Not_Null() throws XenditException {
initCreateParams();

when(this.requestClient.request(
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.createQRCode(
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000))
.thenReturn(VALID_PAYMENT);
QRCode qrCode =
qrCodeClient.createQRCode(
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000);

assertEquals(qrCode, VALID_PAYMENT);
assertNotNull(Xendit.getResponseHeaders().get("Request-Id"));
}

@Test
public void createQRCode_Success_IfParamsIsValid() throws XenditException {
initCreateParams();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ExampleWithClient;

import com.xendit.exception.XenditException;
import com.xendit.Xendit;
import com.xendit.XenditClient;
import com.xendit.model.QRCode;

Expand All @@ -11,8 +12,11 @@ public static void main(String[] args) {
.setApikey("xnd_development_...")
.build();
try {
QRCode qrCode = xenditClient.qrCode.createQRCode("external_id", QRCode.QRCodeType.DYNAMIC, "https://webhook.site", 10000);
QRCode qrCode = xenditClient.qrCode.createQRCode("external_id", QRCode.QRCodeType.DYNAMIC, "IDR", 10000);
System.out.println(qrCode.getId());

//You can find this value in the response headers, under Request-ID. You can use Request-ID to find logs in API Logs in Dashboard (https://dashboard.xendit.co/api-logs).
System.out.println(Xendit.getResponseHeaders().get("Request-Id"));
} catch (XenditException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void main(String[] args) {
FixedVirtualAccount virtualAccount3 = FixedVirtualAccount.createClosed("my_external_id",
BankCode.MANDIRI.getText(), "John Doe", 100000L, closedVAMap);

System.out.println(virtualAccount);
System.out.println(virtualAccount.getId());
} catch (XenditException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ public static void main(String[] args) {
// Xendit.apiKey = "xnd_development_...";

try {
QRCode qrCode = QRCode.createQRCode("12", QRCode.QRCodeType.DYNAMIC, "https://webhook.site", 10000);
QRCode qrCode = QRCode.createQRCode("12", QRCode.QRCodeType.DYNAMIC, "IDR", 10000);
System.out.println(qrCode.getId());

// You can find this value in the response headers, under Request-ID. You can
// use Request-ID to find logs in API Logs in Dashboard
// (https://dashboard.xendit.co/api-logs).
System.out.println(Xendit.getResponseHeaders().get("Request-Id"));
} catch (XenditException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@

public class ExampleUpdateVA {
public static void main(String[] args) {
//access key with Option
Xendit.Opt.setApiKey("xnd_development_...");
// access key with Option
Xendit.Opt.setApiKey("xnd_development...");

//access static variable (same as old code )
//Xendit.apiKey = "xnd_development_...";
// access static variable (same as old code )
// Xendit.apiKey = "xnd_development_...";

try {
Map<String, Object> params = new HashMap<>();
params.put("is_single_use", true);

FixedVirtualAccount fixedVirtualAccount = FixedVirtualAccount.update("614dbfea41de115d0d338602", params);
FixedVirtualAccount fixedVirtualAccount = FixedVirtualAccount.update("6413cdfaca7651511ae7f604", params);
System.out.println(fixedVirtualAccount.getIsSingleUse());

// You can find this value in the response headers, under Request-ID. You can
// use Request-ID to find logs in API Logs in Dashboard
// (https://dashboard.xendit.co/api-logs).
System.out.println(Xendit.getResponseHeaders().get("Request-Id"));
} catch (XenditException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit e79c5c1

Please sign in to comment.