Skip to content
Merged
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
6 changes: 3 additions & 3 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.phaxio</groupId>
<artifactId>phaxio-java</artifactId>
<version>0.3.8</version>
<version>0.4.0</version>

<name>Phaxio Java Client</name>
<description>The official Phaxio client for the JVM</description>
Expand Down Expand Up @@ -37,8 +37,8 @@
<connection>scm:git:[email protected]:phaxio/phaxio-java.git</connection>
<developerConnection>scm:git:[email protected]:phaxio/phaxio-java.git</developerConnection>
<url>[email protected]:phaxio/phaxio-java.git</url>
<tag>phaxio-java-0.3.4</tag>
</scm>
<tag>phaxio-java-0.4.0</tag>
</scm>

<dependencies>
<dependency>
Expand Down
14 changes: 8 additions & 6 deletions client/src/main/java/com/phaxio/Phaxio.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
* The Phaxio API client.
*/
public class Phaxio {
private static final String PHAXIO_ENDPOINT = "https://api.phaxio.com:%s/v2/";
private static final String PHAXIO_SERVICE = "https://api.phaxio.com:%s";
private static final String PHAXIO_VERSION_SEGMENT = "/v2.1/";
private static final String PHAXIO_ENDPOINT = PHAXIO_SERVICE + PHAXIO_VERSION_SEGMENT;
private static final int PHAXIO_PORT = 443;

public Phaxio(String key, String secret) {
this(key, secret, PHAXIO_ENDPOINT, PHAXIO_PORT,null);
}

public Phaxio(String key, String secret,Proxy proxy) {
this(key, secret, PHAXIO_ENDPOINT, PHAXIO_PORT,proxy);
public Phaxio(String key, String secret, Proxy proxy) {
this(key, secret, PHAXIO_ENDPOINT, PHAXIO_PORT, proxy);
}

public Phaxio(String key, String secret, String endpoint, int port) {
this(key,secret,endpoint,port,null);
this(key, secret, endpoint, port, null);
}

private Phaxio(String key, String secret, String endpoint, int port,Proxy proxy) {
Requests requests = new Requests(key, secret, endpoint, port,proxy);
private Phaxio(String key, String secret, String endpoint, int port, Proxy proxy) {
Requests requests = new Requests(key, secret, endpoint, port, proxy);

fax = new FaxRepository(requests);
publicInfo = new PublicRepository(requests);
Expand Down
20 changes: 20 additions & 0 deletions client/src/main/java/com/phaxio/entities/Barcode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.phaxio.entities;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Barcode {
@JsonProperty("type")
public String type;

@JsonProperty("page")
public int page;

@JsonProperty("value")
public String value;

@JsonProperty("identifier")
public String identifier;

@JsonProperty("metadata")
public String metadata;
}
13 changes: 10 additions & 3 deletions client/src/main/java/com/phaxio/resources/Fax.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.phaxio.services.Requests;
import com.phaxio.entities.Barcode;
import com.phaxio.entities.Recipient;
import com.phaxio.restclient.entities.RestRequest;

Expand Down Expand Up @@ -40,9 +41,6 @@ public Fax()
@JsonProperty("id")
public int id;

@JsonProperty("direction")
public String direction;

public int getId() {
return id;
}
Expand All @@ -51,6 +49,15 @@ public void setId(int id) {
this.id = id;
}

@JsonProperty("direction")
public String direction;

@JsonProperty("barcodes")
public List<Barcode> barcodes;

@JsonProperty("caller_name")
public String callerName;

@JsonProperty("num_pages")
public int pageCount;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.phaxio.restclient;

public class BasicAuthentication {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP calls this Authorization, not Authentication.

public class BasicAuthorization {
public final String username;
public final String password;

public BasicAuthentication(String username, String password) {
public BasicAuthorization(String username, String password) {
this.username = username;
this.password = password;
}
Expand Down
10 changes: 6 additions & 4 deletions client/src/main/java/com/phaxio/restclient/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
public class RestClient {
private static final int TIMEOUT = 30000;
private final String endpoint;
private BasicAuthentication auth;
private BasicAuthorization auth;
final private Proxy proxy;

public RestClient(String endpoint,Proxy proxy) {
public RestClient(String endpoint, Proxy proxy) {
this.endpoint = endpoint;
this.proxy = proxy;
}

public void setAuthentication(BasicAuthentication auth) {
public void setAuthorization(BasicAuthorization auth) {
this.auth = auth;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public RestResponse execute(RestRequest request) {
dos.writeBytes("--\r\n");
dos.flush();
dos.close();
} else {
} else if (!request.parameters.isEmpty()) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were always sending at least the authorization parameters, so this client never had to deal with an empty body. This allows that.

byte[] body = getQueryString(request).substring(1).getBytes("UTF-8");

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Expand All @@ -113,6 +113,8 @@ public RestResponse execute(RestRequest request) {
dos.write(body);
dos.flush();
dos.close();
} else {
conn.setRequestProperty("Content-Length", "0");
}

break;
Expand Down
20 changes: 5 additions & 15 deletions client/src/main/java/com/phaxio/services/Requests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.phaxio.entities.Paging;
import com.phaxio.exceptions.*;
import com.phaxio.restclient.BasicAuthorization;
import com.phaxio.restclient.RestClient;
import com.phaxio.restclient.entities.Method;
import com.phaxio.restclient.entities.RestRequest;
Expand All @@ -19,25 +20,17 @@
import java.util.*;

public class Requests {

private static final String KEY_PARAMETER = "api_key";
private static final String SECRET_PARAMETER = "api_secret";

private final String key;
private final String secret;
private final RestClient client;

public Requests(String key, String secret, String endpoint, int port) {
this(key,secret,endpoint,port,null);
this(key, secret, endpoint, port, null);
}

public Requests(String key, String secret, String endpoint, int port,Proxy proxy) {
this.secret = secret;
this.key = key;

public Requests(String key, String secret, String endpoint, int port, Proxy proxy) {
String endpointWithPort = String.format(endpoint, port);

client = new RestClient(endpointWithPort,proxy);
client = new RestClient(endpointWithPort, proxy);
client.setAuthorization(new BasicAuthorization(key, secret));
}

public <T> T get(RestRequest request, Class clazz) {
Expand Down Expand Up @@ -161,9 +154,6 @@ public void delete(RestRequest request) {
}

private RestResponse execute(RestRequest request) {
request.addOrReplaceParameter(SECRET_PARAMETER, secret);
request.addOrReplaceParameter(KEY_PARAMETER, key);

RestResponse response = client.execute(request);

// Check connection errors
Expand Down
16 changes: 16 additions & 0 deletions client/src/test/java/com/phaxio/helpers/Auth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.phaxio.helpers;

import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.phaxio.restclient.BasicAuthorization;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;

public class Auth {
public static final String VALID_KEY = "KEY";
public static final String VALID_SECRET = "SECRET";
public static final StringValuePattern VALID_AUTH_MATCHER = equalTo(new BasicAuthorization(VALID_KEY, VALID_SECRET).toHeader());

public static StringValuePattern authMatcher(String username, String password) {
return equalTo(new BasicAuthorization(username, password).toHeader());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.phaxio.Phaxio;
import com.phaxio.entities.Account;
import com.phaxio.helpers.Auth;
import com.phaxio.helpers.Responses;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -22,13 +23,14 @@ public class AccountRepositoryTest {
public void getsAccountStatus () throws IOException {
String json = Responses.json("/account_status.json");

stubFor(get(urlEqualTo("/v2/account/status?api_secret=SECRET&api_key=KEY"))
stubFor(get(urlEqualTo("/ver/account/status"))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the version string isn't really important, I'm just faking it with the placeholder ver which would allow us to change versions but not update all these places in the tests.

.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBody(json)));

Phaxio phaxio = new Phaxio("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Phaxio phaxio = new Phaxio(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

Account account = phaxio.account.status();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.phaxio.Phaxio;
import com.phaxio.entities.AreaCode;
import com.phaxio.helpers.Auth;
import com.phaxio.helpers.Responses;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -25,13 +26,14 @@ public class AreaCodeRepositoryTest {
public void listsAreaCodes () throws IOException {
String json = Responses.json("/list_area_codes.json");

stubFor(get(urlEqualTo("/v2/public/area_codes?api_secret=SECRET&api_key=KEY"))
stubFor(get(urlEqualTo("/ver/public/area_codes"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBody(json)));

Phaxio phaxio = new Phaxio("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Phaxio phaxio = new Phaxio(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

Iterable<AreaCode> codes = phaxio.publicInfo.areaCode.list();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.phaxio.Phaxio;
import com.phaxio.fixtures.BinaryFixtures;
import com.phaxio.helpers.Auth;
import com.phaxio.helpers.Responses;
import com.phaxio.resources.FaxFile;
import com.phaxio.services.Requests;
Expand All @@ -24,13 +25,14 @@ public class FaxFileTest {
public void getsFaxFile () throws IOException {
byte[] fileBytes = Responses.file("/test.pdf");

stubFor(get(urlEqualTo("/v2/faxes/1/file?api_secret=SECRET&api_key=KEY"))
stubFor(get(urlEqualTo("/ver/faxes/1/file"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/pdf")
.withBody(fileBytes)));

Requests client = new Requests("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Requests client = new Requests(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

FaxFile file = new FaxFile(1);

Expand All @@ -43,13 +45,14 @@ public void getsFaxFile () throws IOException {
public void getsLargeThumbnail () throws IOException {
byte[] fileBytes = BinaryFixtures.getTestPhaxCode();

stubFor(get(urlEqualTo("/v2/faxes/1/file?thumbnail=l&api_secret=SECRET&api_key=KEY"))
stubFor(get(urlEqualTo("/ver/faxes/1/file?thumbnail=l"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/octet")
.withBody(fileBytes)));

Requests client = new Requests("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Requests client = new Requests(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

FaxFile file = new FaxFile(1);

Expand All @@ -62,13 +65,14 @@ public void getsLargeThumbnail () throws IOException {
public void getsSmallThumbnail () throws IOException {
byte[] fileBytes = BinaryFixtures.getTestPhaxCode();

stubFor(get(urlEqualTo("/v2/faxes/1/file?thumbnail=s&api_secret=SECRET&api_key=KEY"))
stubFor(get(urlEqualTo("/ver/faxes/1/file?thumbnail=s"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/octet")
.withBody(fileBytes)));

Requests client = new Requests("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Requests client = new Requests(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

FaxFile file = new FaxFile(1);

Expand All @@ -81,18 +85,20 @@ public void getsSmallThumbnail () throws IOException {
public void deletesFax () throws IOException {
String json = Responses.json("/generic_success.json");

stubFor(delete(urlEqualTo("/v2/faxes/1/file?api_secret=SECRET&api_key=KEY"))
stubFor(delete(urlEqualTo("/ver/faxes/1/file"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBody(json)));

Requests client = new Requests("KEY", "SECRET", "http://localhost:%s/v2/", TEST_PORT);
Requests client = new Requests(Auth.VALID_KEY, Auth.VALID_SECRET, "http://localhost:%s/ver/", TEST_PORT);

FaxFile file = new FaxFile(1);
file.setClient(client);
file.delete();

verify(deleteRequestedFor(urlEqualTo("/v2/faxes/1/file?api_secret=SECRET&api_key=KEY")));
verify(deleteRequestedFor(urlEqualTo("/ver/faxes/1/file"))
.withHeader("Authorization", Auth.VALID_AUTH_MATCHER));
}
}
Loading