Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tests for expected Map key order #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 61 additions & 46 deletions src/main/com/sailthru/client/AbstractSailthruClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
import com.sailthru.client.http.SailthruHttpClient;
import com.sailthru.client.params.ApiFileParams;
import com.sailthru.client.params.ApiParams;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpVersion;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
Expand All @@ -29,10 +19,20 @@
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.Map.Entry;

/**
* Abstract class exposing genric API calls for Sailthru API as per http://docs.sailthru.com/api
* @author Prajwal Tuladhar <[email protected]>
* Abstract class exposing genric API calls for Sailthru API
* @author Prajwal Tuladhar <a href="mailto:[email protected]">[email protected]</a>
* @see <a href="http://docs.sailthru.com/api">http://docs.sailthru.com/api</a>
*/
public abstract class AbstractSailthruClient {

Expand All @@ -45,7 +45,7 @@ public abstract class AbstractSailthruClient {
public static final String DEFAULT_ENCODING = "UTF-8";

//HTTP methods supported by Sailthru API
public static enum HttpRequestMethod {
public enum HttpRequestMethod {
GET,
POST,
DELETE
Expand All @@ -63,22 +63,25 @@ public static enum HttpRequestMethod {

private final SailthruHttpClientConfiguration sailthruHttpClientConfiguration;

private static final Logger logger = LoggerFactory.getLogger(AbstractSailthruClient.class);


/**
* Main constructor class for setting up the client
* @param apiKey
* @param apiSecret
* @param apiUrl
* @param apiKey description // TODO add description
* @param apiSecret description // TODO add description
* @param apiUrl description // TODO add description
*/
public AbstractSailthruClient(String apiKey, String apiSecret, String apiUrl) {
this(apiKey, apiSecret, apiUrl, new DefaultSailthruHttpClientConfiguration());
}

/**
*
* @param apiKey
* @param apiSecret
* @param apiUrl
* @param sailthruHttpClientConfiguration
* @param apiKey description // TODO add description
* @param apiSecret description // TODO add description
* @param apiUrl description // TODO add description
* @param sailthruHttpClientConfiguration description // TODO add description
*/
public AbstractSailthruClient(String apiKey, String apiSecret, String apiUrl, SailthruHttpClientConfiguration sailthruHttpClientConfiguration) {
this.apiKey = apiKey;
Expand All @@ -92,6 +95,7 @@ public AbstractSailthruClient(String apiKey, String apiSecret, String apiUrl, Sa

/**
* Create SailthruHttpClient
* @return SailthruHttpClient
*/
private SailthruHttpClient create() {
HttpParams params = new BasicHttpParams();
Expand All @@ -114,6 +118,7 @@ private SailthruHttpClient create() {

/**
* Getter for SailthruHttpClient
* @return SailthruHttpClient
*/
public SailthruHttpClient getSailthruHttpClient() {
return httpClient;
Expand All @@ -122,6 +127,7 @@ public SailthruHttpClient getSailthruHttpClient() {

/**
* Get Scheme Object
* @return Scheme
*/
protected Scheme getScheme() {
String scheme;
Expand All @@ -141,11 +147,11 @@ protected Scheme getScheme() {

/**
* Make Http request to Sailthru API for given resource with given method and data
* @param action
* @param method
* @param action action to call on the Sailthru API
* @param method request method (e.g,, GET or POST)
* @param data parameter data
* @return Object
* @throws IOException
* @throws IOException description // TODO add description
*/
protected Object httpRequest(ApiAction action, HttpRequestMethod method, Map<String, Object> data) throws IOException {
String url = this.apiUrl + "/" + action.toString();
Expand All @@ -158,9 +164,9 @@ protected Object httpRequest(ApiAction action, HttpRequestMethod method, Map<Str
/**
* Make HTTP Request to Sailthru API but with Api Params rather than generalized Map, this is recommended way to make request if data structure is complex
* @param method HTTP method
* @param apiParams
* @param apiParams parameters to pass to the Sailthru API
* @return Object
* @throws IOException
* @throws IOException description // TODO add description
*/
protected Object httpRequest(HttpRequestMethod method, ApiParams apiParams) throws IOException {
String url = apiUrl + "/" + apiParams.getApiCall().toString();
Expand All @@ -171,11 +177,11 @@ protected Object httpRequest(HttpRequestMethod method, ApiParams apiParams) thro

/**
* Make HTTP Request to Sailthru API involving multi-part uploads but with Api Params rather than generalized Map, this is recommended way to make request if data structure is complex
* @param method
* @param apiParams
* @param fileParams
* @param method description // TODO add description
* @param apiParams description // TODO add description
* @param fileParams description // TODO add description
* @return Object
* @throws IOException
* @throws IOException description // TODO add description
*/
protected Object httpRequest(HttpRequestMethod method, ApiParams apiParams, ApiFileParams fileParams) throws IOException {
String url = apiUrl + "/" + apiParams.getApiCall().toString();
Expand Down Expand Up @@ -212,6 +218,8 @@ private Map<String, String> buildPayload(String jsonPayload) {

/**
* Get Signature Hash from given Map
* @param parameters description // TODO add description
* @return String
*/
protected String getSignatureHash(Map<String, String> parameters) {
List<String> values = new ArrayList<String>();
Expand All @@ -236,35 +244,39 @@ protected String getSignatureHash(Map<String, String> parameters) {
* HTTP GET Request with Map
* @param action API action
* @param data Parameter data
* @throws IOException
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiGet(ApiAction action, Map<String, Object> data) throws IOException {
return httpRequestJson(action, HttpRequestMethod.GET, data);
}

/**
* HTTP GET Request with Interface implementation of ApiParams
* @param data
* @throws IOException
* @param data description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiGet(ApiParams data) throws IOException {
return httpRequestJson(HttpRequestMethod.GET, data);
}

/**
* HTTP POST Request with Map
* @param action
* @param data
* @throws IOException
* @param action description // TODO add description
* @param data description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiPost(ApiAction action, Map<String, Object> data) throws IOException {
return httpRequestJson(action, HttpRequestMethod.POST, data);
}

/**
* HTTP POST Request with Interface implementation of ApiParams
* @param data
* @throws IOException
* @param data description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiPost(ApiParams data) throws IOException {
return httpRequestJson(HttpRequestMethod.POST, data);
Expand All @@ -273,9 +285,10 @@ public JsonResponse apiPost(ApiParams data) throws IOException {

/**
* HTTP POST Request with Interface implementation of ApiParams and ApiFileParams
* @param data
* @param fileParams
* @throws IOException
* @param data description // TODO add description
* @param fileParams description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiPost(ApiParams data, ApiFileParams fileParams) throws IOException {
return httpRequestJson(HttpRequestMethod.POST, data, fileParams);
Expand All @@ -284,26 +297,28 @@ public JsonResponse apiPost(ApiParams data, ApiFileParams fileParams) throws IOE

/**
* HTTP DELETE Request
* @param action
* @param data
* @throws IOException
* @param action description // TODO add description
* @param data description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiDelete(ApiAction action, Map<String, Object> data) throws IOException {
return httpRequestJson(action, HttpRequestMethod.DELETE, data);
}

/**
* HTTP DELETE Request with Interface implementation of ApiParams
* @param data
* @throws IOException
* @param data description // TODO add description
* @return JsonResponse
* @throws IOException description // TODO add description
*/
public JsonResponse apiDelete(ApiParams data) throws IOException {
return httpRequestJson(HttpRequestMethod.DELETE, data);
}

/**
* Set response Handler, currently only JSON is supported but XML can also be supported later on
* @param responseHandler
* @param responseHandler description // TODO add description
*/
public void setResponseHandler(SailthruResponseHandler responseHandler) {
this.handler.setSailthruResponseHandler(responseHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/main/com/sailthru/client/ApiAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* API calls
* @author Prajwal Tuladhar <[email protected]>
* @author Prajwal Tuladhar <a href="mailto:[email protected]">[email protected]</a>
*/
public enum ApiAction {
event,
Expand Down
Loading