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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Default ignored files
.idea
.target
target
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ If you need support using AfterShip products, please contact support@aftership.c
- [Endpoints](#endpoints)
- [/trackings](#trackings)
- [/couriers](#couriers)
- [/courier-connections](#courier-connections)
- [/estimated-delivery-date](#estimated-delivery-date)
- [Help](#help)
- [License](#license)
Expand All @@ -41,6 +42,7 @@ Each SDK version is designed to work with a specific API version. Please refer t

| SDK Version | Supported API Version | Branch |
| ----------- | --------------------- | ----------------------------------------------------------- |
| 9.x.x | 2025-04 | https://github.com/AfterShip/tracking-sdk-java/tree/2025-04 |
| 8.x.x | 2025-01 | https://github.com/AfterShip/tracking-sdk-java/tree/2025-01 |
| 7.x.x | 2024-10 | https://github.com/AfterShip/tracking-sdk-java/tree/2024-10 |
| 6.x.x | 2024-07 | https://github.com/AfterShip/tracking-sdk-java/tree/2024-07 |
Expand All @@ -55,7 +57,7 @@ Each SDK version is designed to work with a specific API version. Please refer t
<dependency>
<groupId>com.aftership</groupId>
<artifactId>tracking-sdk</artifactId>
<version>8.0.0</version>
<version>9.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -106,7 +108,7 @@ public class App {

## Rate Limiter

See the [Rate Limit](https://www.aftership.com/docs/tracking/2025-01/quickstart/rate-limit) to understand the AfterShip rate limit policy.
See the [Rate Limit](https://www.aftership.com/docs/tracking/2025-04/quickstart/rate-limit) to understand the AfterShip rate limit policy.

## Error Handling

Expand Down Expand Up @@ -155,6 +157,7 @@ The AfterShip instance has the following properties which are exactly the same a

- courier - Get a list of our supported couriers.
- tracking - Create trackings, update trackings, and get tracking results.
- courier-connection - Create courier connections, update courier connections, and get courier connections results.
- estimated-delivery-date - Get estimated delivery date for your order.

### /trackings
Expand Down Expand Up @@ -235,14 +238,7 @@ System.out.println(response.getTrackingNumber());
**GET** /couriers

```java
GetUserCouriersResponse response = CourierResource.getUserCouriers().fetch();
System.out.println(response.getTotal());
```

**GET** /couriers/all

```java
GetAllCouriersResponse response = CourierResource.getAllCouriers().fetch();
GetUserCouriersResponse response = CourierResource.getCouriers().fetch();
System.out.println(response.getTotal());
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.aftership</groupId>
<artifactId>tracking-sdk</artifactId>
<version>8.0.0</version>
<version>9.0.0</version>

<name>AfterShip Tracking SDK</name>
<description>The official AfterShip Tracking Java API library</description>
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/aftership/constant/ErrorEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public enum ErrorEnum {
"You have exceeded the API call rate limit. The default limit is 10 requests per second."),
INTERNAL_ERROR(500, 500, "Something went wrong on AfterShip's end.");

private final int code;

private final int statusCode;

private final String message;

private static final Map<Integer, ErrorEnum> META = new HashMap<>();

static {
Expand All @@ -72,24 +78,12 @@ public enum ErrorEnum {
}
}

private final int code;
private final int statusCode;
private final String message;

ErrorEnum(int code, int statusCode, String message) {
this.code = code;
this.statusCode = statusCode;
this.message = message;
}

public static int getByMetaCode(int code) {
ErrorEnum e = META.get(code);
if (e == null) {
return BAD_REQUEST.code;
}
return e.code;
}

public int getCode() {
return code;
}
Expand All @@ -102,6 +96,14 @@ public String getMessage() {
return message;
}

public static int getByMetaCode(int code) {
ErrorEnum e = META.get(code);
if (e == null) {
return BAD_REQUEST.code;
}
return e.code;
}

@Override
public String toString() {
return "ErrorEnum{" + "code=" + code + ", message='" + message + '\'' + '}';
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/aftership/courier/CourierResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ public static DetectCourierCreator detectCourier() {
return new DetectCourierCreator();
}

public static GetUserCouriersFetcher getUserCouriers() {
return new GetUserCouriersFetcher();
}

public static GetAllCouriersFetcher getAllCouriers() {
return new GetAllCouriersFetcher();
public static GetCouriersFetcher getCouriers() {
return new GetCouriersFetcher();
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/aftership/courier/DetectCourierCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.aftership.base.Creator;
import com.aftership.http.*;
import com.aftership.http.Request;
import com.aftership.model.DetectCourierRequest;
import com.aftership.model.DetectCourierResponse;
import com.google.gson.Gson;
Expand All @@ -15,7 +16,6 @@

public class DetectCourierCreator extends Creator<DetectCourierResponse> {
private final Map<String, String> headerParams = new HashMap<>(8);
private DetectCourierRequest detectCourierRequest;

public DetectCourierCreator addHeaderParam(final String name, final String value) {
if (value == null || value.equals("null")) {
Expand All @@ -34,14 +34,16 @@ private void setHeaderParams(final Request request) {
}
}

private DetectCourierRequest detectCourierRequest;

public DetectCourierCreator setDetectCourierRequest(DetectCourierRequest detectCourierRequest) {
this.detectCourierRequest = detectCourierRequest;
return this;
}

@Override
public DetectCourierResponse create(AfterShipClient client) throws Exception {
String path = "/tracking/2025-01/couriers/detect";
String path = "/tracking/2025-04/couriers/detect";
Request request = new Request(HttpMethod.POST, path);
request.setBody((new Gson()).toJson(detectCourierRequest));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

import com.aftership.base.Fetcher;
import com.aftership.http.*;
import com.aftership.model.GetAllCouriersResponse;
import com.aftership.http.Request;
import com.aftership.model.GetCouriersResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
import java.util.Map;

public class GetAllCouriersFetcher extends Fetcher<GetAllCouriersResponse> {
public class GetCouriersFetcher extends Fetcher<GetCouriersResponse> {
private final Map<String, String> headerParams = new HashMap<>(8);

public GetAllCouriersFetcher addHeaderParam(final String name, final String value) {
public GetCouriersFetcher addHeaderParam(final String name, final String value) {
if (value == null || value.equals("null")) {
return this;
}
Expand All @@ -32,17 +33,41 @@ private void setHeaderParams(final Request request) {
}
}

private Boolean active;

private String slug;

public GetCouriersFetcher setActive(Boolean active) {
this.active = active;
return this;
}

public GetCouriersFetcher setSlug(String slug) {
this.slug = slug;
return this;
}

@Override
public GetAllCouriersResponse fetch(AfterShipClient client) throws Exception {
String path = "/tracking/2025-01/couriers/all";
public GetCouriersResponse fetch(AfterShipClient client) throws Exception {
String path = "/tracking/2025-04/couriers";
Request request = new Request(HttpMethod.GET, path);
addQueryParams(request);
setHeaderParams(request);
Response response = client.request(request);
AfterShipResponse<GetAllCouriersResponse> trackingResponse =
AfterShipResponse<GetCouriersResponse> trackingResponse =
new Gson()
.fromJson(
response.getContent(),
new TypeToken<AfterShipResponse<GetAllCouriersResponse>>() {}.getType());
new TypeToken<AfterShipResponse<GetCouriersResponse>>() {}.getType());
return trackingResponse.getData();
}

private void addQueryParams(final Request request) {
if (active != null) {
request.addQueryParam("active", String.valueOf(active));
}
if (slug != null) {
request.addQueryParam("slug", slug);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This code was auto generated by AfterShip SDK Generator.
* Do not edit the class manually.
*/
package com.aftership.courier_connection;

import com.aftership.base.Resource;

public class CourierConnectionResource extends Resource {
public static GetCourierConnectionsByIdFetcher getCourierConnectionsById() {
return new GetCourierConnectionsByIdFetcher();
}

public static GetCourierConnectionsReader getCourierConnections() {
return new GetCourierConnectionsReader();
}

public static PutCourierConnectionsByIdUpdater putCourierConnectionsById() {
return new PutCourierConnectionsByIdUpdater();
}

public static DeleteCourierConnectionsByIdDeleter deleteCourierConnectionsById() {
return new DeleteCourierConnectionsByIdDeleter();
}

public static PostCourierConnectionsCreator postCourierConnections() {
return new PostCourierConnectionsCreator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This code was auto generated by AfterShip SDK Generator.
* Do not edit the class manually.
*/
package com.aftership.courier_connection;

import com.aftership.base.Deleter;
import com.aftership.constant.ErrorEnum;
import com.aftership.exception.ApiException;
import com.aftership.http.*;
import com.aftership.http.Request;
import com.aftership.model.CourierConnection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
import java.util.Map;

public class DeleteCourierConnectionsByIdDeleter extends Deleter<CourierConnection> {
private final Map<String, String> headerParams = new HashMap<>(8);

public DeleteCourierConnectionsByIdDeleter addHeaderParam(final String name, final String value) {
if (value == null || value.equals("null")) {
return this;
}

if (!headerParams.containsKey(name)) {
headerParams.put(name, value);
}
return this;
}

private void setHeaderParams(final Request request) {
for (final Map.Entry<String, String> entry : headerParams.entrySet()) {
request.addHeaderParam(entry.getKey(), entry.getValue());
}
}

private String id;

public DeleteCourierConnectionsByIdDeleter setId(String id) {
this.id = id;
return this;
}

@Override
public CourierConnection delete(AfterShipClient client) throws Exception {
if (id == null || id.isEmpty()) {
throw new ApiException(
ErrorEnum.BAD_REQUEST.getCode(),
ErrorEnum.BAD_REQUEST.getMessage() + ": `id` is invalid");
}
String path = String.format("/tracking/2025-04/courier-connections/%s", id);
Request request = new Request(HttpMethod.DELETE, path);
setHeaderParams(request);
Response response = client.request(request);
AfterShipResponse<CourierConnection> trackingResponse =
new Gson()
.fromJson(
response.getContent(),
new TypeToken<AfterShipResponse<CourierConnection>>() {}.getType());
return trackingResponse.getData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
* This code was auto generated by AfterShip SDK Generator.
* Do not edit the class manually.
*/
package com.aftership.courier;
package com.aftership.courier_connection;

import com.aftership.base.Fetcher;
import com.aftership.constant.ErrorEnum;
import com.aftership.exception.ApiException;
import com.aftership.http.*;
import com.aftership.model.GetUserCouriersResponse;
import com.aftership.http.Request;
import com.aftership.model.CourierConnection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
import java.util.Map;

public class GetUserCouriersFetcher extends Fetcher<GetUserCouriersResponse> {
public class GetCourierConnectionsByIdFetcher extends Fetcher<CourierConnection> {
private final Map<String, String> headerParams = new HashMap<>(8);

public GetUserCouriersFetcher addHeaderParam(final String name, final String value) {
public GetCourierConnectionsByIdFetcher addHeaderParam(final String name, final String value) {
if (value == null || value.equals("null")) {
return this;
}
Expand All @@ -32,17 +35,29 @@ private void setHeaderParams(final Request request) {
}
}

private String id;

public GetCourierConnectionsByIdFetcher setId(String id) {
this.id = id;
return this;
}

@Override
public GetUserCouriersResponse fetch(AfterShipClient client) throws Exception {
String path = "/tracking/2025-01/couriers";
public CourierConnection fetch(AfterShipClient client) throws Exception {
if (id == null || id.isEmpty()) {
throw new ApiException(
ErrorEnum.BAD_REQUEST.getCode(),
ErrorEnum.BAD_REQUEST.getMessage() + ": `id` is invalid");
}
String path = String.format("/tracking/2025-04/courier-connections/%s", id);
Request request = new Request(HttpMethod.GET, path);
setHeaderParams(request);
Response response = client.request(request);
AfterShipResponse<GetUserCouriersResponse> trackingResponse =
AfterShipResponse<CourierConnection> trackingResponse =
new Gson()
.fromJson(
response.getContent(),
new TypeToken<AfterShipResponse<GetUserCouriersResponse>>() {}.getType());
new TypeToken<AfterShipResponse<CourierConnection>>() {}.getType());
return trackingResponse.getData();
}
}
Loading