From 07245f08d5ee5740ed265b4ea04b935c2d25b40f Mon Sep 17 00:00:00 2001 From: Mohamed Yousif Date: Thu, 25 Aug 2022 11:31:06 +0200 Subject: [PATCH] Add url params to the api --- .../main/java/com/tuti/api/TutiApiClient.java | 23 +++++++++++++------ .../java/com/tuti/api/data/PaymentToken.java | 2 -- .../java/com/tuti/api/TutiApiClientTest.java | 18 +++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 lib/src/test/java/com/tuti/api/TutiApiClientTest.java diff --git a/lib/src/main/java/com/tuti/api/TutiApiClient.java b/lib/src/main/java/com/tuti/api/TutiApiClient.java index 899b8b1..1d3e2eb 100644 --- a/lib/src/main/java/com/tuti/api/TutiApiClient.java +++ b/lib/src/main/java/com/tuti/api/TutiApiClient.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; +import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -90,8 +91,8 @@ private static synchronized Gson getGsonInstance() { } private String getServerURL(boolean development) { - String developmentHost = "https://staging.app.2t.sd/consumer/"; - String productionHost = "https://staging.app.2t.sd/consumer/"; + String developmentHost = "staging.app.2t.sd/consumer/"; + String productionHost = "staging.app.2t.sd/consumer/"; return development ? developmentHost : productionHost; } @@ -197,26 +198,32 @@ public void generatePaymentToken(PaymentToken request, ResponseCallable onResponse, ErrorCallable onError) { - sendRequest(RequestMethods.GET, serverURL + Operations.GetPaymentToken, request, TutiResponse.class, TutiResponse.class, onResponse, onError, null); + public void getPaymentToken(String uuid, ResponseCallable onResponse, ErrorCallable onError) { + sendRequest(RequestMethods.GET, serverURL + Operations.GetPaymentToken, null, TutiResponse.class, TutiResponse.class, onResponse, onError, null, "uuid", uuid); } public void quickPayment(EBSRequest request, ResponseCallable onResponse, ErrorCallable onError) { sendRequest(RequestMethods.POST, serverURL + Operations.QuickPayment, request, TutiResponse.class, TutiResponse.class, onResponse, onError, null); } - public Thread sendRequest(RequestMethods method, String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError, Map headers) { + public Thread sendRequest(RequestMethods method, String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError, Map headers, String ...params) { // create a runnable to run it in a new thread (so main thread never hangs) Runnable runnable = () -> { OkHttpClient okHttpClient = getOkHttpInstance(); - Gson gson = getGsonInstance(); + Gson gson = getGsonInstance(); RequestBody requestBody = RequestBody.create(gson.toJson(requestToBeSent), JSON); - Request.Builder requestBuilder = new Request.Builder().url(URL); + HttpUrl url; + if (params != null) { + url = new HttpUrl.Builder().scheme("https").host(URL).addQueryParameter(params[0], params[1]).build(); + }else { + url = new HttpUrl.Builder().scheme("https").host(URL).build(); + } + Request.Builder requestBuilder = new Request.Builder().url(url); if (authToken != null) requestBuilder.header("Authorization", authToken); //add additional headers set by the user @@ -234,10 +241,12 @@ public Thread sendRequest(RequestMethods method, String URL, Object requestToBeS } else if (method == RequestMethods.PUT) { requestBuilder.put(requestBody); }else { + requestBuilder.get(); } Request request = requestBuilder.build(); + try (Response rawResponse = okHttpClient.newCall(request).execute()) { // check for http errors int responseCode = rawResponse.code(); diff --git a/lib/src/main/java/com/tuti/api/data/PaymentToken.java b/lib/src/main/java/com/tuti/api/data/PaymentToken.java index 7fed25a..3aadc6a 100644 --- a/lib/src/main/java/com/tuti/api/data/PaymentToken.java +++ b/lib/src/main/java/com/tuti/api/data/PaymentToken.java @@ -15,8 +15,6 @@ */ public class PaymentToken implements Serializable { private int amount; - private String token; - /** * ParseQRToken creates a PaymentToken object from a base64 encoded QR code. This is method * can be used when showing a tuti encoded QR token to a user, the user can then scan that diff --git a/lib/src/test/java/com/tuti/api/TutiApiClientTest.java b/lib/src/test/java/com/tuti/api/TutiApiClientTest.java new file mode 100644 index 0000000..e9e4dae --- /dev/null +++ b/lib/src/test/java/com/tuti/api/TutiApiClientTest.java @@ -0,0 +1,18 @@ +package com.tuti.api; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +class TutiApiClientTest { + + @Test + void getPaymentToken() { + TutiApiClient tutiApiClient = new TutiApiClient(); + UUID uuid = UUID.randomUUID(); + tutiApiClient.getPaymentToken(uuid.toString(),null, null ); + + } +} \ No newline at end of file