Skip to content

Commit

Permalink
Add url params to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
adonese committed Aug 25, 2022
1 parent 6d18703 commit 07245f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
23 changes: 16 additions & 7 deletions lib/src/main/java/com/tuti/api/TutiApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -197,26 +198,32 @@ public void generatePaymentToken(PaymentToken request, ResponseCallable<TutiResp
sendRequest(RequestMethods.POST, serverURL + Operations.GeneratePaymentToken, request, TutiResponse.class, TutiResponse.class, onResponse, onError, null);
}

public void getPaymentToken(PaymentToken request, ResponseCallable<TutiResponse> onResponse, ErrorCallable<TutiResponse> onError) {
sendRequest(RequestMethods.GET, serverURL + Operations.GetPaymentToken, request, TutiResponse.class, TutiResponse.class, onResponse, onError, null);
public void getPaymentToken(String uuid, ResponseCallable<TutiResponse> onResponse, ErrorCallable<TutiResponse> onError) {
sendRequest(RequestMethods.GET, serverURL + Operations.GetPaymentToken, null, TutiResponse.class, TutiResponse.class, onResponse, onError, null, "uuid", uuid);
}

public void quickPayment(EBSRequest request, ResponseCallable<PaymentToken> onResponse, ErrorCallable<TutiResponse> 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<String, String> headers) {
public Thread sendRequest(RequestMethods method, String URL, Object requestToBeSent, Type ResponseType, Type ErrorType, ResponseCallable onResponse, ErrorCallable onError, Map<String, String> 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
Expand All @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions lib/src/main/java/com/tuti/api/data/PaymentToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/src/test/java/com/tuti/api/TutiApiClientTest.java
Original file line number Diff line number Diff line change
@@ -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 );

}
}

0 comments on commit 07245f0

Please sign in to comment.