Package manifold.json.rt.api
Class Requester<T>
- java.lang.Object
-
- manifold.json.rt.api.Requester<T>
-
- Type Parameters:
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
This class defines methods to simplify making HTTP requests involved with basic REST API calls supporting GET, POST, PUT, PATCH, and DELETE and handles responses in JSON & JSON Schema, YAML, XML, CSV, or plain text. An instance of this class may be used for multiple get/post/etc. requests. Normally you use this class via the JSON APIrequest(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);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRequester.Format
-
Constructor Summary
Constructors Constructor Description Requester(String urlBase)Get an instance ofRequesterfrom a JSON API type eg.,User.request().Requester(String urlBase, Function<Object,Object> resultCoercer)Requester(Endpoint endpoint)Requester(Endpoint endpoint, Function<Object,Object> resultCoercer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> Rdelete(Object arguments)Same as calling:delete(String, Object, Format)withdelete("", arguments, _format)<R> Rdelete(String urlSuffix)Same as calling:delete(String, Object, Format)withdelete(urlSuffix, null, _format)<R> Rdelete(String urlSuffix, Object arguments)Same as calling:delete(String, Object, Format)withdelete(urlSuffix, arguments, _format)<R> Rdelete(String urlSuffix, Object arguments, Requester.Format format)Make an HTTP DELETE request tourlBase + urlSuffix.EndpointgetEndpoint()Requester.FormatgetFormat()Map<String,String>getHeaders()IJsonList<T>getMany()Uses HTTP GET for the complete list ofTJSON API objects as aIJsonList<T>.IJsonList<T>getMany(Object arguments)Same as calling:getMany(String, Object, Format)withgetMany("", arguments, _format)IJsonList<T>getMany(String urlSuffix)Same as calling:getMany(String, Object, Format)withgetMany(urlSuffix, null, _format)IJsonList<T>getMany(String urlSuffix, Object arguments)Same as calling:getMany(String, Object, Format)withgetMany(urlSuffix, arguments, _format)IJsonList<T>getMany(String urlSuffix, Object arguments, Requester.Format format)Make an HTTP GET request tourlBase + urlSuffix.TgetOne()Use HTTP GET for a singleTJSON API object specified in theurlSuffix, such as"/108".TgetOne(Object arguments)Same as calling:getOne(String, Object, Format)withgetOne("", arguments, _format)TgetOne(String urlSuffix)Use HTTP GET for a singleTJSON API object specified in theurlSuffix, such as"/108".TgetOne(String urlSuffix, Object arguments)Same as calling:getOne(String, Object, Format)withgetOne(urlSuffix, arguments, _format)TgetOne(String urlSuffix, Object arguments, Requester.Format format)Make an HTTP GET request tourlBase + urlSuffix.Map<String,String>getParameters()Function<T,Object>getRawResponseHandler()intgetTimeout()<R> RpatchMany(String urlSuffix, List<T> payload)Same as calling:patchMany(String, List, Format)withpatchMany(urlSuffix, payload, _format)<R> RpatchMany(String urlSuffix, List<T> payload, Requester.Format format)Make an HTTP PATCH request tourlBase + urlSuffix.<R> RpatchMany(List<T> payload)Same as calling:patchMany(String, List, Format)withpatchMany("", payload, _format)<R> RpatchOne(String urlSuffix, T payload)Same as calling:patchOne(String, Object, Format)withpatchOne(urlSuffix, payload, _format)<R> RpatchOne(String urlSuffix, T payload, Requester.Format format)Make an HTTP PATCH request tourlBase + urlSuffix.<R> RpatchOne(T payload)Same as calling:patchOne(String, Object, Format)withpatchOne("", payload, _format)<R> RpostMany(String urlSuffix, List<T> payload)Same as calling:postMany(String, List, Format)withpostMany(urlSuffix, payload, _format)<R> RpostMany(String urlSuffix, List<T> payload, Requester.Format format)Make an HTTP POST request tourlBase + urlSuffix.<R> RpostMany(List<T> payload)Same as calling:postMany(String, List, Format)withpostMany("", payload, _format)<R> RpostOne(String urlSuffix, T payload)Same as calling:postOne(String, Object, Format)withpostOne(urlSuffix, payload, _format)<R> RpostOne(String urlSuffix, T payload, Requester.Format format)Make an HTTP POST request tourlBase + urlSuffix.<R> RpostOne(T payload)Same as calling:postOne(String, Object, Format)withpostOne("", payload, _format)<R> RputMany(String urlSuffix, List<T> payload)Same as calling:putMany(String, List, Format)withputMany(urlSuffix, payload, _format)<R> RputMany(String urlSuffix, List<T> payload, Requester.Format format)Make an HTTP PUT request tourlBase + urlSuffix.<R> RputMany(List<T> payload)Same as calling:putMany(String, List, Format)withputMany("", payload, _format)<R> RputOne(String urlSuffix, T payload)Same as calling:putOne(String, Object, Format)withputOne(urlSuffix, payload, _format)<R> RputOne(String urlSuffix, T payload, Requester.Format format)Make an HTTP PUT request tourlBase + urlSuffix.<R> RputOne(T payload)Same as calling:putOne(String, Object, Format)withputOne("", payload, _format)Requester<T>withAuthorization(String tokenType, String accessToken)Requester<T>withBasicAuthorization(String username, String password)Set the Basic Authorization header using the providedusernameandpasswordRequester<T>withBearerAuthorization(String accessToken)Set the Bearer Authorization header using the providedaccessToken.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.
-
-
-
Constructor Detail
-
Requester
public Requester(String urlBase)
Get an instance ofRequesterfrom a JSON API type eg.,User.request(). Requester is a builder type: you can configure the requests you'll make usingwithXxx()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);- Parameters:
urlBase- A URL providing HTTP services forT, such as "http://example.com/users"
-
Requester
public Requester(Endpoint endpoint)
-
-
Method Detail
-
getEndpoint
public Endpoint getEndpoint()
-
getFormat
public Requester.Format getFormat()
-
getTimeout
public int getTimeout()
-
withResponseFormat
public Requester<T> withResponseFormat(Requester.Format format)
Set the default format expected in the response. The response will be parsed according to this setting.- Parameters:
format- Json, Yaml, Xml, Csv, or Plain text. Default is Json.
-
withHeader
public Requester<T> withHeader(String name, String value)
Set an HTTP request headername : valuepair See-
withParam
public Requester<T> withParam(String name, String value)
Add aname=valueparameter to the request URL.
-
withBasicAuthorization
public Requester<T> withBasicAuthorization(String username, String password)
Set the Basic Authorization header using the providedusernameandpassword
-
withBearerAuthorization
public Requester<T> withBearerAuthorization(String accessToken)
Set the Bearer Authorization header using the providedaccessToken. For instance, if using OAuth,accessTokenis the token response from:curl -d "grant_type=password&client_id=[...]&client_secret=[...]&username=[...]&password=[...]" https://[domain]/[oauth-service]
-
withTimeout
public Requester<T> withTimeout(int timeout)
The connection timeout setting in milliseconds. If the timeout expires before the connection can be established, a {@link java.net.SocketTimeoutException) is thrown. A value of zero is interpreted as an infinite timeout, this is the default setting.
-
withRawResponseHandler
public Requester<T> withRawResponseHandler(Function<T,Object> handler)
- Parameters:
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.
-
getRawResponseHandler
public Function<T,Object> getRawResponseHandler()
- Returns:
- The raw response handler or null if one is not assigned.
-
getOne
public T getOne()
Use HTTP GET for a singleTJSON API object specified in theurlSuffix, such as"/108".- Returns:
- A single
TJSON API object specified in theurlSuffixSame as calling:getOne(String, Object, Format)withgetOne("", null, _format)
-
getOne
public T getOne(String urlSuffix)
Use HTTP GET for a singleTJSON API object specified in theurlSuffix, such as"/108".- Parameters:
urlSuffix- A suffix identifying theTJSON API object to getOne- Returns:
- A single
TJSON API object specified in theurlSuffixSame as calling:getOne(String, Object, Format)withgetOne(urlSuffix, null, _format)
-
getOne
public T getOne(Object arguments)
Same as calling:getOne(String, Object, Format)withgetOne("", arguments, _format)
-
getOne
public T getOne(String urlSuffix, Object arguments)
Same as calling:getOne(String, Object, Format)withgetOne(urlSuffix, arguments, _format)
-
getOne
public T getOne(String urlSuffix, Object arguments, Requester.Format format)
Make an HTTP GET request tourlBase + urlSuffix.arguments, if non-null, is sent in the URL as JSON encoded URL arguments.- Parameters:
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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
getMany
public IJsonList<T> getMany()
Uses HTTP GET for the complete list ofTJSON API objects as aIJsonList<T>.- Returns:
- The complete list of
TJSON API objects as aIJsonList<T>Same as calling:getMany(String, Object, Format)withgetMany("", null, _format)
-
getMany
public IJsonList<T> getMany(String urlSuffix)
Same as calling:getMany(String, Object, Format)withgetMany(urlSuffix, null, _format)
-
getMany
public IJsonList<T> getMany(Object arguments)
Same as calling:getMany(String, Object, Format)withgetMany("", arguments, _format)
-
getMany
public IJsonList<T> getMany(String urlSuffix, Object arguments)
Same as calling:getMany(String, Object, Format)withgetMany(urlSuffix, arguments, _format)
-
getMany
public IJsonList<T> getMany(String urlSuffix, Object arguments, Requester.Format format)
Make an HTTP GET request tourlBase + urlSuffix.arguments, if non-null, is sent in the URL as JSON encoded URL arguments.- Parameters:
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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
postOne
public <R> R postOne(T payload)
Same as calling:postOne(String, Object, Format)withpostOne("", payload, _format)
-
postOne
public <R> R postOne(String urlSuffix, T payload)
Same as calling:postOne(String, Object, Format)withpostOne(urlSuffix, payload, _format)
-
postOne
public <R> R postOne(String urlSuffix, T payload, Requester.Format format)
Make an HTTP POST request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
postMany
public <R> R postMany(List<T> payload)
Same as calling:postMany(String, List, Format)withpostMany("", payload, _format)
-
postMany
public <R> R postMany(String urlSuffix, List<T> payload)
Same as calling:postMany(String, List, Format)withpostMany(urlSuffix, payload, _format)
-
postMany
public <R> R postMany(String urlSuffix, List<T> payload, Requester.Format format)
Make an HTTP POST request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
putOne
public <R> R putOne(T payload)
Same as calling:putOne(String, Object, Format)withputOne("", payload, _format)
-
putOne
public <R> R putOne(String urlSuffix, T payload)
Same as calling:putOne(String, Object, Format)withputOne(urlSuffix, payload, _format)
-
putOne
public <R> R putOne(String urlSuffix, T payload, Requester.Format format)
Make an HTTP PUT request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
putMany
public <R> R putMany(List<T> payload)
Same as calling:putMany(String, List, Format)withputMany("", payload, _format)
-
putMany
public <R> R putMany(String urlSuffix, List<T> payload)
Same as calling:putMany(String, List, Format)withputMany(urlSuffix, payload, _format)
-
putMany
public <R> R putMany(String urlSuffix, List<T> payload, Requester.Format format)
Make an HTTP PUT request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
patchOne
public <R> R patchOne(T payload)
Same as calling:patchOne(String, Object, Format)withpatchOne("", payload, _format)
-
patchOne
public <R> R patchOne(String urlSuffix, T payload)
Same as calling:patchOne(String, Object, Format)withpatchOne(urlSuffix, payload, _format)
-
patchOne
public <R> R patchOne(String urlSuffix, T payload, Requester.Format format)
Make an HTTP PATCH request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
patchMany
public <R> R patchMany(List<T> payload)
Same as calling:patchMany(String, List, Format)withpatchMany("", payload, _format)
-
patchMany
public <R> R patchMany(String urlSuffix, List<T> payload)
Same as calling:patchMany(String, List, Format)withpatchMany(urlSuffix, payload, _format)
-
patchMany
public <R> R patchMany(String urlSuffix, List<T> payload, Requester.Format format)
Make an HTTP PATCH request tourlBase + urlSuffix. Thepayload, if non-null, is sent as JSON encoded text in the request's message body.- Type Parameters:
R- The expected type of the response- Parameters:
payload- 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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
delete
public <R> R delete(Object arguments)
Same as calling:delete(String, Object, Format)withdelete("", arguments, _format)
-
delete
public <R> R delete(String urlSuffix)
Same as calling:delete(String, Object, Format)withdelete(urlSuffix, null, _format)
-
delete
public <R> R delete(String urlSuffix, Object arguments)
Same as calling:delete(String, Object, Format)withdelete(urlSuffix, arguments, _format)
-
delete
public <R> R delete(String urlSuffix, Object arguments, Requester.Format format)
Make an HTTP DELETE request tourlBase + urlSuffix. Thepayload, if non-null, is sent in the URL as JSON encoded URL arguments.- Type Parameters:
R- The expected type of the response- Parameters:
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, orPlain- Returns:
- A JSON value parsed from the
formatspecified encoded response (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
-
-
-