Skip to content

Commit bcf9033

Browse files
fix: connection reset error (#6)
1 parent 5134e87 commit bcf9033

File tree

3 files changed

+70
-54
lines changed

3 files changed

+70
-54
lines changed

README.md

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ If you need support using AfterShip products, please contact [email protected]
1919
- [Error Handling](#error-handling)
2020
- [Error List](#error-list)
2121
- [Endpoints](#endpoints)
22-
- [/address-validations](#address-validations)
22+
- [/locations](#locations)
2323
- [/rates](#rates)
2424
- [/shipper-accounts](#shipper-accounts)
2525
- [/labels](#labels)
2626
- [/couriers](#couriers)
2727
- [/cancel-labels](#cancel-labels)
28-
- [/cancel-pickups](#cancel-pickups)
29-
- [/locations](#locations)
28+
- [/address-validations](#address-validations)
3029
- [/manifests](#manifests)
3130
- [/pickups](#pickups)
31+
- [/cancel-pickups](#cancel-pickups)
3232
- [Help](#help)
3333
- [License](#license)
3434

@@ -48,7 +48,7 @@ Before you begin to integrate:
4848
<dependency>
4949
<groupId>com.aftership</groupId>
5050
<artifactId>shipping-sdk</artifactId>
51-
<version>3.0.0</version>
51+
<version>3.0.1</version>
5252
</dependency>
5353
```
5454

@@ -161,8 +161,8 @@ The SDK will return an error object when there is any error during the request,
161161

162162
The AfterShip SDK has the following resource which are exactly the same as the API endpoints:
163163

164-
- AddressValidationsBetaResource
165-
- Create an address validation
164+
- LocationsResource
165+
- Get locations
166166
- RatesResource
167167
- Get rates
168168
- Calculate rates
@@ -185,12 +185,8 @@ The AfterShip SDK has the following resource which are exactly the same as the A
185185
- Get the cancelled labels
186186
- Cancel a label
187187
- Get a cancelled label
188-
- CancelPickupsResource
189-
- Get the cancelled pickups
190-
- Cancel a pickup
191-
- Get a cancelled pickup
192-
- LocationsResource
193-
- Get locations
188+
- AddressValidationsBetaResource
189+
- Create an address validation
194190
- ManifestsResource
195191
- Get manifests
196192
- Create a manifest
@@ -199,15 +195,17 @@ The AfterShip SDK has the following resource which are exactly the same as the A
199195
- Get pickups
200196
- Create a pickup
201197
- Get a pickup
198+
- CancelPickupsResource
199+
- Get the cancelled pickups
200+
- Cancel a pickup
201+
- Get a cancelled pickup
202202

203-
### /address-validations
204-
**POST** /address-validations
203+
### /locations
204+
**GET** /locations
205205

206206
```java
207-
PostAddressValidationsRequest request = new PostAddressValidationsRequest();
208-
PostAddressValidationsResponse response = AddressValidationsBetaResource.postAddressValidations()
209-
.setPostAddressValidationsRequest(request)
210-
.create();
207+
GetLocationsResponse response = LocationsResource.getLocations()
208+
.fetch();
211209
System.out.println(response.getData());
212210
```
213211

@@ -374,45 +372,17 @@ The AfterShip SDK has the following resource which are exactly the same as the A
374372
System.out.println(response.getData());
375373
```
376374

377-
### /cancel-pickups
378-
**GET** /cancel-pickups
379-
380-
```java
381-
GetCancelPickupsResponse response = CancelPickupsResource.getCancelPickups()
382-
.fetch();
383-
System.out.println(response.getData());
384-
```
385-
386-
**POST** /cancel-pickups
375+
### /address-validations
376+
**POST** /address-validations
387377

388378
```java
389-
PostCancelPickupsRequest request = new PostCancelPickupsRequest();
390-
PostCancelPickupsRequestPickup pickup = new PostCancelPickupsRequestPickup();
391-
request.setPickup(pickup);
392-
PostCancelPickupsResponse response = CancelPickupsResource.postCancelPickups()
393-
.setPostCancelPickupsRequest(request)
379+
PostAddressValidationsRequest request = new PostAddressValidationsRequest();
380+
PostAddressValidationsResponse response = AddressValidationsBetaResource.postAddressValidations()
381+
.setPostAddressValidationsRequest(request)
394382
.create();
395383
System.out.println(response.getData());
396384
```
397385

398-
**GET** /cancel-pickups/{id}
399-
400-
```java
401-
GetCancelPickupResponse response = CancelPickupsResource.getCancelPickup()
402-
.setId("valid_value")
403-
.fetch();
404-
System.out.println(response.getData());
405-
```
406-
407-
### /locations
408-
**GET** /locations
409-
410-
```java
411-
GetLocationsResponse response = LocationsResource.getLocations()
412-
.fetch();
413-
System.out.println(response.getData());
414-
```
415-
416386
### /manifests
417387
**GET** /manifests
418388

@@ -473,6 +443,36 @@ The AfterShip SDK has the following resource which are exactly the same as the A
473443
System.out.println(response.getData());
474444
```
475445

446+
### /cancel-pickups
447+
**GET** /cancel-pickups
448+
449+
```java
450+
GetCancelPickupsResponse response = CancelPickupsResource.getCancelPickups()
451+
.fetch();
452+
System.out.println(response.getData());
453+
```
454+
455+
**POST** /cancel-pickups
456+
457+
```java
458+
PostCancelPickupsRequest request = new PostCancelPickupsRequest();
459+
PostCancelPickupsRequestPickup pickup = new PostCancelPickupsRequestPickup();
460+
request.setPickup(pickup);
461+
PostCancelPickupsResponse response = CancelPickupsResource.postCancelPickups()
462+
.setPostCancelPickupsRequest(request)
463+
.create();
464+
System.out.println(response.getData());
465+
```
466+
467+
**GET** /cancel-pickups/{id}
468+
469+
```java
470+
GetCancelPickupResponse response = CancelPickupsResource.getCancelPickup()
471+
.setId("valid_value")
472+
.fetch();
473+
System.out.println(response.getData());
474+
```
475+
476476

477477
## Help
478478

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.aftership</groupId>
88
<artifactId>shipping-sdk</artifactId>
9-
<version>3.0.0</version>
9+
<version>3.0.1</version>
1010

1111
<name>AfterShip Shipping SDK</name>
1212
<description>The official AfterShip Shipping Java API library</description>

src/main/java/com/aftership/shipping/http/HttpClient.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
import org.apache.http.message.BasicHeader;
2020
import org.apache.http.util.EntityUtils;
2121

22+
import java.net.SocketException;
2223
import java.net.SocketTimeoutException;
2324
import java.net.URI;
2425
import java.nio.charset.StandardCharsets;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.Map;
29+
import java.util.concurrent.TimeUnit;
2830

2931
public class HttpClient {
3032

31-
private static final String DEFAULT_USER_AGENT = "shipping-sdk-java/3.0.0 (https://www.aftership.com) apache-httpclient/4.5.14";
33+
private static final String DEFAULT_USER_AGENT = "shipping-sdk-java/3.0.1 (https://www.aftership.com) apache-httpclient/4.5.14";
3234
protected final org.apache.http.client.HttpClient client;
3335

3436
public HttpClient(final RequestConfig requestConfig, String userAgent) {
@@ -46,6 +48,9 @@ public HttpClient(final RequestConfig requestConfig, String userAgent) {
4648
client = clientBuilder
4749
.setDefaultHeaders(headers)
4850
.setDefaultRequestConfig(requestConfig)
51+
.setMaxConnPerRoute(20)
52+
.setMaxConnTotal(100)
53+
.setConnectionTimeToLive(300, TimeUnit.SECONDS).evictExpiredConnections()
4954
.build();
5055
}
5156

@@ -66,6 +71,17 @@ public Response request(final Request request, int retries) throws Exception {
6671
"Request timed out."
6772
);
6873
}
74+
} catch (SocketException e) {
75+
if (e.getMessage().equals("Connection reset")) {
76+
if (i > retries) {
77+
throw new ApiException(
78+
ErrorEnum.TIMED_OUT.name(),
79+
"Request timed out."
80+
);
81+
}
82+
} else {
83+
throw e;
84+
}
6985
} catch (Exception e) {
7086
throw e;
7187
}
@@ -115,7 +131,7 @@ private boolean shouldRetry(Response response) {
115131

116132
private int delay(int retryAttempt) {
117133
int delayBase = 3;
118-
int delay = delayBase * (2 ^ (retryAttempt - 1));
134+
int delay = delayBase * (1 << (retryAttempt - 1));
119135
double jitter = delay * (Math.random() - 0.5);
120136
return (int) (Math.max(1, delay + jitter) * 1000);
121137
}

0 commit comments

Comments
 (0)