com.wallee.sdk.service.CountryStateService Maven / Gradle / Ivy
Show all versions of wallee-java-sdk Show documentation
package com.wallee.sdk.service;
import com.wallee.sdk.ApiClient;
import com.wallee.sdk.ErrorCode;
import com.wallee.sdk.WalleeSdkException;
import com.wallee.sdk.URIBuilderUtil;
import com.wallee.sdk.model.ClientError;
import com.wallee.sdk.model.RestCountryState;
import com.wallee.sdk.model.ServerError;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.api.client.http.*;
import com.google.api.client.json.Json;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class CountryStateService {
private ApiClient apiClient;
public CountryStateService(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
* All
* This operation returns all states of all countries.
* 200 - This status code indicates that a client request was successfully received, understood, and accepted.
*
442 - This status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
*
542 - This status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the client request.
* @return List<RestCountryState>
* @throws IOException if an error occurs while attempting to invoke the API
* For more information visit this link.
* @see All Documentation
**/
public List all() throws IOException {
HttpResponse response = allForHttpResponse();
String returnType = "List<RestCountryState>";
if(returnType.equals("String")){
return (List) (Object) response.parseAsString();
}
TypeReference typeRef = new TypeReference>() {};
if (isNoBodyResponse(response)) {
throw new WalleeSdkException(ErrorCode.ENTITY_NOT_FOUND, "Entity was not found for: " + typeRef.getType().getTypeName());
}
return (List)apiClient.getObjectMapper().readValue(response.getContent(), typeRef);
}
/**
* All
* This operation returns all states of all countries.
* 200 - This status code indicates that a client request was successfully received, understood, and accepted.
*
442 - This status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
*
542 - This status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the client request.
* @param params Map of query params. A collection will be interpreted as passing in multiple instances of the same query param.
* @return List<RestCountryState>
* @throws IOException if an error occurs while attempting to invoke the API
* For more information visit this link.
* @see All Documentation
**/
public List all(Map params) throws IOException {
HttpResponse response = allForHttpResponse(params);
String returnType = "List<RestCountryState>";
if(returnType.equals("String")){
return (List) (Object) response.parseAsString();
}
TypeReference typeRef = new TypeReference>() {};
if (isNoBodyResponse(response)) {
throw new WalleeSdkException(ErrorCode.ENTITY_NOT_FOUND, "Entity was not found for: " + typeRef.getType().getTypeName());
}
return (List)apiClient.getObjectMapper().readValue(response.getContent(), typeRef);
}
public HttpResponse allForHttpResponse() throws IOException {
URIBuilder uriBuilder = URIBuilderUtil.create(apiClient.getBasePath() + "/country-state/all");
GenericUrl genericUrl = new GenericUrl(URIBuilderUtil.build(uriBuilder));
HttpContent content = null;
HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.GET, genericUrl, content);
httpRequest.getHeaders().setContentType("*/*");
int readTimeOut = apiClient.getReadTimeOut() * 1000;
httpRequest.setReadTimeout(readTimeOut);
return httpRequest.execute();
}
public HttpResponse allForHttpResponse(Map params) throws IOException {
URIBuilder uriBuilder = URIBuilderUtil.create(apiClient.getBasePath() + "/country-state/all");
// Copy the params argument if present, to allow passing in immutable maps
Map allParams = params == null ? new HashMap() : new HashMap(params);
for (Map.Entry entryMap: allParams.entrySet()) {
String key = entryMap.getKey();
Object value = entryMap.getValue();
if (key != null && value != null) {
uriBuilder = URIBuilderUtil.applyQueryParam(uriBuilder, key, value);
}
}
GenericUrl genericUrl = new GenericUrl(URIBuilderUtil.build(uriBuilder));
HttpContent content = null;
HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.GET, genericUrl, content);
httpRequest.getHeaders().setContentType("*/*");
int readTimeOut = apiClient.getReadTimeOut() * 1000;
httpRequest.setReadTimeout(readTimeOut);
return httpRequest.execute();
}
/**
* Find by Country
* This operation returns all states for a given country.
* 200 - This status code indicates that a client request was successfully received, understood, and accepted.
*
442 - This status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
*
542 - This status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the client request.
* @param code The country code in ISO code two letter format for which all states should be returned.
* @return List<RestCountryState>
* @throws IOException if an error occurs while attempting to invoke the API
* For more information visit this link.
* @see Find by Country Documentation
**/
public List country(String code) throws IOException {
HttpResponse response = countryForHttpResponse(code);
String returnType = "List<RestCountryState>";
if(returnType.equals("String")){
return (List) (Object) response.parseAsString();
}
TypeReference typeRef = new TypeReference>() {};
if (isNoBodyResponse(response)) {
throw new WalleeSdkException(ErrorCode.ENTITY_NOT_FOUND, "Entity was not found for: " + typeRef.getType().getTypeName());
}
return (List)apiClient.getObjectMapper().readValue(response.getContent(), typeRef);
}
/**
* Find by Country
* This operation returns all states for a given country.
* 200 - This status code indicates that a client request was successfully received, understood, and accepted.
*
442 - This status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
*
542 - This status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the client request.
* @param code The country code in ISO code two letter format for which all states should be returned.
* @param params Map of query params. A collection will be interpreted as passing in multiple instances of the same query param.
* @return List<RestCountryState>
* @throws IOException if an error occurs while attempting to invoke the API
* For more information visit this link.
* @see Find by Country Documentation
**/
public List country(String code, Map params) throws IOException {
HttpResponse response = countryForHttpResponse(code, params);
String returnType = "List<RestCountryState>";
if(returnType.equals("String")){
return (List) (Object) response.parseAsString();
}
TypeReference typeRef = new TypeReference>() {};
if (isNoBodyResponse(response)) {
throw new WalleeSdkException(ErrorCode.ENTITY_NOT_FOUND, "Entity was not found for: " + typeRef.getType().getTypeName());
}
return (List)apiClient.getObjectMapper().readValue(response.getContent(), typeRef);
}
public HttpResponse countryForHttpResponse(String code) throws IOException {
// verify the required parameter 'code' is set
if (code == null) {
throw new IllegalArgumentException("Missing the required parameter 'code' when calling country");
}
URIBuilder uriBuilder = URIBuilderUtil.create(apiClient.getBasePath() + "/country-state/country");
if (code != null) {
String key = "code";
Object value = code;
uriBuilder = URIBuilderUtil.applyQueryParam(uriBuilder, key, value);
}
GenericUrl genericUrl = new GenericUrl(URIBuilderUtil.build(uriBuilder));
HttpContent content = null;
HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.GET, genericUrl, content);
httpRequest.getHeaders().setContentType("*/*");
int readTimeOut = apiClient.getReadTimeOut() * 1000;
httpRequest.setReadTimeout(readTimeOut);
return httpRequest.execute();
}
public HttpResponse countryForHttpResponse(String code, Map params) throws IOException {
// verify the required parameter 'code' is set
if (code == null) {
throw new IllegalArgumentException("Missing the required parameter 'code' when calling country");
}
URIBuilder uriBuilder = URIBuilderUtil.create(apiClient.getBasePath() + "/country-state/country");
// Copy the params argument if present, to allow passing in immutable maps
Map allParams = params == null ? new HashMap() : new HashMap(params);
// Add the required query param 'code' to the map of query params
allParams.put("code", code);
for (Map.Entry entryMap: allParams.entrySet()) {
String key = entryMap.getKey();
Object value = entryMap.getValue();
if (key != null && value != null) {
uriBuilder = URIBuilderUtil.applyQueryParam(uriBuilder, key, value);
}
}
GenericUrl genericUrl = new GenericUrl(URIBuilderUtil.build(uriBuilder));
HttpContent content = null;
HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.GET, genericUrl, content);
httpRequest.getHeaders().setContentType("*/*");
int readTimeOut = apiClient.getReadTimeOut() * 1000;
httpRequest.setReadTimeout(readTimeOut);
return httpRequest.execute();
}
private boolean isNoBodyResponse(HttpResponse response) throws IOException {
java.io.InputStream content = response.getContent();
return content.available() == 0;
}
}