T
- the RuntimePlatform
subclass containing this RestAction
public abstract class RestAction<T extends RuntimePlatform> extends RuntimeAction<T>
This action provides the execution logic to compute GET and POST REST requests over a provided endpoint. Request parameters, headers, and body can be specified through the constructor parameters.
Note: this class assumes that the requested REST API expects nothing or JSON in the query body. This class
also expects that the API response contains an empty body or a JsonElement
.
Modifier and Type | Class and Description |
---|---|
static class |
RestAction.MethodKind
The kind of REST methods supported by this class.
|
Modifier and Type | Field and Description |
---|---|
protected Map<String,String> |
headers
A
Map containing user-defined headers to include in the request. |
protected com.google.gson.JsonElement |
jsonContent
The
JsonElement to include in the request's body. |
protected RestAction.MethodKind |
method
The
RestAction.MethodKind of the request to perform. |
protected Map<String,Object> |
params
A
Map containing user-defined parameters to include in the request. |
protected String |
restEndpoint
The REST API endpoint to request.
|
runtimePlatform, session
Constructor and Description |
---|
RestAction(T runtimePlatform,
XatkitSession session,
RestAction.MethodKind method,
Map<String,String> headers,
String restEndpoint,
Map<String,Object> params,
com.google.gson.JsonElement jsonContent)
Constructs a new
RestAction . |
Modifier and Type | Method and Description |
---|---|
protected Object |
compute()
Computes the REST request and returned the handled result.
|
static com.google.gson.JsonElement |
getJsonBody(InputStream is)
Extracts the
JsonElement contained in the provided is . |
static String |
getStringBody(InputStream is)
Extracts the
String contained in the provided is . |
protected abstract Object |
handleResponse(com.mashape.unirest.http.Headers headers,
int status,
InputStream body)
Handles the REST API response and computes the action's results
|
call, getSession, init
protected RestAction.MethodKind method
RestAction.MethodKind
of the request to perform.protected String restEndpoint
The provided REST endpoint url must be absolute. Relative endpoint urls are not supported for now.
protected com.google.gson.JsonElement jsonContent
JsonElement
to include in the request's body.
This element is only used for POST requests.
public RestAction(T runtimePlatform, XatkitSession session, RestAction.MethodKind method, @Nullable Map<String,String> headers, String restEndpoint, @Nullable Map<String,Object> params, @Nullable com.google.gson.JsonElement jsonContent)
RestAction
.
This method doesn't perform the REST API request, this is done asynchronously in the compute()
method.
runtimePlatform
- the RuntimePlatform
containing this actionsession
- the XatkitSession
associated to this actionmethod
- the REST method to useheaders
- the Map
of user-defined headers to include in the requestrestEndpoint
- the REST API endpoint to requestparams
- the Map
of user-defined parameters to include in the requestjsonContent
- the JsonElement
to include in the request's bodyNullPointerException
- if the provided runtimePlatform
or session
is null
IllegalArgumentException
- if the provided method
is null
, or if the provided restEndpoint
is null
or empty
public static com.google.gson.JsonElement getJsonBody(InputStream is) throws com.google.gson.JsonSyntaxException
JsonElement
contained in the provided is
.is
- the InputStream
to extract the JsonElement
fromJsonElement
com.google.gson.JsonSyntaxException
public static String getStringBody(InputStream is)
String
contained in the provided is
.is
- the InputStream
to extract the String
fromString
protected final Object compute() throws Exception
This method performs the request over the defined REST API, and delegates the result computation to the
handleResponse(Headers, int, InputStream)
method.
compute
in class RuntimeAction<T extends RuntimePlatform>
Exception
- if an error occurred when executing the REST requesthandleResponse(Headers, int, InputStream)
protected abstract Object handleResponse(com.mashape.unirest.http.Headers headers, int status, InputStream body)
This method is overridden in concrete subclasses to implement action-specific behaviors.
headers
- the Headers
returned by the REST APIstatus
- the status code returned by the REST APIbody
- the InputStream
containing the response bodyCopyright © 2020 SOM Research Lab. All rights reserved.