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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Bad Request (status code 400) response from the server.
*/
public class BadRequestException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public BadRequestException(Response response) {
super("A Bad request status code was received. Please verify the input.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Forbidden (status code 403) response from the server.
*/
public class ForbiddenException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public ForbiddenException(Response response) {
super("A Forbidden status code was received. Please verify the permissions of your user.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's an I'm a Teapot (status code 418) response from the server.
*/
public class ImATeapotException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public ImATeapotException(Response response) {
super("I'm a teapot. I cannot process data.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's an Internal Server Error (status code 500) response from the server.
*/
public class InternalServerErrorException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public InternalServerErrorException(Response response) {
super("An internal error occurred on the server. Please contact an administrator.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Method Not Allowed (status code 405) response from the server.
*/
public class MethodNotAllowedException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public MethodNotAllowedException(Response response) {
super("Trying to access service with the wrong HTTP method.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Not Acceptable (status code 406) response from the server.
*/
public class NotAcceptableException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public NotAcceptableException(Response response) {
super("Request indicates that the accept header is not of the same type as the returned one from the server.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Not Found (status code 404) response from the server.
*/
public class NotFoundException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public NotFoundException(Response response) {
super("The sought entity was not found.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Request Timeout (status code 408) response from the server.
*/
public class RequestTimeoutException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public RequestTimeoutException(Response response) {
super("Server is taking longer than the expected amount. Please retry later.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's a Service Unavailable (status code 503) response from the server.
*/
public class ServiceUnavailableException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public ServiceUnavailableException(Response response) {
super("Service is currently unavailable. Please retry later.", response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.polyapi.commons.api.error.http;

import io.polyapi.commons.api.http.Response;

/**
* Exception thrown when there's an Unauthorized (status code 401) response from the server.
*/
public class UnauthorizedException extends HttpResponseException {

/**
* Constructor that takes a message and the response returned.
*
* @param response The response.
*/
public UnauthorizedException(Response response) {
super("An Unauthorized status code was received. Please verify the permissions of your user.", response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ private <I, O> O parsedCall(HttpMethod method, String relativePath, Map<String,
headers.forEach((key, value) -> allHeaders.put(key, value.stream().toList()));

Response response = callApi(method, relativePath, allHeaders, queryParams, jsonParser.toJsonInputStream(body));
if (response.statusCode() < 200) {
throw new UnexpectedInformationalResponseException(response);
}
if (response.statusCode() >= 400) {
// TODO: Change this to more specific exceptions per code. As some may require actions rather than displaying an error (i.e. token refresh).
switch (response.statusCode()) {
default:
throw new UnexpectedHttpResponseException(response);
}
}
log.debug("Response is successful. Status code is {}.", response.statusCode());
log.debug("Parsing response.");
O result = Optional.of(expectedResponseType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package io.polyapi.commons.internal.http;

import io.polyapi.commons.api.error.PolyApiException;
import io.polyapi.commons.api.http.*;
import io.polyapi.commons.api.http.HttpClient;
import io.polyapi.commons.api.http.HttpMethod;
import io.polyapi.commons.api.http.Request;
import io.polyapi.commons.api.http.RequestRecord;
import io.polyapi.commons.api.http.Response;
import io.polyapi.commons.api.http.ResponseRecord;
import io.polyapi.commons.api.http.TokenProvider;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
Expand All @@ -21,27 +27,24 @@
public class DefaultHttpClient implements HttpClient {

private final OkHttpClient client;
private final TokenProvider tokenProvider;
private final HttpClientConfiguration configuration;

/**
* Utility constructor that sets a default {@link OkHttpClient} and uses a {@link TokenProvider}.
*
* @param tokenProvider The provided token provider.
* @param connectTimeoutMillis The amount of milliseconds that the client will wait on connection before timing out.
* @param readTimeoutMillis The amount of milliseconds that the client will wait on reading the response before timing out.
* @param writeTimeoutMillis The amount of milliseconds that the client will wait on writing before timing out.
* @param configuration The configuration for the HTTP client.
*/
public DefaultHttpClient(TokenProvider tokenProvider, Long connectTimeoutMillis, Long readTimeoutMillis, Long writeTimeoutMillis) {
public DefaultHttpClient(HttpClientConfiguration configuration) {
this(new OkHttpClient.Builder()
.connectTimeout(connectTimeoutMillis, MILLISECONDS)
.readTimeout(readTimeoutMillis, MILLISECONDS)
.writeTimeout(writeTimeoutMillis, MILLISECONDS)
.build(), tokenProvider);
.connectTimeout(configuration.getConnectTimeoutMillis(), MILLISECONDS)
.readTimeout(configuration.getReadTimeoutMillis(), MILLISECONDS)
.writeTimeout(configuration.getWriteTimeoutMillis(), MILLISECONDS)
.build(), configuration);
}

public DefaultHttpClient(OkHttpClient client, TokenProvider tokenProvider) {
public DefaultHttpClient(OkHttpClient client, HttpClientConfiguration configuration) {
this.client = client;
this.tokenProvider = tokenProvider;
this.configuration = configuration;
}

@Override
Expand All @@ -53,7 +56,7 @@ public HttpRequestBuilder prepareRequest(String host, Integer port, HttpMethod m
@Override
public HttpRequestBuilder prepareAuthenticatedRequest(String host, Integer port, HttpMethod method, String relativePath) {
return prepareRequest(host, port, method, relativePath)
.withHeader("Authorization", tokenProvider.getTokenAsHeader());
.withHeader("Authorization", configuration.getTokenProvider().getTokenAsHeader());
}

@Override
Expand Down Expand Up @@ -97,7 +100,7 @@ public Response send(Request request) {
);
result.body().reset();
}
return result;
return (response.code() < 200 || response.code() > 299) ? configuration.getErrorHandlingStrategy().apply(result) : result;
} catch (IOException e) {
// FIXME: Throw the appropriate exception.
throw new RuntimeException(e);
Expand Down
Loading