T
- The type corresponding with the HTTP requests made from this class. For instance, this type is returned
from GET calls and is also the type of the payload sent for POST, PUT, and PATCH calls. Since DELETE calls
do not necessarily send or receive this type, it is not part of the signatures of those methods.public class Requester<T> extends Object
request(url)
method to manage simple HTTP request API calls:
User user = User.request("http://example.com/users").getOne("/$id");
// or
Requester<User> req = User.request("http://example.com/users")
.withBearerAuthorization("xxx...x"); // eg., using OAuth token
User user = req.getOne("/$id");
user.setName("Scott");
req.putOne("/$id", user);
Modifier and Type | Class and Description |
---|---|
static class |
Requester.Format |
Constructor and Description |
---|
Requester(Endpoint endpoint) |
Requester(Endpoint endpoint,
Function<Object,Object> resultCoercer) |
Requester(String urlBase)
Get an instance of
Requester from a JSON API type eg., User.request() . |
Requester(String urlBase,
Function<Object,Object> resultCoercer) |
Modifier and Type | Method and Description |
---|---|
<R> R |
delete(Object arguments)
Same as calling:
delete(String, Object, Format) with delete("", arguments, _format) |
<R> R |
delete(String urlSuffix)
Same as calling:
delete(String, Object, Format) with delete(urlSuffix, null, _format) |
<R> R |
delete(String urlSuffix,
Object arguments)
Same as calling:
delete(String, Object, Format) with delete(urlSuffix, arguments, _format) |
<R> R |
delete(String urlSuffix,
Object arguments,
Requester.Format format)
Make an HTTP DELETE request to
urlBase + urlSuffix . |
Endpoint |
getEndpoint() |
Requester.Format |
getFormat() |
Map<String,String> |
getHeaders() |
IJsonList<T> |
getMany()
Uses HTTP GET for the complete list of
T JSON API objects as a IJsonList<T> . |
IJsonList<T> |
getMany(Object arguments)
Same as calling:
getMany(String, Object, Format) with getMany("", arguments, _format) |
IJsonList<T> |
getMany(String urlSuffix)
Same as calling:
getMany(String, Object, Format) with getMany(urlSuffix, null, _format) |
IJsonList<T> |
getMany(String urlSuffix,
Object arguments)
Same as calling:
getMany(String, Object, Format) with getMany(urlSuffix, arguments, _format) |
IJsonList<T> |
getMany(String urlSuffix,
Object arguments,
Requester.Format format)
Make an HTTP GET request to
urlBase + urlSuffix . |
T |
getOne()
Use HTTP GET for a single
T JSON API object specified in the urlSuffix , such as "/108" . |
T |
getOne(Object arguments)
Same as calling:
getOne(String, Object, Format) with getOne("", arguments, _format) |
T |
getOne(String urlSuffix)
Use HTTP GET for a single
T JSON API object specified in the urlSuffix , such as "/108" . |
T |
getOne(String urlSuffix,
Object arguments)
Same as calling:
getOne(String, Object, Format) with getOne(urlSuffix, arguments, _format) |
T |
getOne(String urlSuffix,
Object arguments,
Requester.Format format)
Make an HTTP GET request to
urlBase + urlSuffix . |
Map<String,String> |
getParameters() |
Function<T,Object> |
getRawResponseHandler() |
int |
getTimeout() |
<R> R |
patchMany(List<T> payload)
Same as calling:
patchMany(String, List, Format) with patchMany("", payload, _format) |
<R> R |
patchMany(String urlSuffix,
List<T> payload)
Same as calling:
patchMany(String, List, Format) with patchMany(urlSuffix, payload, _format) |
<R> R |
patchMany(String urlSuffix,
List<T> payload,
Requester.Format format)
Make an HTTP PATCH request to
urlBase + urlSuffix . |
<R> R |
patchOne(String urlSuffix,
T payload)
Same as calling:
patchOne(String, Object, Format) with patchOne(urlSuffix, payload, _format) |
<R> R |
patchOne(String urlSuffix,
T payload,
Requester.Format format)
Make an HTTP PATCH request to
urlBase + urlSuffix . |
<R> R |
patchOne(T payload)
Same as calling:
patchOne(String, Object, Format) with patchOne("", payload, _format) |
<R> R |
postMany(List<T> payload)
Same as calling:
postMany(String, List, Format) with postMany("", payload, _format) |
<R> R |
postMany(String urlSuffix,
List<T> payload)
Same as calling:
postMany(String, List, Format) with postMany(urlSuffix, payload, _format) |
<R> R |
postMany(String urlSuffix,
List<T> payload,
Requester.Format format)
Make an HTTP POST request to
urlBase + urlSuffix . |
<R> R |
postOne(String urlSuffix,
T payload)
Same as calling:
postOne(String, Object, Format) with postOne(urlSuffix, payload, _format) |
<R> R |
postOne(String urlSuffix,
T payload,
Requester.Format format)
Make an HTTP POST request to
urlBase + urlSuffix . |
<R> R |
postOne(T payload)
Same as calling:
postOne(String, Object, Format) with postOne("", payload, _format) |
<R> R |
putMany(List<T> payload)
Same as calling:
putMany(String, List, Format) with putMany("", payload, _format) |
<R> R |
putMany(String urlSuffix,
List<T> payload)
Same as calling:
putMany(String, List, Format) with putMany(urlSuffix, payload, _format) |
<R> R |
putMany(String urlSuffix,
List<T> payload,
Requester.Format format)
Make an HTTP PUT request to
urlBase + urlSuffix . |
<R> R |
putOne(String urlSuffix,
T payload)
Same as calling:
putOne(String, Object, Format) with putOne(urlSuffix, payload, _format) |
<R> R |
putOne(String urlSuffix,
T payload,
Requester.Format format)
Make an HTTP PUT request to
urlBase + urlSuffix . |
<R> R |
putOne(T payload)
Same as calling:
putOne(String, Object, Format) with putOne("", payload, _format) |
Requester<T> |
withAuthorization(String tokenType,
String accessToken) |
Requester<T> |
withBasicAuthorization(String username,
String password)
Set the Basic Authorization header using the provided
username and password |
Requester<T> |
withBearerAuthorization(String accessToken)
Set the Bearer Authorization header using the provided
accessToken . |
Requester<T> |
withCoercer(Function<Object,Object> resultCoercer) |
Requester<T> |
withHeader(String name,
String value)
|
Requester<T> |
withRawResponseHandler(Function<T,Object> handler) |
Requester<T> |
withResponseFormat(Requester.Format format)
Set the default format expected in the response.
|
Requester<T> |
withTimeout(int timeout)
The connection timeout setting in milliseconds.
|
public Requester(String urlBase)
Requester
from a JSON API type eg., User.request()
. Requester is a builder
type: you can configure the requests you'll make using withXxx()
calls to specify an authorization
token, response format, custom headers, etc. Then you can make one or more requests with a single instance:
Requester<User> req = User.request("http://example.com/users")
.withBearerAuthorization("xxx...x"); // eg., using OAuth token
User user = req.getOne("/$id");
user.setName("Scott");
req.putOne("/$id", user);
urlBase
- A URL providing HTTP services for T
, such as "http://example.com/users"public Requester(Endpoint endpoint)
public Endpoint getEndpoint()
public Requester.Format getFormat()
public int getTimeout()
public Requester<T> withResponseFormat(Requester.Format format)
format
- Json, Yaml, Xml, Csv, or Plain text. Default is Json.public Requester<T> withHeader(String name, String value)
name : value
pair
See
public Requester<T> withParam(String name, String value)
name=value
parameter to the request URL.public Requester<T> withBasicAuthorization(String username, String password)
username
and password
public Requester<T> withBearerAuthorization(String accessToken)
accessToken
.
For instance, if using OAuth, accessToken
is the token response from:
curl -d "grant_type=password&client_id=[...]&client_secret=[...]&username=[...]&password=[...]"
https://[domain]/[oauth-service]
public Requester<T> withTimeout(int timeout)
public Requester<T> withRawResponseHandler(Function<T,Object> handler)
handler
- An optional handler for processing the raw response as an arbitrary Bindings instance. The handler
may return a custom bindings object which overrides the default, type-safe result instance. In any
case, the handler can process the response in any way. Note, modifications made to the response
persist and, therefore, affect default internal data and error processing.public Function<T,Object> getRawResponseHandler()
public T getOne()
T
JSON API object specified in the urlSuffix
, such as "/108"
.T
JSON API object specified in the urlSuffix
Same as calling:
getOne(String, Object, Format)
with getOne("", null, _format)
public T getOne(String urlSuffix)
T
JSON API object specified in the urlSuffix
, such as "/108"
.urlSuffix
- A suffix identifying the T
JSON API object to getOneT
JSON API object specified in the urlSuffix
Same as calling:
getOne(String, Object, Format)
with getOne(urlSuffix, null, _format)
public T getOne(Object arguments)
getOne(String, Object, Format)
with getOne("", arguments, _format)
public T getOne(String urlSuffix, Object arguments)
getOne(String, Object, Format)
with getOne(urlSuffix, arguments, _format)
public T getOne(String urlSuffix, Object arguments, Requester.Format format)
urlBase + urlSuffix
. arguments
, if non-null, is sent in the URL as
JSON encoded URL arguments.arguments
- A JSON value object, sent in the URL as JSON encoded arguments, nullableurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or
Bindings of String/JSON value)public IJsonList<T> getMany()
T
JSON API objects as a IJsonList<T>
.T
JSON API objects as a IJsonList<T>
Same as calling:
getMany(String, Object, Format)
with getMany("", null, _format)
public IJsonList<T> getMany(String urlSuffix)
getMany(String, Object, Format)
with getMany(urlSuffix, null, _format)
public IJsonList<T> getMany(Object arguments)
getMany(String, Object, Format)
with getMany("", arguments, _format)
public IJsonList<T> getMany(String urlSuffix, Object arguments)
getMany(String, Object, Format)
with getMany(urlSuffix, arguments, _format)
public IJsonList<T> getMany(String urlSuffix, Object arguments, Requester.Format format)
urlBase + urlSuffix
. arguments
, if non-null, is sent in the URL as
JSON encoded URL arguments.arguments
- A JSON value object, sent in the URL as JSON encoded arguments, nullableurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or
Bindings of String/JSON value)public <R> R postOne(T payload)
postOne(String, Object, Format)
with postOne("", payload, _format)
public <R> R postOne(String urlSuffix, T payload)
postOne(String, Object, Format)
with postOne(urlSuffix, payload, _format)
public <R> R postOne(String urlSuffix, T payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R postMany(List<T> payload)
postMany(String, List, Format)
with postMany("", payload, _format)
public <R> R postMany(String urlSuffix, List<T> payload)
postMany(String, List, Format)
with postMany(urlSuffix, payload, _format)
public <R> R postMany(String urlSuffix, List<T> payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R putOne(T payload)
putOne(String, Object, Format)
with putOne("", payload, _format)
public <R> R putOne(String urlSuffix, T payload)
putOne(String, Object, Format)
with putOne(urlSuffix, payload, _format)
public <R> R putOne(String urlSuffix, T payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R putMany(List<T> payload)
putMany(String, List, Format)
with putMany("", payload, _format)
public <R> R putMany(String urlSuffix, List<T> payload)
putMany(String, List, Format)
with putMany(urlSuffix, payload, _format)
public <R> R putMany(String urlSuffix, List<T> payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R patchOne(T payload)
patchOne(String, Object, Format)
with patchOne("", payload, _format)
public <R> R patchOne(String urlSuffix, T payload)
patchOne(String, Object, Format)
with patchOne(urlSuffix, payload, _format)
public <R> R patchOne(String urlSuffix, T payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R patchMany(List<T> payload)
patchMany(String, List, Format)
with patchMany("", payload, _format)
public <R> R patchMany(String urlSuffix, List<T> payload)
patchMany(String, List, Format)
with patchMany(urlSuffix, payload, _format)
public <R> R patchMany(String urlSuffix, List<T> payload, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent as JSON encoded
text in the request's message body.R
- The expected type of the responsepayload
- A JSON value object, sent as JSON encoded text in the request's message bodyurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)public <R> R delete(Object arguments)
delete(String, Object, Format)
with delete("", arguments, _format)
public <R> R delete(String urlSuffix)
delete(String, Object, Format)
with delete(urlSuffix, null, _format)
public <R> R delete(String urlSuffix, Object arguments)
delete(String, Object, Format)
with delete(urlSuffix, arguments, _format)
public <R> R delete(String urlSuffix, Object arguments, Requester.Format format)
urlBase + urlSuffix
. The payload
, if non-null, is sent in the URL as JSON
encoded URL arguments.R
- The expected type of the responsearguments
- A JSON value object, sent in the URL as JSON encoded arguments, nullableurlSuffix
- A suffix, such as "/108", nullableformat
- The expected format of the response. One of: Json
, Yaml
, Xml
, Csv
, or Plain
format
specified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)Copyright © 2024. All rights reserved.