Package com.nexmo.client
Class AbstractMethod<RequestT,ResultT>
- java.lang.Object
-
- com.nexmo.client.AbstractMethod<RequestT,ResultT>
-
- Type Parameters:
RequestT- The type of the method-specific request object that will be used to construct an HTTP requestResultT- The type of method-specific response object which will be constructed from the returned HTTP response
- All Implemented Interfaces:
Method<RequestT,ResultT>
- Direct Known Subclasses:
SearchRejectedMessagesEndpoint,SmsSearchEndpoint,SmsSingleSearchEndpoint
public abstract class AbstractMethod<RequestT,ResultT> extends java.lang.Object implements Method<RequestT,ResultT>
Abstract class to assist in implementing a call against a REST endpoint.Concrete implementations must implement
makeRequest(Object)to construct aRequestBuilderfrom the provided parameterized request object, andparseResponse(HttpResponse)to construct the parameterizedHttpResponseobject.The REST call is executed by calling
execute(Object).
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpWrapperhttpWrapper
-
Constructor Summary
Constructors Constructor Description AbstractMethod(HttpWrapper httpWrapper)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected org.apache.http.client.methods.RequestBuilderapplyAuth(org.apache.http.client.methods.RequestBuilder request)Apply an appropriate authentication method (specified bygetAcceptableAuthMethods()to the providedRequestBuilder, and return the result.ResultTexecute(RequestT request)Execute the REST call represented by this method object.protected abstract java.lang.Class[]getAcceptableAuthMethods()protected AuthMethodgetAuthMethod(java.lang.Class[] acceptableAuthMethods)Utility method for obtaining an appropriateAuthMethodfor this call.abstract org.apache.http.client.methods.RequestBuildermakeRequest(RequestT request)Construct and return a RequestBuilder instance from the provided request.abstract ResultTparseResponse(org.apache.http.HttpResponse response)Construct a ResultT representing the contents of the HTTP response returned from the Nexmo Voice API.voidsetHttpClient(org.apache.http.client.HttpClient client)
-
-
-
Field Detail
-
httpWrapper
protected final HttpWrapper httpWrapper
-
-
Constructor Detail
-
AbstractMethod
public AbstractMethod(HttpWrapper httpWrapper)
-
-
Method Detail
-
execute
public ResultT execute(RequestT request) throws NexmoResponseParseException, NexmoClientException
Execute the REST call represented by this method object.- Specified by:
executein interfaceMethod<RequestT,ResultT>- Parameters:
request- A RequestT representing input to the REST call to be made- Returns:
- A ResultT representing the response from the executed REST call
- Throws:
NexmoClientException- if there is a problem parsing the HTTP responseNexmoResponseParseException
-
applyAuth
protected org.apache.http.client.methods.RequestBuilder applyAuth(org.apache.http.client.methods.RequestBuilder request) throws NexmoClientException
Apply an appropriate authentication method (specified bygetAcceptableAuthMethods()to the providedRequestBuilder, and return the result.- Parameters:
request- A RequestBuilder which has not yet had authentication information applied- Returns:
- A RequestBuilder with appropriate authentication information applied (may or not be the same instance as
request
) - Throws:
NexmoClientException- If no appropriateAuthMethodis available
-
getAuthMethod
protected AuthMethod getAuthMethod(java.lang.Class[] acceptableAuthMethods) throws NexmoClientException
Utility method for obtaining an appropriateAuthMethodfor this call.- Parameters:
acceptableAuthMethods- an array of classes, representing authentication methods that are acceptable for this endpoint- Returns:
- An AuthMethod created from one of the provided acceptableAuthMethods.
- Throws:
NexmoClientException- If no AuthMethod is available from the provided array of acceptableAuthMethods.
-
setHttpClient
public void setHttpClient(org.apache.http.client.HttpClient client)
-
getAcceptableAuthMethods
protected abstract java.lang.Class[] getAcceptableAuthMethods()
-
makeRequest
public abstract org.apache.http.client.methods.RequestBuilder makeRequest(RequestT request) throws java.io.UnsupportedEncodingException
Construct and return a RequestBuilder instance from the provided request.- Parameters:
request- A RequestT representing input to the REST call to be made- Returns:
- A ResultT representing the response from the executed REST call
- Throws:
java.io.UnsupportedEncodingException- if UTF-8 encoding is not supported by the JVM
-
parseResponse
public abstract ResultT parseResponse(org.apache.http.HttpResponse response) throws java.io.IOException
Construct a ResultT representing the contents of the HTTP response returned from the Nexmo Voice API.- Parameters:
response- An HttpResponse returned from the Nexmo Voice API- Returns:
- A ResultT type representing the result of the REST call
- Throws:
java.io.IOException- if a problem occurs parsing the response
-
-