Skip to content
This repository was archived by the owner on Sep 1, 2025. It is now read-only.

Commit f2c63c9

Browse files
author
Sergi Castellsagué
committed
Add patch method
1 parent dcc0451 commit f2c63c9

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

pom.xml

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

55
<groupId>com.github.stuartapp</groupId>
66
<artifactId>stuart-client-java</artifactId>
7-
<version>1.0.1-SNAPSHOT</version>
7+
<version>1.0.2</version>
88

99
<name>Stuart API Java client</name>
1010
<description>Java library to interact with the Stuart API.</description>

src/main/java/com/stuart/stuartclientjava/infrastructure/HttpClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public ApiResponse performPost(String resource, String body) throws IOException
3737
return new ApiResponse(response.code(), response.body().string());
3838
}
3939

40+
public ApiResponse performPatch(String resource, String body) throws IOException {
41+
Request request = new Request.Builder()
42+
.url(getFullUrl(resource))
43+
.headers(Headers.of(getDefaultHeaders()))
44+
.patch(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body))
45+
.build();
46+
47+
Response response = client.newCall(request).execute();
48+
return new ApiResponse(response.code(), response.body().string());
49+
}
50+
4051
private Map<String, String> getDefaultHeaders() {
4152
Map<String, String> result = new HashMap<String, String>();
4253
result.put("Authorization", String.format("Bearer %s", authenticator.getAccessToken()));

src/test/java/com/stuart/stuartclientjava.infrastructure/HttpClientTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ void shouldSendPostRequestWithCorrectParameters() throws IOException {
6969
assertThat(sink.readUtf8()).isEqualTo("{'some': 'body'}");
7070
}
7171

72+
@Test
73+
void shouldSendPatchRequestWithCorrectParameters() throws IOException {
74+
// when
75+
this.httpClient.performPatch("/sample-endpoint", "{'some': 'body'}");
76+
Request request = this.okHttpClient.getRequest();
77+
78+
// then
79+
assertThat(request.url().toString()).isEqualTo("https://sandbox-api.stuart.com/sample-endpoint");
80+
assertThat(request.method()).isEqualTo("PATCH");
81+
assertThat(request.headers()).isEqualTo(
82+
Headers.of(
83+
"Authorization", "Bearer token",
84+
"User-Agent", String.format("stuart-client-java/%s", new Version().getCurrent()),
85+
"Content-Type", "application/json"
86+
)
87+
);
88+
89+
Buffer sink = new Buffer();
90+
request.body().writeTo(sink);
91+
assertThat(sink.readUtf8()).isEqualTo("{'some': 'body'}");
92+
}
93+
7294
private class HttpClientMock extends HttpClient {
7395

7496
public HttpClientMock(Authenticator authenticator, OkHttpClient client) {

0 commit comments

Comments
 (0)