public class RestClient extends Object
Modifier and Type | Field and Description |
---|---|
static URL |
DEFAULT_BASE_URL
The standard base URI for the GATE Cloud API.
|
Constructor and Description |
---|
RestClient(String apiKeyId,
String apiPassword)
Create a client that uses the
default base
URL . |
RestClient(URL url,
String apiKeyId,
String apiPassword)
Create a client using a specified base URL (for advanced use only -
the default URL will work for all normal cases).
|
Modifier and Type | Method and Description |
---|---|
void |
delete(String target)
Perform an HTTP DELETE request for the given resource.
|
<T> T |
get(String target,
com.fasterxml.jackson.core.type.TypeReference<T> responseType)
Perform an HTTP GET request, parsing the JSON response to create a
new object.
|
URL |
getBaseUrl() |
void |
getForUpdate(String target,
Object responseObject)
Perform an HTTP GET request, parsing the JSON response to update
the state of an existing object.
|
Map<String,List<String>> |
getLastHeaders()
Returns the response headers that were sent with the most recent
successful HTTP response received by this client in this thread.
|
URL |
getRedirect(URL source)
Perform an HTTP GET request on a URL whose response is expected to
be a 3xx redirection, and return the target redirection URL.
|
<T> T |
post(String target,
com.fasterxml.jackson.core.type.TypeReference<T> responseType,
Object requestBody)
Perform an HTTP POST request, parsing the JSON response to create a
new object.
|
void |
postForUpdate(String target,
Object responseObject,
Object requestBody)
Perform an HTTP POST request, parsing the JSON response to update
the state of an existing object.
|
<T> T |
request(String target,
String method,
com.fasterxml.jackson.core.type.TypeReference<T> responseType,
Object requestBody,
String... extraHeaders)
Make an API request and parse the JSON response into a new object.
|
InputStream |
requestForStream(String target,
String method,
Object requestBody,
int gzipThreshold,
String... extraHeaders)
Make an API request and return the raw data from the response as an
InputStream.
|
void |
requestForUpdate(String target,
String method,
Object responseObject,
Object requestBody,
String... extraHeaders)
Make an API request and parse the JSON response, using the response
to update the state of an existing object.
|
public static final URL DEFAULT_BASE_URL
public RestClient(String apiKeyId, String apiPassword)
default base
URL
.apiKeyId
- API key identifier for authentication. If null, no
authentication will be used, which means the client can
only be used to call public pipelines via the online API.apiPassword
- API key passwordpublic RestClient(URL url, String apiKeyId, String apiPassword)
url
- API base URLapiKeyId
- API key identifier for authenticationapiPassword
- API key passwordpublic URL getBaseUrl()
public <T> T request(String target, String method, com.fasterxml.jackson.core.type.TypeReference<T> responseType, Object requestBody, String... extraHeaders) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).method
- the request method (GET, POST, DELETE, etc.)responseType
- the Java type corresponding to a successful
response message for this URLrequestBody
- the value to send as the request body. If
null
, no request body is sent. If an
InputStream
or a StreamWriteable
then its content will be sent as-is and an appropriate
Content-Type
should be given in the
extraHeaders
(note that the input stream will
not be closed by this method, that is the
responsibility of the caller). Otherwise the provided
object will be serialized to JSON and sent with the
default application/json
MIME type.extraHeaders
- any additional HTTP headers, specified as an
alternating sequence of header names and valuesnull
for a 201 responseRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public InputStream requestForStream(String target, String method, Object requestBody, int gzipThreshold, String... extraHeaders) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).method
- the request method (GET, POST, DELETE, etc.)requestBody
- the value to send as the request body. If
null
, no request body is sent. If an
InputStream
or a StreamWriteable
then its content will be sent as-is and an appropriate
Content-Type
should be given in the
extraHeaders
(note that the input stream will
not be closed by this method, that is the
responsibility of the caller). Otherwise the provided
object will be serialized to JSON and sent with the
default application/json
MIME type.gzipThreshold
- size threshold above which the request body
should be GZIP compressed. If negative, the request will
never be compressed.extraHeaders
- any additional HTTP headers, specified as an
alternating sequence of header names and valuesnull
for a 201 responseRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public void requestForUpdate(String target, String method, Object responseObject, Object requestBody, String... extraHeaders) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).method
- the request method (GET, POST, DELETE, etc.)responseObject
- the Java object to update from a successful
response message for this URLrequestBody
- the value to send as the request body. If
null
, no request body is sent. If an
InputStream
or a StreamWriteable
then its content will be sent as-is and an appropriate
Content-Type
should be given in the
extraHeaders
(note that the input stream will
not be closed by this method, that is the
responsibility of the caller). Otherwise the provided
object will be serialized to JSON and sent with the
default application/json
MIME type.extraHeaders
- any additional HTTP headers, specified as an
alternating sequence of header names and valuesRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public <T> T get(String target, com.fasterxml.jackson.core.type.TypeReference<T> responseType) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).responseType
- the Java type corresponding to a successful
response message for this URLnull
for a 201 responseRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public void getForUpdate(String target, Object responseObject) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).responseObject
- the Java object to update from a successful
response message for this URLRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public <T> T post(String target, com.fasterxml.jackson.core.type.TypeReference<T> responseType, Object requestBody) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).responseType
- the Java type corresponding to a successful
response message for this URLrequestBody
- the value to send as the request body. If
null
, no request body is sent. If an
InputStream
or a StreamWriteable
then its content will be sent as-is and an appropriate
Content-Type
should be given in the
extraHeaders
(note that the input stream will
not be closed by this method, that is the
responsibility of the caller). Otherwise the provided
object will be serialized to JSON and sent with the
default application/json
MIME type.null
for a 201 responseRestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public void postForUpdate(String target, Object responseObject, Object requestBody) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).responseObject
- the Java object to update from a successful
response message for this URLrequestBody
- the value to send as the request body. If
null
, no request body is sent. If an
InputStream
or a StreamWriteable
then its content will be sent as-is and an appropriate
Content-Type
should be given in the
extraHeaders
(note that the input stream will
not be closed by this method, that is the
responsibility of the caller). Otherwise the provided
object will be serialized to JSON and sent with the
default application/json
MIME type.RestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public void delete(String target) throws RestClientException
target
- the URL to request (relative URLs will resolve
against the base URL
).RestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception).public URL getRedirect(URL source) throws RestClientException
source
- the URL to request (relative URLs will resolve
against the base URL
).RestClientException
- if an exception occurs during
processing, or the server returns a 4xx or 5xx error
response (in which case the response JSON message will be
available as a JsonNode
in the exception), or if
the response was not a 3xx redirection.public Map<String,List<String>> getLastHeaders()
Copyright © 2023 GATE. All rights reserved.