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

implement validations and exceptions #4

Merged
merged 34 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3c85c27
W.I.P.
pierce-h Aug 10, 2021
6bca9d2
W.I.P.
pierce-h Aug 10, 2021
c0164c4
W.I.P.
pierce-h Aug 10, 2021
e9fb98d
Updated exception classes and defined their shape
KaseyCantu Aug 25, 2021
7783661
Updated exception classes and defined their shape
KaseyCantu Aug 25, 2021
ebbef9d
Added lib to handle json serialization and client in progress.
KaseyCantu Aug 26, 2021
420e8cf
work in progress - fixed client bugs and now working on services
KaseyCantu Aug 27, 2021
8f06195
work in progress - client wrap up and services implementation
KaseyCantu Aug 30, 2021
25e6c9f
work in progress - fixed type bugs in client, got listCarriers workin…
KaseyCantu Aug 30, 2021
3e7771c
work in progress
KaseyCantu Aug 30, 2021
b32e27c
work in progress.. issue serializing java array to json
KaseyCantu Aug 31, 2021
d97a0dc
Fixed typing bugs and added to test for services - address validation…
KaseyCantu Aug 31, 2021
69d764f
Added validations for apiKey value in Config
KaseyCantu Aug 31, 2021
6bdbdce
Added constants interface for reusing highly repeated values.
KaseyCantu Aug 31, 2021
01ecd64
Reformatted for readability
KaseyCantu Aug 31, 2021
6399066
Housekeeping
KaseyCantu Aug 31, 2021
4708a24
Housekeeping
KaseyCantu Aug 31, 2021
c9c8d4c
satisfy linter and compiler
KaseyCantu Aug 31, 2021
c9773a3
Added docstrings
KaseyCantu Aug 31, 2021
f0027ac
Updated exception throwing/handling and added a test dep
KaseyCantu Sep 1, 2021
58154e9
Updated CI to install deps before linting - CI was previously crashin…
KaseyCantu Sep 1, 2021
b269281
Merge pull request #5 from ShipEngine/kc/http-client-implementation
KaseyCantu Sep 1, 2021
36c91c2
debugging CI
KaseyCantu Sep 1, 2021
80e2f67
debugging CI
KaseyCantu Sep 1, 2021
a3bde79
debugging CI
KaseyCantu Sep 1, 2021
ef610d3
debugging CI
KaseyCantu Sep 1, 2021
98682bc
Completed services in SDK
KaseyCantu Sep 1, 2021
7d9daa4
Completed services in SDK
KaseyCantu Sep 1, 2021
0bcaaa4
work in progress - mocking in tests is working on to rest of tests
KaseyCantu Sep 2, 2021
306ed03
work in progress - adjusted Config class and working on mocks for tests
KaseyCantu Sep 3, 2021
785ae57
Added the rest of the http mocks to test the services in the SDK
KaseyCantu Sep 8, 2021
4e37993
Added a method to check the response for headers and retry after on 4…
KaseyCantu Sep 9, 2021
11c6154
Updates per review
KaseyCantu Sep 10, 2021
55ba7d8
Merge pull request #6 from ShipEngine/kc/implement-services-with-tests
KaseyCantu Sep 10, 2021
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: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
109 changes: 78 additions & 31 deletions src/main/java/com/shipengine/Config.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
package com.shipengine;

import java.util.HashMap;

public class Config {
private String apiKey;
private String baseUrl = "https://api.shipengine.com/";
private int pageSize = 5000;
private int retries = 1;
private int timeout = 50;

public Config(String apiKey) {
setApiKey(apiKey);
}

public Config(String apiKey, int timeout, int retries, int pageSize) {
setApiKey(apiKey);
setTimeout(timeout);
setRetries(retries);
setPageSize(pageSize);
}

/*
* The URL of the ShipEngine API. You can usually leave this unset and it will
* default to our public API.
*/
public String getBaseUrl() {
return baseUrl;
}

/*
* Your ShipEngine API key. This can be a production or sandbox key. Sandbox
* keys start with "TEST_".
*/
String apiKey;
public String getApiKey() {
return apiKey;
}

/*
* The URL of the ShipEngine API. You can usually leave this unset and it will
* default to our public API.
* Set the ShipEngine API key.
*/
String baseUrl = "https://api.shipengine.com/";
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

/*
* Some ShipEngine API endpoints return paged data. This lets you control the
* number of items returned per request. Larger numbers will use more memory but
* will require fewer HTTP requests.
* The maximum amount of time (in milliseconds) to wait for a response from the
* ShipEngine server.
*
* Defaults to 50.
* Defaults to 5000 (5 seconds).
*/
int pageSize;
public int getTimeout() {
return timeout;
}

/*
* Set the timeout (in milliseconds).
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
}

/*
* If the ShipEngine client receives a rate limit error it can automatically
Expand All @@ -30,40 +68,49 @@ public class Config {
* Defaults to 1, which means up to 2 attempts will be made (the original
* attempt, plus one retry).
*/
int retries;
public int getRetries() {
return retries;
}

/*
* The maximum amount of time (in milliseconds) to wait for a response from the
* ShipEngine server.
*
* Defaults to 5000 (5 seconds).
* Set the retries.
*/
int timeout;

public Config(String apiKey) {
this.apiKey = apiKey;
this.timeout = 5000;
this.retries = 1;
this.pageSize = 50;
public void setRetries(int retries) {
this.retries = retries;
}

public String getBaseUrl() {
return baseUrl;
/*
* Some ShipEngine API endpoints return paged data. This lets you control the
* number of items returned per request. Larger numbers will use more memory but
* will require fewer HTTP requests.
*
* Defaults to 50.
*/
public int getPageSize() {
return pageSize;
}

public String getApiKey() {
return apiKey;
/*
* Set the page size.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTimeout() {
return timeout;
public Config merge() {
return this;
}

public int getRetries() {
return retries;
public Config merge(String apiKey) {
return new Config(apiKey);
}

public int getPageSize() {
return pageSize;
public Config merge(String apiKey, int timeout, int retries, int pageSize) {
return new Config(
apiKey,
timeout,
retries,
pageSize
);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/shipengine/InternalClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.shipengine;

import java.util.HashMap;
import java.util.Map;

public class InternalClient {
public HashMap requestLoop(
String httpMethod,
String endpoint,
String body,
Config config
) {
return new HashMap();
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/shipengine/ShipEngine.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.shipengine;

public class ShipEngine {
Config config;
private Config config;

public ShipEngine(String apiKey) {
this.config = new Config(apiKey);
}

public ShipEngine(String apiKey, int timeout, int retries, int pageSize) {
this.config = new Config(apiKey, timeout, retries, pageSize);
}

public Config getConfig() {
return config;
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/shipengine/exception/ClientTimeoutError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.shipengine.exception;

import java.net.MalformedURLException;
import java.net.URL;

public class ClientTimeoutError extends ShipEngineException {

int retryAfter;

ErrorSource source;

String requestID;

public ClientTimeoutError(String requestID, ErrorSource source, int retryAfter) throws MalformedURLException {
KaseyCantu marked this conversation as resolved.
Show resolved Hide resolved
super(
String.format("The request took longer than the %s seconds allowed.", retryAfter),
requestID,
source,
ErrorType.SYSTEM,
ErrorCode.TIMEOUT,
new URL("https://www.shipengine.com/docs/rate-limits")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.shipengine.exception;

/**
* This error occurs when a required field has not been set. This includes fields
* that are conditionally required.
*/
public class FieldValueRequiredException extends RuntimeException {
/**
* The name of the invalid field.
*/
String fieldName;

public FieldValueRequiredException(
String fieldName
) {
super(String.format("%s is a required field.", fieldName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.shipengine.exception;

import java.net.MalformedURLException;
import java.net.URL;

/**
* This error occurs when a field has been set to an invalid value.
*/
public class InvalidFieldValueException extends ShipEngineException {
/**
* The name of the invalid field.
*/
String fieldName;

/**
* The value of the invalid field.
*/
String fieldValue;

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public String getFieldValue() {
return fieldValue;
}

public void setFieldValue(String fieldValue) {
KaseyCantu marked this conversation as resolved.
Show resolved Hide resolved
this.fieldValue = fieldValue;
}

public InvalidFieldValueException(
String fieldName,
String fieldValue
) throws MalformedURLException {
KaseyCantu marked this conversation as resolved.
Show resolved Hide resolved
super(
String.format("%s - %s was provided.", fieldName, fieldValue),
"",
ErrorSource.SHIPENGINE,
ErrorType.VALIDATION,
ErrorCode.INVALID_FIELD_VALUE,
new URL("https://www.shipengine.com/docs/")
);
this.setFieldName(fieldName);
this.setFieldValue(fieldValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.shipengine.exception;

import java.net.MalformedURLException;
import java.net.URL;

/**
* This error occurs when a request to ShipEngine API is blocked due to the rate
* limit being exceeded.
*/
public class RateLimitExceededException extends ShipEngineException {
/**
* The amount of time (in milliseconds) to wait before retrying the request.
*/
private int retryAfter;

public int getRetryAfter() {
return retryAfter;
}

public void setRetryAfter(int retryAfter) {
KaseyCantu marked this conversation as resolved.
Show resolved Hide resolved
this.retryAfter = retryAfter;
}

public RateLimitExceededException(
String requestID, ErrorSource source, int retryAfter
) throws MalformedURLException {
KaseyCantu marked this conversation as resolved.
Show resolved Hide resolved
super(
"You have exceeded the rate limit.",
requestID,
source,
ErrorType.SYSTEM,
ErrorCode.RATE_LIMIT_EXCEEDED,
new URL("https://www.shipengine.com/docs/rate-limits")
);
this.setRetryAfter(retryAfter);
}
}
Loading