Skip to content

Commit

Permalink
refactor: Tidy up AbstractMethod docs / logs
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 9, 2025
1 parent 77da52b commit b5ee01c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
97 changes: 63 additions & 34 deletions src/main/java/com/vonage/client/AbstractMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,68 @@
* <p>
* The REST call is executed by calling {@link #execute(Object)}.
*
* @param <RequestT> The request object type that will be used to construct the HTTP request body.
* @param <ResultT> The response object type which will be constructed from the returned HTTP response body.
* @param <REQ> The request object type that will be used to construct the HTTP request body.
* @param <RES> The response object type which will be constructed from the returned HTTP response body.
*
* @see DynamicEndpoint for an abstract implementation which handles the most common use cases.
*/
public abstract class AbstractMethod<RequestT, ResultT> implements RestEndpoint<RequestT, ResultT> {
public abstract class AbstractMethod<REQ, RES> implements RestEndpoint<REQ, RES> {
private static final Logger LOGGER = Logger.getLogger(AbstractMethod.class.getName());

private static boolean shouldLog() {
return LOGGER.isLoggable(Level.INFO);
}

protected final HttpWrapper httpWrapper;
private final HttpWrapper httpWrapper;

public AbstractMethod(HttpWrapper httpWrapper) {
/**
* Construct a new AbstractMethod instance with the given HTTP client.
*
* @param httpWrapper The wrapper containing the HTTP client and configuration.
*/
protected AbstractMethod(HttpWrapper httpWrapper) {
this.httpWrapper = httpWrapper;
}

/**
* Gets the underlying HTTP client wrapper.
*
* @return The {@link HttpWrapper} used by this endpoint.
*/
public HttpWrapper getHttpWrapper() {
return httpWrapper;
}

protected ResultT postProcessParsedResponse(ResultT response) {
/**
* Method which allows further modification of the response object after it has been parsed.
*
* @param response The unmarshalled response object.
*
* @return The final result object to return; usually the same object that was passed in.
*/
protected RES postProcessParsedResponse(RES response) {
return response;
}

private HttpUriRequest createFullHttpRequest(REQ request) throws VonageClientException {
return applyAuth(makeRequest(request))
.setHeader(HttpHeaders.USER_AGENT, httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();
}

/**
* Execute the REST call represented by this method object.
* Executes the REST call represented by this endpoint.
*
* @param request A RequestT representing input to the REST call to be made
* @param request The request object representing input to the REST call to be made.
*
* @return A ResultT representing the response from the executed REST call
* @return The result object representing the response from the executed REST call.
*
* @throws VonageClientException if there is a problem parsing the HTTP response
* @throws VonageResponseParseException if there was a problem parsing the HTTP response.
* @throws VonageMethodFailedException if there was a problem executing the HTTP request.
*/
@Override
public ResultT execute(RequestT request) throws VonageResponseParseException, VonageClientException {
HttpUriRequest httpRequest = applyAuth(makeRequest(request))
.setHeader(HttpHeaders.USER_AGENT, httpWrapper.getUserAgent())
.setCharset(StandardCharsets.UTF_8).build();
public RES execute(REQ request) throws VonageMethodFailedException, VonageResponseParseException {
final HttpUriRequest httpRequest = createFullHttpRequest(request);

if (shouldLog()) {
LOGGER.info("Request " + httpRequest.getMethod() + " " + httpRequest.getURI());
Expand All @@ -90,26 +112,28 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
}
LOGGER.info(headersStr.toString());
}
LOGGER.info("Request body: " + request);
if (request != null) {
LOGGER.info("--- REQUEST BODY ---\n" + request);
}
}

try (CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
try (final CloseableHttpResponse response = httpWrapper.getHttpClient().execute(httpRequest)) {
try {
if (shouldLog()) {
LOGGER.info("Response " + response.getStatusLine());
Header[] headers = response.getAllHeaders();
if (headers != null && headers.length > 0) {
StringBuilder headersStr = new StringBuilder("Response headers:\n");
StringBuilder headersStr = new StringBuilder("--- RESPONSE HEADERS ---\n");
for (Header header : headers) {
headersStr.append('\n').append(header.getName()).append(": ").append(header.getValue());
}
LOGGER.info(headersStr.toString());
}
}

ResultT responseBody = parseResponse(response);
if (shouldLog()) {
LOGGER.info("Response body: " + responseBody);
final RES responseBody = parseResponse(response);
if (responseBody != null && shouldLog()) {
LOGGER.info("--- RESPONSE BODY ---\n" + responseBody);
}

return postProcessParsedResponse(responseBody);
Expand All @@ -126,15 +150,15 @@ public ResultT execute(RequestT request) throws VonageResponseParseException, Vo
}

/**
* Apply an appropriate authentication method (specified by {@link #getAcceptableAuthMethods()}) to the provided
* {@link RequestBuilder}, and return the result.
* Apply an appropriate authentication method (specified by {@link #getAcceptableAuthMethods()}) to the
* provided {@link RequestBuilder}, and return the result.
*
* @param request A RequestBuilder which has not yet had authentication information applied
* @param request A RequestBuilder which has not yet had authentication information applied.
*
* @return A RequestBuilder with appropriate authentication information applied (may or not be the same instance as
* <pre>request</pre>)
* @return A RequestBuilder with appropriate authentication information applied
* (may or not be the same instance as <pre>request</pre>).
*
* @throws VonageClientException If no appropriate {@link AuthMethod} is available
* @throws VonageClientException If no appropriate {@link AuthMethod} is available.
*/
final RequestBuilder applyAuth(RequestBuilder request) throws VonageClientException {
AuthMethod am = getAuthMethod();
Expand Down Expand Up @@ -164,25 +188,30 @@ protected AuthMethod getAuthMethod() throws VonageUnexpectedException {
return httpWrapper.getAuthCollection().getAcceptableAuthMethod(getAcceptableAuthMethods());
}

/**
* Gets applicable authentication methods for this endpoint.
*
* @return The set of acceptable authentication method classes (at least one must be provided).
*/
protected abstract Set<Class<? extends AuthMethod>> getAcceptableAuthMethods();

/**
* Construct and return a RequestBuilder instance from the provided request.
*
* @param request A RequestT representing input to the REST call to be made
* @param request A request object representing input to the REST call to be made.
*
* @return A ResultT representing the response from the executed REST call
* @return A RequestBuilder instance representing the HTTP request to be made.
*/
protected abstract RequestBuilder makeRequest(RequestT request);
protected abstract RequestBuilder makeRequest(REQ request);

/**
* Construct a ResultT representing the contents of the HTTP response returned from the Vonage Voice API.
* Construct a response object representing the contents of the HTTP response returned from the Vonage API.
*
* @param response An HttpResponse returned from the Vonage Voice API
* @param response An HttpResponse returned from the Vonage API.
*
* @return A ResultT type representing the result of the REST call
* @return The unmarshalled result of the REST call.
*
* @throws IOException if a problem occurs parsing the response
* @throws IOException if a problem occurs parsing the response.
*/
protected abstract ResultT parseResponse(HttpResponse response) throws IOException;
protected abstract RES parseResponse(HttpResponse response) throws IOException;
}
6 changes: 3 additions & 3 deletions src/test/java/com/vonage/client/DynamicEndpointTestSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,16 @@ private void assertRequestUriAndBody(T request, String expectedRequestBodyString
assertEquals(expectedUri, builder.build().getURI().toString().split("\\?")[0]);

AbstractMethod<T, R> endpoint = endpointAsAbstractMethod();
HttpConfig originalConfig = endpoint.httpWrapper.getHttpConfig();
HttpConfig originalConfig = endpoint.getHttpWrapper().getHttpConfig();
try {
String baseUri = customBaseUri();
endpoint.httpWrapper.setHttpConfig(HttpConfig.builder().baseUri(baseUri).build());
endpoint.getHttpWrapper().setHttpConfig(HttpConfig.builder().baseUri(baseUri).build());
builder = makeTestRequest(request);
expectedUri = expectedCustomBaseUri() + expectedEndpointUri(request);
assertEquals(expectedUri, builder.build().getURI().toString().split("\\?")[0]);
}
finally {
endpoint.httpWrapper.setHttpConfig(originalConfig);
endpoint.getHttpWrapper().setHttpConfig(originalConfig);
}
}

Expand Down

0 comments on commit b5ee01c

Please sign in to comment.