Skip to content

Commit 9da3b88

Browse files
authored
BREAKING CHANGE: Remove shaded dependencies and use HttpClient interface (#219)
* BREAKING CHANGE: Remove shaded dependencies * feat: Use HttpClient interface * test: Fix tests * fix: Fix compile errors * fix: Fix all tests * fix: Fix NPE * test: Add more tests * test: Add more tests and doc comments * test: More tests and minor refactoring
1 parent 13c7a91 commit 9da3b88

34 files changed

+2400
-1510
lines changed

pom.xml

+1-35
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<artifactId>github-client</artifactId>
6-
<version>0.3.12-SNAPSHOT</version>
6+
<version>0.4.0-SNAPSHOT</version>
77

88
<parent>
99
<groupId>com.spotify</groupId>
@@ -86,8 +86,6 @@
8686
<opencensus.version>0.31.1</opencensus.version>
8787
<okhttp.version>4.11.0</okhttp.version>
8888
<opentelemetry.version>1.42.1</opentelemetry.version>
89-
90-
<shade.id>${project.groupId}.githubclient.shade</shade.id>
9189
</properties>
9290

9391
<dependencyManagement>
@@ -483,38 +481,6 @@
483481
</sourceDirectories>
484482
</configuration>
485483
</plugin>
486-
<plugin>
487-
<groupId>org.apache.maven.plugins</groupId>
488-
<artifactId>maven-shade-plugin</artifactId>
489-
<version>3.2.3</version>
490-
<executions>
491-
<execution>
492-
<phase>package</phase>
493-
<goals>
494-
<goal>shade</goal>
495-
</goals>
496-
<configuration>
497-
<artifactSet>
498-
<includes>
499-
<include>${project.groupId}:${project.artifactId}</include>
500-
<include>com.squareup.okhttp3</include>
501-
<include>com.squareup.okio</include>
502-
</includes>
503-
</artifactSet>
504-
<relocations>
505-
<relocation>
506-
<pattern>okhttp3</pattern>
507-
<shadedPattern>${shade.id}.okhttp3</shadedPattern>
508-
</relocation>
509-
<relocation>
510-
<pattern>okio</pattern>
511-
<shadedPattern>${shade.id}.okio</shadedPattern>
512-
</relocation>
513-
</relocations>
514-
</configuration>
515-
</execution>
516-
</executions>
517-
</plugin>
518484
<plugin>
519485
<groupId>org.apache.maven.plugins</groupId>
520486
<artifactId>maven-javadoc-plugin</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
/** BaseHttpResponse is the base implementation of HttpResponse. */
27+
public abstract class BaseHttpResponse implements HttpResponse {
28+
private static final int HTTP_OK = 200;
29+
private static final int HTTP_BAD_REQUEST = 400;
30+
31+
protected final HttpRequest request;
32+
protected final int statusCode;
33+
protected final String statusMessage;
34+
protected final Map<String, List<String>> headers;
35+
36+
public BaseHttpResponse(
37+
final HttpRequest request,
38+
final int statusCode,
39+
final String statusMessage,
40+
final Map<String, List<String>> headers) {
41+
this.request = request;
42+
this.statusCode = statusCode;
43+
this.statusMessage = statusMessage;
44+
this.headers = headers;
45+
}
46+
47+
/**
48+
* Returns the request that generated this response.
49+
*
50+
* @return HttpRequest the request that generated this response
51+
*/
52+
@Override
53+
public HttpRequest request() {
54+
return request;
55+
}
56+
57+
/**
58+
* Returns the HTTP status code of the response.
59+
*
60+
* @return the status code of the response
61+
*/
62+
@Override
63+
public int statusCode() {
64+
return statusCode;
65+
}
66+
67+
/**
68+
* Returns the HTTP status message of the response.
69+
*
70+
* @return the status message of the response
71+
*/
72+
@Override
73+
public String statusMessage() {
74+
return statusMessage;
75+
}
76+
77+
/**
78+
* Returns the headers of the response.
79+
*
80+
* @return the headers of the response as a Map of strings
81+
*/
82+
@Override
83+
public Map<String, List<String>> headers() {
84+
return this.headers;
85+
}
86+
87+
/**
88+
* Returns the values of the header with the given name. If the header is not present, this method
89+
* returns null.
90+
*
91+
* @param headerName the name of the header
92+
* @return the values of the header with the given name as a List of strings, or null if the
93+
* header is not present
94+
*/
95+
@Override
96+
public List<String> headers(final String headerName) {
97+
return this.headers.get(headerName);
98+
}
99+
100+
/**
101+
* Returns the first value of the header with the given name. If the header is not present, this
102+
* method returns null.
103+
*
104+
* @param headerName the name of the header
105+
* @return the first value of the header with the given name, or null if the header is not present
106+
*/
107+
@Override
108+
public String header(final String headerName) {
109+
List<String> headerValues = this.headers(headerName);
110+
if (headerValues == null || headerValues.isEmpty()) {
111+
return null;
112+
}
113+
if (headerValues.size() == 1) {
114+
return headerValues.get(0);
115+
} else {
116+
return String.join(",", headerValues);
117+
}
118+
}
119+
120+
/**
121+
* Was the request successful?
122+
*
123+
* @return true if the status code is in the range [200, 400)
124+
*/
125+
@Override
126+
public boolean isSuccessful() {
127+
return this.statusCode() >= HTTP_OK && this.statusCode() < HTTP_BAD_REQUEST;
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import com.spotify.github.tracing.Tracer;
24+
import java.util.concurrent.CompletableFuture;
25+
26+
public interface HttpClient {
27+
CompletableFuture<HttpResponse> send(HttpRequest request);
28+
void setTracer(Tracer tracer);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25+
import com.spotify.github.GithubStyle;
26+
import java.util.List;
27+
import java.util.Map;
28+
import org.immutables.value.Value;
29+
30+
import javax.annotation.Nullable;
31+
32+
@Value.Immutable
33+
@GithubStyle
34+
@JsonSerialize(as = ImmutableHttpRequest.class)
35+
@JsonDeserialize(as = ImmutableHttpRequest.class)
36+
public interface HttpRequest {
37+
@Value.Default
38+
default String method() {
39+
return "GET";
40+
}
41+
42+
String url();
43+
44+
@Nullable
45+
String body();
46+
47+
@Value.Default
48+
default Map<String, List<String>> headers() {
49+
return Map.of();
50+
}
51+
52+
default List<String> headers(String headerName) {
53+
return headers().get(headerName);
54+
}
55+
56+
default String header(String headerName) {
57+
List<String> headerValues = this.headers(headerName);
58+
if (headerValues == null || headerValues.isEmpty()) {
59+
return null;
60+
}
61+
if (headerValues.size() == 1) {
62+
return headerValues.get(0);
63+
} else {
64+
return String.join(",", headerValues);
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import java.io.InputStream;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
public interface HttpResponse {
28+
// Returns the request that was sent to the server
29+
HttpRequest request();
30+
// Returns the HTTP status code
31+
int statusCode();
32+
// Returns the HTTP status message
33+
String statusMessage();
34+
// Returns the response body as an InputStream
35+
InputStream body();
36+
// Returns the response body as a String
37+
String bodyString();
38+
// Returns the response headers as a Map
39+
Map<String, List<String>> headers();
40+
// Returns the response headers for a specific header name as a list of Strings
41+
List<String> headers(String headerName);
42+
// Returns the response headers for a specific header name as a single String
43+
String header(String headerName);
44+
// Returns true if the response was successful
45+
boolean isSuccessful();
46+
// Closes the response
47+
void close();
48+
}

0 commit comments

Comments
 (0)