use of jp.ne.paypay.model.ResponseParameters in project paypayopa-sdk-java by paypay.
the class ApiClient method execute.
/**
* Execute HTTP call and deserialize the HTTP response body into the given return type.
*
* @param returnType The return type used to deserialize HTTP response body
* @param <T> The return type corresponding to (same with) returnType
* @param call Call
* @param apiName The API Name for resolve url
* @return ApiResponse object containing response status, headers and
* data, which is a Java object deserialized from response body and would be null
* when returnType is null.
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType, String apiName) throws ApiException {
try {
Response response = call.execute();
ResponseParameters responseParameters = new ResponseParameters().setResponse(response).setApiName(apiName).setReturnType(returnType);
T data = handleResponse(responseParameters);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
}
}
use of jp.ne.paypay.model.ResponseParameters in project paypayopa-sdk-java by paypay.
the class ApiClientTest method handleUnsuccessfulResponseTest.
@Test
public void handleUnsuccessfulResponseTest() {
Request.Builder requestBuild = new Request.Builder();
requestBuild.url("http://paypay.ne.jp/v2/qrcode");
requestBuild.header("content-type", "application/json");
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "{\"merchantPaymentId\":'paymentId'}");
requestBuild.post(requestBody);
Request request = requestBuild.build();
Response.Builder builder = new Response.Builder();
builder.header("content-type", "application/json");
builder.request(request);
builder.code(400);
builder.protocol(Protocol.HTTP_2);
ResultInfo resultInfo = new ResultInfo();
resultInfo.setMessage("FAILED");
QRCodeDetails qrCodeDetails = new QRCodeDetails();
qrCodeDetails.setResultInfo(resultInfo);
JSON json = new JSON();
ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), json.serialize(qrCodeDetails));
builder.body(responseBody);
Response response = builder.build();
Type localVarReturnType = new TypeToken<QRCodeDetails>() {
}.getType();
Assert.assertThrows(ApiException.class, () -> apiClient.handleResponse(new ResponseParameters().setResponse(response).setReturnType(localVarReturnType)));
Assert.assertThrows(ApiException.class, () -> apiClient.handleResponse(new ResponseParameters().setResponse(response)));
}
use of jp.ne.paypay.model.ResponseParameters in project paypayopa-sdk-java by paypay.
the class ApiClientTest method handleResponseTest.
@Test
public void handleResponseTest() throws ApiException, IOException {
Request.Builder requestBuild = new Request.Builder();
requestBuild.url("http://paypay.ne.jp/v2/qrcode");
requestBuild.header("content-type", "application/json");
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "{\"merchantPaymentId\":'paymentId'}");
requestBuild.post(requestBody);
Request request = requestBuild.build();
Response.Builder builder = new Response.Builder();
builder.header("content-type", "application/json");
builder.request(request);
builder.code(200);
builder.protocol(Protocol.HTTP_2);
ResultInfo resultInfo = new ResultInfo();
resultInfo.setMessage("SUCCESS");
QRCodeDetails qrCodeDetails = new QRCodeDetails();
qrCodeDetails.setResultInfo(resultInfo);
JSON json = new JSON();
builder.body(ResponseBody.create(MediaType.parse("application/json"), json.serialize(qrCodeDetails)));
Response response = builder.build();
Type localVarReturnType = new TypeToken<QRCodeDetails>() {
}.getType();
ResponseParameters responseParameters = new ResponseParameters().setResponse(response).setReturnType(localVarReturnType).setApiName("v2_createPayment");
Object result = apiClient.handleResponse(responseParameters);
Assert.assertTrue(result instanceof QRCodeDetails);
responseParameters.setReturnType(null).setApiName(null);
result = apiClient.handleResponse(new ResponseParameters().setResponse(response));
Assert.assertFalse(result instanceof QRCodeDetails);
localVarReturnType = new TypeToken<File>() {
}.getType();
result = apiClient.handleResponse(responseParameters.setReturnType(localVarReturnType));
Assert.assertTrue(result instanceof File);
Mockito.when(call.execute()).thenReturn(response);
ApiResponse<Object> apiResponse = apiClient.execute(call, localVarReturnType, null);
Assert.assertEquals(200, apiResponse.getStatusCode());
Mockito.when(call.execute()).thenThrow(IOException.class);
Assert.assertThrows(ApiException.class, () -> apiClient.execute(call, new TypeToken<File>() {
}.getType(), ""));
resultInfo = new ResultInfo();
resultInfo.setMessage("You are not authorized");
resultInfo.setCode("UNAUTHORIZED");
resultInfo.setCodeId("802001");
NotDataResponse notDataResponse = new NotDataResponse();
notDataResponse.setResultInfo(resultInfo);
builder.code(401);
builder.body(ResponseBody.create(MediaType.parse("application/json"), json.serialize(notDataResponse)));
response = builder.build();
responseParameters.setResponse(response).setReturnType(localVarReturnType).setApiName("v2_createPayment");
try {
apiClient.handleResponse(responseParameters);
} catch (ApiException e) {
Assert.assertEquals(e.getResolveUrl(), "https://developer.paypay.ne.jp/develop/resolve?api_name=v2_createPayment&code=UNAUTHORIZED&code_id=802001");
Assert.assertTrue(e.getResponseBody().contains("UNAUTHORIZED"));
}
}
Aggregations