Skip to content

Commit c724afc

Browse files
authored
[Feature] Add library hooks (#277)
1 parent 83c0312 commit c724afc

File tree

15 files changed

+501
-7
lines changed

15 files changed

+501
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next release
4+
5+
- Adds new `RequestHook` and `ResponseHook` classes. (un)subscribe to them with the new `subscribeToRequestHook`, `subscribeToResponseHook`, `unsubscribeFromRequestHook`, or `unsubscribeFromResponseHook` methods of an `EasyPostClient`
6+
37
## v6.7.0 (2023-06-06)
48

59
- Retrieving carrier metadata is now generally available via `client.carrierMetadata.retrieve`

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ test:
6262
## update-examples-submodule - Update the examples submodule
6363
update-examples-submodule:
6464
git submodule init
65-
git submodule update
65+
git submodule update --remote
6666

6767
.PHONY: help build clean coverage docs install-styleguide install lint publish publish-dry release scan scan-strict test update-examples-submodule

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ public class CreateShipment {
8989
}
9090
```
9191

92+
## HTTP Hooks
93+
94+
Users can subscribe to HTTP requests and responses via the `RequestHook` and `ResponseHook` objects. To do so, pass a function to the `subscribeToRequestHook` or `subscribeToResponseHook` methods of an `EasyPostClient` object:
95+
96+
```java
97+
EasyPostClient client = new EasyPostClient(System.getenv("EASYPOST_API_KEY"));
98+
99+
// For request hook custom function, make sure you have RequestHookResponses in the parameter
100+
public static Object requestHookFunction(RequestHookResponses datas) {
101+
// Pass your code here, the information about the request/response is available within the datas parameter.
102+
return true;
103+
}
104+
105+
client.subscribeToRequestHook(requestHookFunction); // subscribe to request hook by passing your custom function
106+
client.unsubscribeToRequestHook(requestHookFunction); // unsubscribe from request hook
107+
108+
// For response hook custom function, make sure you have ResponseHookResponses in the parameter
109+
public static Object responseHookFunction(ResponseHookResponses datas) {
110+
// Pass your code here, the information about the request/response is available within the datas parameter.
111+
return true;
112+
}
113+
114+
client.subscribeToRequestHook(responseHookFunction); // subscribe to response hook by passing your custom function
115+
client.unsubscribeToRequestHook(responseHookFunction); // unsubscribe from response hook
116+
```
117+
92118
## Documentation
93119

94120
API documentation can be found at: <https://easypost.com/docs/api>.

dependency-check-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<vulnerabilityName>CVE-2022-3171</vulnerabilityName>
99
<vulnerabilityName>CVE-2022-3509</vulnerabilityName>
1010
<vulnerabilityName>CVE-2022-3510</vulnerabilityName>
11+
<vulnerabilityName>CVE-2023-2976</vulnerabilityName>
1112
</suppress>
1213
</suppressions>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.easypost.hooks;
2+
3+
import java.util.function.Function;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class RequestHook {
8+
private List<Function<RequestHookResponses, Object>> eventHandlers = new ArrayList<>();
9+
10+
/**
11+
* Add a function to the list of event handlers.
12+
* @param handler The event handler function to be added.
13+
*/
14+
public void addEventHandler(Function<RequestHookResponses, Object> handler) {
15+
eventHandlers.add(handler);
16+
}
17+
18+
/**
19+
* Remove a function to the list of event handlers.
20+
* @param handler The event handler function to be removed.
21+
*/
22+
public void removeEventHandler(Function<RequestHookResponses, Object> handler) {
23+
eventHandlers.remove(handler);
24+
}
25+
26+
/**
27+
* Execute all the functions from the event handlers.
28+
* @param datas The datas from the hooks.
29+
*/
30+
public void executeEventHandler(RequestHookResponses datas) {
31+
for (Function<RequestHookResponses, Object> eventHandler : eventHandlers) {
32+
Object result = eventHandler.apply(datas);
33+
}
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.easypost.hooks;
2+
3+
import java.util.Map;
4+
5+
import com.google.gson.JsonObject;
6+
7+
import lombok.Getter;
8+
import lombok.Setter;
9+
10+
@Setter @Getter
11+
public class RequestHookResponses {
12+
private Map<String, String> headers;
13+
private String method;
14+
private String path;
15+
private JsonObject requestBody;
16+
private String requestTimestamp;
17+
private String requestUuid;
18+
19+
/**
20+
* RequestHookResponses constructor.
21+
*
22+
* @param headers The headers of the request.
23+
* @param method The HTTP method of the request.
24+
* @param path The path of the request.
25+
* @param requestBody The JSON object representing the request body.
26+
* @param requestTimestamp The timestamp of the request.
27+
* @param requestUuid The UUID of the request.
28+
*/
29+
public RequestHookResponses(Map<String, String> headers, String method, String path, JsonObject requestBody,
30+
String requestTimestamp, String requestUuid) {
31+
this.headers = headers;
32+
this.method = method;
33+
this.path = path;
34+
this.requestBody = requestBody;
35+
this.requestTimestamp = requestTimestamp;
36+
this.requestUuid = requestUuid;
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.easypost.hooks;
2+
3+
import java.util.function.Function;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class ResponseHook {
8+
private List<Function<ResponseHookResponses, Object>> eventHandlers = new ArrayList<>();
9+
10+
/**
11+
* Add a function to the list of event handlers.
12+
* @param handler The event handler function to be added.
13+
*/
14+
public void addEventHandler(Function<ResponseHookResponses, Object> handler) {
15+
eventHandlers.add(handler);
16+
}
17+
18+
/**
19+
* Remove a function to the list of event handlers.
20+
* @param handler The event handler function to be removed.
21+
*/
22+
public void removeEventHandler(Function<ResponseHookResponses, Object> handler) {
23+
eventHandlers.remove(handler);
24+
}
25+
26+
/**
27+
* Execute all the functions from the event handlers.
28+
* @param datas The datas from the hooks.
29+
*/
30+
public void executeEventHandler(ResponseHookResponses datas) {
31+
for (Function<ResponseHookResponses, Object> eventHandler : eventHandlers) {
32+
Object result = eventHandler.apply(datas);
33+
}
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.easypost.hooks;
2+
3+
import java.util.Map;
4+
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
@Setter @Getter
9+
public class ResponseHookResponses {
10+
private int httpStatus;
11+
private Map<String, String> headers;
12+
private String method;
13+
private String path;
14+
private String responseBody;
15+
private String responseTimestamp;
16+
private String requestTimestamp;
17+
private String requestUuid;
18+
19+
/**
20+
* ResponseHookResponses constructor.
21+
*
22+
* @param httpStatus The HTTP status code of the response.
23+
* @param headers The headers of the response.
24+
* @param method The HTTP method of the request.
25+
* @param path The path of the request.
26+
* @param responseBody The response body as a string.
27+
* @param responseTimestamp The timestamp of the response.
28+
* @param requestTimestamp The timestamp of the corresponding request.
29+
* @param requestUuid The UUID of the corresponding request.
30+
*/
31+
public ResponseHookResponses(int httpStatus, Map<String, String> headers, String method, String path,
32+
String responseBody, String responseTimestamp, String requestTimestamp, String requestUuid) {
33+
this.httpStatus = httpStatus;
34+
this.headers = headers;
35+
this.method = method;
36+
this.path = path;
37+
this.responseBody = responseBody;
38+
this.responseTimestamp = responseTimestamp;
39+
this.requestTimestamp = requestTimestamp;
40+
this.requestUuid = requestUuid;
41+
}
42+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Custom hook classes for the EasyPost API.
3+
*
4+
* @author EasyPost developers
5+
* @version 1.0
6+
* @see <a href="https://www.easypost.com/docs">EasyPost API documentation</a>
7+
* @since 1.0
8+
*/
9+
package com.easypost.hooks;

0 commit comments

Comments
 (0)