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
36 changes: 1 addition & 35 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>github-client</artifactId>
<version>0.3.12-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>

<parent>
<groupId>com.spotify</groupId>
Expand Down Expand Up @@ -86,8 +86,6 @@
<opencensus.version>0.31.1</opencensus.version>
<okhttp.version>4.11.0</okhttp.version>
<opentelemetry.version>1.42.1</opentelemetry.version>

<shade.id>${project.groupId}.githubclient.shade</shade.id>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -483,38 +481,6 @@
</sourceDirectories>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
<include>com.squareup.okhttp3</include>
<include>com.squareup.okio</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>${shade.id}.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>${shade.id}.okio</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/com/spotify/github/http/BaseHttpResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2021 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.http;

import java.util.List;
import java.util.Map;

/** BaseHttpResponse is the base implementation of HttpResponse. */
public abstract class BaseHttpResponse implements HttpResponse {
private static final int HTTP_OK = 200;
private static final int HTTP_BAD_REQUEST = 400;

protected final HttpRequest request;
protected final int statusCode;
protected final String statusMessage;
protected final Map<String, List<String>> headers;

public BaseHttpResponse(
final HttpRequest request,
final int statusCode,
final String statusMessage,
final Map<String, List<String>> headers) {
this.request = request;
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.headers = headers;
}

/**
* Returns the request that generated this response.
*
* @return HttpRequest the request that generated this response
*/
@Override
public HttpRequest request() {
return request;
}

/**
* Returns the HTTP status code of the response.
*
* @return the status code of the response
*/
@Override
public int statusCode() {
return statusCode;
}

/**
* Returns the HTTP status message of the response.
*
* @return the status message of the response
*/
@Override
public String statusMessage() {
return statusMessage;
}

/**
* Returns the headers of the response.
*
* @return the headers of the response as a Map of strings
*/
@Override
public Map<String, List<String>> headers() {
return this.headers;
}

/**
* Returns the values of the header with the given name. If the header is not present, this method
* returns null.
*
* @param headerName the name of the header
* @return the values of the header with the given name as a List of strings, or null if the
* header is not present
*/
@Override
public List<String> headers(final String headerName) {
return this.headers.get(headerName);
}

/**
* Returns the first value of the header with the given name. If the header is not present, this
* method returns null.
*
* @param headerName the name of the header
* @return the first value of the header with the given name, or null if the header is not present
*/
@Override
public String header(final String headerName) {
List<String> headerValues = this.headers(headerName);
if (headerValues == null || headerValues.isEmpty()) {
return null;
}
if (headerValues.size() == 1) {
return headerValues.get(0);
} else {
return String.join(",", headerValues);
}
}

/**
* Was the request successful?
*
* @return true if the status code is in the range [200, 400)
*/
@Override
public boolean isSuccessful() {
return this.statusCode() >= HTTP_OK && this.statusCode() < HTTP_BAD_REQUEST;
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/spotify/github/http/HttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2021 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.http;

import com.spotify.github.tracing.Tracer;
import java.util.concurrent.CompletableFuture;

public interface HttpClient {
CompletableFuture<HttpResponse> send(HttpRequest request);
void setTracer(Tracer tracer);
}
67 changes: 67 additions & 0 deletions src/main/java/com/spotify/github/http/HttpRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2021 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.http;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import java.util.List;
import java.util.Map;
import org.immutables.value.Value;

import javax.annotation.Nullable;

@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableHttpRequest.class)
@JsonDeserialize(as = ImmutableHttpRequest.class)
public interface HttpRequest {
@Value.Default
default String method() {
return "GET";
}

String url();

@Nullable
String body();

@Value.Default
default Map<String, List<String>> headers() {
return Map.of();
}

default List<String> headers(String headerName) {
return headers().get(headerName);
}

default String header(String headerName) {
List<String> headerValues = this.headers(headerName);
if (headerValues == null || headerValues.isEmpty()) {
return null;
}
if (headerValues.size() == 1) {
return headerValues.get(0);
} else {
return String.join(",", headerValues);
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/spotify/github/http/HttpResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2021 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.http;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public interface HttpResponse {
// Returns the request that was sent to the server
HttpRequest request();
// Returns the HTTP status code
int statusCode();
// Returns the HTTP status message
String statusMessage();
// Returns the response body as an InputStream
InputStream body();
// Returns the response body as a String
String bodyString();
// Returns the response headers as a Map
Map<String, List<String>> headers();
// Returns the response headers for a specific header name as a list of Strings
List<String> headers(String headerName);
// Returns the response headers for a specific header name as a single String
String header(String headerName);
// Returns true if the response was successful
boolean isSuccessful();
// Closes the response
void close();
}
Loading