Skip to content

Instantly share code, notes, and snippets.

@ts14ic
Created January 4, 2020 21:44
Show Gist options
  • Save ts14ic/483d2c7875e470b01d5b57a945bd87e1 to your computer and use it in GitHub Desktop.
Save ts14ic/483d2c7875e470b01d5b57a945bd87e1 to your computer and use it in GitHub Desktop.
import java.util.*;
import static java.util.Objects.requireNonNull;
public class HttpRequest {
private final Method method;
private final String url;
private final Map<String, String> headers;
private String body;
public HttpRequest(Method method, String url) {
this.method = requireNonNull(method);
this.url = requireNonNull(url);
this.body = "";
this.headers = new HashMap<>();
}
public Method getMethod() {
return method;
}
public String getUrl() {
return url;
}
public HttpRequest setBody(String body) {
this.body = body;
return this;
}
public String getBody() {
return body;
}
public HttpRequest setHeader(String name, String value) {
headers.put(name, value);
return this;
}
public Set<Map.Entry<String, String>> getHeaders() {
return Collections.unmodifiableSet(headers.entrySet());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HttpRequest that = (HttpRequest) o;
return method == that.method &&
Objects.equals(url, that.url) &&
Objects.equals(headers, that.headers) &&
Objects.equals(body, that.body);
}
@Override
public int hashCode() {
return Objects.hash(method, url, headers, body);
}
@Override
public String toString() {
return "HttpRequest{" +
"method=" + method +
", url='" + url + '\'' +
", headers=" + headers +
", body='" + body + '\'' +
'}';
}
public enum Method {
GET, POST
}
}
/**
* A wrapper around AsyncHttpClient to facilitate test writing.
*/
public class HttpRequester {
private final AsyncHttpClient client;
@Inject
public HttpRequester() {
this.client = new DefaultAsyncHttpClient();
}
/**
* Executes the request synchronously.
*
* @param request the request to execute.
* @return http response
* @throws ExecutionException when unexpected error happens like timeout.
* @throws InterruptedException when request thread interrupted.
*/
public HttpResponse execute(HttpRequest request) throws ExecutionException, InterruptedException {
Response response = executeAsync(request).get();
return HttpResponse.newBuilder()
.setBody(response.getResponseBody())
.setStatusCode(response.getStatusCode())
.build();
}
private ListenableFuture<Response> executeAsync(HttpRequest request) {
BoundRequestBuilder builder;
if (request.getMethod() == HttpRequest.Method.GET) {
builder = client.prepareGet(request.getUrl());
} else if (request.getMethod() == HttpRequest.Method.POST) {
builder = client.preparePost(request.getUrl());
} else {
throw new UnsupportedOperationException(request.getMethod() + " not yet supported");
}
for (Map.Entry<String, String> header : request.getHeaders()) {
builder = builder.setHeader(header.getKey(), header.getValue());
}
builder = builder.setBody(request.getBody());
return builder.execute();
}
}
import java.util.Objects;
public class HttpResponse {
private final int statusCode;
private final String body;
private HttpResponse(Builder builder) {
statusCode = builder.statusCode;
body = builder.body;
}
public static Builder newBuilder() {
return new Builder();
}
public int getStatusCode() {
return statusCode;
}
public String getBody() {
return body;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HttpResponse that = (HttpResponse) o;
return statusCode == that.statusCode &&
Objects.equals(body, that.body);
}
@Override
public int hashCode() {
return Objects.hash(statusCode, body);
}
@Override
public String toString() {
return "HttpResponse{" +
"statusCode=" + statusCode +
", body='" + body + '\'' +
'}';
}
public static final class Builder {
private int statusCode;
private String body;
private Builder() {
}
public Builder setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}
public Builder setBody(String body) {
this.body = body;
return this;
}
public HttpResponse build() {
return new HttpResponse(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment