Package com.tccc.kos.commons.web.api
Class ApiRequest<T>
java.lang.Object
com.tccc.kos.commons.web.api.ApiRequest<T>
Data class used by ApiClient to make API calls to local and remote nodes.
If the
ApiClient
doesn't directly support the method and/or options you require,
then use this class to build an exact request.
Data Fields
The following fields comprise theApiRequest
:
private String node; // the destination node name; use {@code null} for local
private RequestMethod method; // the HTTP method to use (default is GET)
private String url; // the URL to send the request to
private Map<String, String> headers; // headers [name/value pairs] (optional)
private Object body; // the body to include in the request (optional)
private Class<T> responseClass; // type to convert the response to (optional)
private int timeout; // timeout, in msec (optional); <= 0 means "use default"
Examples
A GET request that returns a string
public class MyClass {
@Autowired
private ApiClient apiClient;
public void someMethod() {
ApiRequest<String> apiRequest = new ApiRequest();
apiRequest.setUrl("https://myserver.com/apiv2/path/to/resource?param=value");
apiRequest.setResponseClass(String.class);
apiClient.api(apiRequest);
}
}
A POST request with body and response objects
public class MyClass {
@Autowired
private ApiClient apiClient;
public void someMethod() {
ApiRequest<ResponseData> apiRequest = new ApiRequest();
apiRequest.setNode("Primary")
.setMethod(RequestMethod.POST)
.setUrl("https://myserver.com/apiv2/path/to/resource?param=value");
.addHeader("header1", "value1")
.setTrace("XYZ")
.setBody(sourceDataObject)
.setResonseClass(ResponseData.class)
.setTimeout(3000);
apiClient.api(apiRequest);
}
}
- Since:
- 1.0
- Version:
- 2022-09-30
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newApiRequest
in which all data fields arenull
except the requestmethod
field, which is initialized to GET. -
Method Summary
Modifier and TypeMethodDescriptionAdds the specified header to the message.addHeaders
(Map<String, Object> headers) Adds all theheaders
from the specified map.getAlias()
Return the currently configured destination alias.getBody()
Returns the request body.Returns the header with the specified name.Returns the map of all the headers.Returns the HTTP request method.getNode()
Returns the name of the destination node.Returns the response body.int
Returns the timeout value, in msec.getTrace()
Returns the "trace" header.getUrl()
Returns the URL.boolean
Determines if the specified header exists.removeHeader
(String name) Removes the specified header.Set the router alias of the destination.Returnsthis
.setHeaders
(Map<String, String> headers) Returnsthis
.setMethod
(RequestMethod method) Returnsthis
.Returnsthis
.Flags the request as being strictly ordered.setResponseClass
(Class<T> responseClass) Returnsthis
.setTimeout
(int timeout) Returnsthis
.Sets the "trace" header for the message.Returnsthis
.
-
Constructor Details
-
ApiRequest
public ApiRequest()Constructs a newApiRequest
in which all data fields arenull
except the requestmethod
field, which is initialized to GET.
-
-
Method Details
-
getNode
Returns the name of the destination node.- Returns:
- the name of the destination node, or
null
if none
-
setNode
Returnsthis
.- Parameters:
node
- the id of the destination node- Returns:
this
-
setAlias
Set the router alias of the destination.setNode()
calls this with the specifiedNodeId
. Calling this directly will clear the value ofnode
. This can be used with router aliases instead of nodeId's.- Parameters:
addr
- the websocket router address of the destination
-
getAlias
Return the currently configured destination alias. -
getMethod
Returns the HTTP request method.- Returns:
- the HTTP request method, or
null
if none
-
setMethod
Returnsthis
.- Parameters:
method
- the HTTP request method- Returns:
this
-
getUrl
Returns the URL.- Returns:
- the URL, or
null
if none
-
setUrl
Returnsthis
.- Parameters:
url
- the URL string to set- Returns:
this
-
getBody
Returns the request body.- Returns:
- the request body, or
null
if none
-
setBody
Returnsthis
.- Parameters:
body
- the request body- Returns:
this
-
getResponseClass
Returns the response body.- Returns:
- the response body, or
null
if none
-
setResponseClass
Returnsthis
.- Parameters:
responseClass
- the response body class- Returns:
this
-
getTimeout
public int getTimeout()Returns the timeout value, in msec.- Returns:
- the timeout value (msec);
<= 0
means "use default"
-
setTimeout
Returnsthis
.- Parameters:
timeout
- the timeout value (msec) to use;<= 0
means "use default"- Returns:
this
-
getTrace
Returns the "trace" header.- Returns:
- the trace value, or
null
if it doesn't exist
-
setTrace
Sets the "trace" header for the message. This causes the router infrastructure code to log data about this message. It does this by adding a header named "trace" whose value is given in thetrace
parameter.- Parameters:
trace
- the trace string to put into the log statements
-
setOrdered
Flags the request as being strictly ordered. It does this by adding a header named "ordered" whose value istrue
. -
getHeader
Returns the header with the specified name.- Parameters:
name
- the header name- Returns:
- the value of the header, or
null
if no header with that name
-
hasHeader
Determines if the specified header exists.- Parameters:
name
- the header name- Returns:
true
if the header exists, otherwisefalse
-
getHeaders
Returns the map of all the headers.- Returns:
- the map containing the headers; never returns
null
-
setHeaders
Returnsthis
.- Parameters:
headers
- map of all name/value headers; can benull
- Returns:
this
-
addHeader
Adds the specified header to the message. If thevalue
is null, then the header is removed. The value is converted to its string equivlent using itstoString()
method.- Parameters:
name
- the header's namevalue
- the header's value- Returns:
this
-
addHeaders
Adds all theheaders
from the specified map. All values are converted to their string equivalent using theirtoString()
method.- Parameters:
headers
- a map of headers- Returns:
this
-
removeHeader
Removes the specified header.- Parameters:
name
- the name of the header to remove- Returns:
- the previous value associated with
name
, ornull
if there was no mapping forname
-