Skip to content

Commit

Permalink
we don't really need to set multivalued map to set our API client hea…
Browse files Browse the repository at this point in the history
…ders, by not doing so we reduce the number of javax packages usage to a single file
  • Loading branch information
ibalosh committed Feb 27, 2023
1 parent a2c1dee commit 065b858
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ out
src/test/java/temp
src/test/resources/*properties*
.gradle

.java-version
20 changes: 10 additions & 10 deletions src/main/java/com/postmarkapp/postmark/Postmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.postmarkapp.postmark.client.AccountApiClient;
import com.postmarkapp.postmark.client.ApiClient;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

Expand Down Expand Up @@ -34,11 +34,11 @@ public enum DEFAULTS {
*/
public static class DefaultHeaders {

public static MultivaluedMap<String, Object> headers() {
MultivaluedMap headerValues = new MultivaluedHashMap<>();
headerValues.putSingle("User-Agent", "Postmark Java Library: " + libraryVersion());
headerValues.putSingle("Accept", "application/json");
headerValues.putSingle("Content-Type", "application/json");
public static Map<String, Object> headers() {
Map headerValues = new HashMap();
headerValues.put("User-Agent", "Postmark Java Library: " + libraryVersion());
headerValues.put("Accept", "application/json");
headerValues.put("Content-Type", "application/json");
return headerValues;
}

Expand Down Expand Up @@ -94,9 +94,9 @@ private Postmark() {}

private final static Logger LOGGER = Logger.getLogger(Postmark.class.getName());

private static MultivaluedMap<String,Object> getHeadersWithAuth(DEFAULTS authType, String apiToken) {
MultivaluedMap headers = DefaultHeaders.headers();
headers.putSingle(authType.value, apiToken);
private static Map<String,Object> getHeadersWithAuth(DEFAULTS authType, String apiToken) {
Map headers = DefaultHeaders.headers();
headers.put(authType.value, apiToken);
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.postmarkapp.postmark.client.data.model.templates.TemplatesPushRequest;
import com.postmarkapp.postmark.client.exception.PostmarkException;

import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.util.Map;

/**
* Class that handles on very top level all API requests. All Postmark public endpoints which
Expand All @@ -26,11 +26,11 @@ public class AccountApiClient extends BaseApiClient {
private final String domainsEndpoint = "/domains/";
private final String sendersEndpoint = "/senders/";

public AccountApiClient(String baseUrl, MultivaluedMap<String,Object> headers) {
public AccountApiClient(String baseUrl, Map<String,Object> headers) {
super(baseUrl,headers);
}

public AccountApiClient(String baseUrl, MultivaluedMap<String,Object> headers, boolean secureConnection) {
public AccountApiClient(String baseUrl, Map<String,Object> headers, boolean secureConnection) {
super(baseUrl, headers, secureConnection);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/postmarkapp/postmark/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.postmarkapp.postmark.client.data.model.webhooks.Webhooks;
import com.postmarkapp.postmark.client.exception.PostmarkException;

import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Class that handles on very top level all API requests. All Postmark public endpoints which
Expand All @@ -51,11 +51,11 @@ public class ApiClient extends BaseApiClient {
private final String suppressionsEndpoint = "/suppressions/";
private final String messageStreamsEndpoint = "/message-streams/";

public ApiClient(String baseUrl, MultivaluedMap<String, Object> headers) {
public ApiClient(String baseUrl, Map<String, Object> headers) {
super(baseUrl,headers);
}

public ApiClient(String baseUrl, MultivaluedMap<String, Object> headers, boolean secureConnection) {
public ApiClient(String baseUrl, Map<String, Object> headers, boolean secureConnection) {
super(baseUrl, headers, secureConnection);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.postmarkapp.postmark.client;

import javax.ws.rs.core.MultivaluedMap;
import java.util.Map;

/**
* Class that handles (on very high level) API requests. All Postmark public endpoints which
Expand All @@ -14,12 +14,12 @@ public String getEndpointUrl(String endpoint) {
return baseUrl + endpoint;
}

public BaseApiClient(String baseUrl, MultivaluedMap<String,Object> headers) {
public BaseApiClient(String baseUrl, Map<String,Object> headers) {
super(headers);
this.baseUrl = baseUrl;
}

public BaseApiClient(String baseUrl, MultivaluedMap<String,Object> headers, boolean secureConnection) {
public BaseApiClient(String baseUrl, Map<String,Object> headers, boolean secureConnection) {
this(baseUrl, headers);
setSecureConnection(secureConnection);
}
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/com/postmarkapp/postmark/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Response;
import java.util.Map;

/**
* Base HTTP client class solely responsible for making
Expand All @@ -29,18 +29,18 @@ public enum DEFAULTS {
}
}

private final MultivaluedMap<String,Object> headers;
private final Map<String,Object> headers;
private final Client client;

private boolean secureConnection = true;

public HttpClient(MultivaluedMap<String,Object> headers, int connectTimeoutSeconds, int readTimeoutSeconds) {
public HttpClient(Map<String,Object> headers, int connectTimeoutSeconds, int readTimeoutSeconds) {
this(headers);
setConnectTimeoutSeconds(connectTimeoutSeconds);
setReadTimeoutSeconds(readTimeoutSeconds);
}

public HttpClient(MultivaluedMap<String,Object> headers) {
public HttpClient(Map<String,Object> headers) {
this.headers = headers;
this.client = buildClient();
setReadTimeoutSeconds(DEFAULTS.READ_TIMEOUT_SECONDS.value);
Expand All @@ -58,31 +58,27 @@ public HttpClient(MultivaluedMap<String,Object> headers) {
*/
public ClientResponse execute(REQUEST_TYPES requestType, String url, String data) {
Response response;
final WebTarget target = client.target(getHttpUrl(url));
final Builder requestBuilder = clientRequestBuilder((url));

switch (requestType) {
case POST:
response = target.request().headers(headers).post(Entity.json(data), Response.class);
break;

case GET:
response = target.request().headers(headers).get(Response.class);
response = requestBuilder.post(Entity.json(data), Response.class);
break;

case PUT:
response = target.request().headers(headers).put(Entity.json(data), Response.class);
response = requestBuilder.put(Entity.json(data), Response.class);
break;

case PATCH:
response = target.request().headers(headers).method("PATCH", Entity.json(data), Response.class);
response = requestBuilder.method("PATCH", Entity.json(data), Response.class);
break;

case DELETE:
response = target.request().headers(headers).delete(Response.class);
response = requestBuilder.delete(Response.class);
break;

default:
response = target.request().headers(headers).get(Response.class);
response = requestBuilder.get(Response.class);
break;

}
Expand Down Expand Up @@ -150,6 +146,16 @@ private Client buildClient() {
return client;
}

private Builder clientRequestBuilder(String url) {
Builder requestBuilder = client.target(getHttpUrl(url)).request();

for (Map.Entry<String, Object> header : headers.entrySet()) {
requestBuilder.header(header.getKey(), header.getValue());
}

return requestBuilder;
}

/**
* Build HTTP client with custom config used for requests
* like:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.postmarkapp.postmark.client.data.parser.DataHandler;
import com.postmarkapp.postmark.client.exception.*;

import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.util.Map;

/**
* Client class acts as handler between HTTP requests handler class (HttpClient) and class which provides access to all endpoints to call.
Expand All @@ -16,13 +16,13 @@ public class HttpClientHandler {
protected final DataHandler dataHandler;
private final HttpClientErrorHandler httpClientErrorHandler;

protected HttpClientHandler(MultivaluedMap<String,Object> headers) {
protected HttpClientHandler(Map<String,Object> headers) {
this.dataHandler = new DataHandler(false);
this.httpClientErrorHandler = new HttpClientErrorHandler(this.dataHandler);
httpClient = new HttpClient(headers);
}

protected HttpClientHandler(MultivaluedMap<String,Object> headers, boolean secureConnection) {
protected HttpClientHandler(Map<String,Object> headers, boolean secureConnection) {
this(headers);
this.getHttpClient().setSecureConnection(secureConnection);
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/unit/client/ApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import com.postmarkapp.postmark.Postmark;
import com.postmarkapp.postmark.client.ApiClient;
import javax.ws.rs.core.MultivaluedMap;

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Created by bash on 11/13/17.
*/
public class ApiClientTest {

MultivaluedMap headers = Postmark.DefaultHeaders.headers();
Map headers = Postmark.DefaultHeaders.headers();
ApiClient client = new ApiClient(Postmark.DEFAULTS.API_URL.value, headers);

@Test
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/unit/client/BaseApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import com.postmarkapp.postmark.client.HttpClient;
import org.junit.jupiter.api.Test;

import javax.ws.rs.core.MultivaluedMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Created by bash on 11/13/17.
*/
public class BaseApiClientTest {

MultivaluedMap headers = Postmark.DefaultHeaders.headers();
Map headers = Postmark.DefaultHeaders.headers();
BaseApiClient client = new BaseApiClient(Postmark.DEFAULTS.API_URL.value, headers);

@Test
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/unit/client/HttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.postmarkapp.postmark.client.exception.PostmarkException;
import org.junit.jupiter.api.Test;

import javax.ws.rs.core.MultivaluedHashMap;
import java.io.IOException;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -16,23 +16,23 @@ public class HttpClientTest {

@Test
void execute() throws IOException, PostmarkException {
HttpClient client = new HttpClient(new MultivaluedHashMap<>());
HttpClient client = new HttpClient(new HashMap<>());
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, Postmark.DEFAULTS.API_URL.value);

assertNotNull(response.getMessage());
}

@Test
void executeIncorrectLink() throws IOException, PostmarkException {
HttpClient client = new HttpClient(new MultivaluedHashMap<>());
HttpClient client = new HttpClient(new HashMap<>());
HttpClient.ClientResponse response = client.execute(HttpClient.REQUEST_TYPES.GET, Postmark.DEFAULTS.API_URL.value + "/someweirdlink");

assertEquals(response.getCode(),404);
}

@Test
void getClient() throws IOException, PostmarkException {
HttpClient client = new HttpClient(new MultivaluedHashMap<>());
HttpClient client = new HttpClient(new HashMap<>());
assertNotNull(client.getClient());
}
}

0 comments on commit 065b858

Please sign in to comment.