Class DynamicRequest
DynamicRequest
can be
used to configure the endpoint to which the request is sent, the request headers, path params, query params
and the request body.
An instance of DynamicRequest
can be created by either directly calling the constructor with an
ObjectSerializer
and HttpPipeline
or obtained from a service client that preconfigures known
components of the request like URL, path params etc.
To demonstrate how this class can be used to construct a request, let's use a Pet Store service as an example. The list of APIs available on this service are documented in the swagger definition.
Creating an instance of DynamicRequest using the constructor
ObjectSerializer serializer = JsonSerializerProviders.createInstance(true); HttpPipeline pipeline = new HttpPipelineBuilder().build(); DynamicRequest dynamicRequest = new DynamicRequest(serializer, pipeline);
An Azure service client may provide methods that are specific to the service which returns an instance
DynamicRequest
that comes preconfigured with some request components like the endpoint, required path
params, headers etc.
Configuring the request with a path param and making a HTTP GET request
Continuing with the pet store example, getting information about a pet requires making a HTTP GET call to the pet service and setting the pet id in path param as shown in the sample below.DynamicResponse response = dynamicRequest .setUrl("https://petstore.example.com/pet/{petId}") // may already be set if request is created from a client .setPathParam("petId", "2343245") .send(); // makes the service call
Configuring the request with JSON body and making a HTTP POST request
To add a new pet to the pet store, a HTTP POST call should be made to the service with the details of the pet that is to be added. The details of the pet are included as the request body in JSON format. The JSON structure for the request is defined as follows:
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
To create a concrete request, Json builder provided in javax package is used here for demonstration. However, any
other Json building library can be used to achieve similar results.
JsonArray photoUrls = Json.createArrayBuilder() .add("https://imgur.com/pet1") .add("https://imgur.com/pet2") .build(); JsonArray tags = Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("id", 0) .add("name", "Labrador") .build()) .add(Json.createObjectBuilder() .add("id", 1) .add("name", "2021") .build()) .build(); JsonObject requestBody = Json.createObjectBuilder() .add("id", 0) .add("name", "foo") .add("status", "available") .add("category", Json.createObjectBuilder().add("id", 0).add("name", "dog")) .add("photoUrls", photoUrls) .add("tags", tags) .build(); String requestBodyStr = requestBody.toString();Now, this string representation of the JSON request can be set as body of DynamicRequest
DynamicResponse response = dynamicRequest .setUrl("https://petstore.example.com/pet") // may already be set if request is created from a client .addHeader("Content-Type", "application/json") .setBody(requestBodyStr) .send(); // makes the service call
-
Constructor Summary
ConstructorsConstructorDescriptionDynamicRequest
(ObjectSerializer objectSerializer, HttpPipeline httpPipeline) Creates an instance of the Dynamic request. -
Method Summary
Modifier and TypeMethodDescriptionaddHeader
(HttpHeader httpHeader) Adds a header to the HTTP requestAdds a header to the HTTP request.addQueryParam
(String parameterName, String value) Adds a query parameter to the request URL.Returns theHttpPipeline
used for sending this request.Returns theObjectSerializer
used for serializing this request.send()
Sends the request through the HTTP pipeline synchronously.Sends the request through the HTTP pipeline synchronously.Sends the request through the HTTP pipeline asynchronously.Sets the body on the HTTP request.Sets the string representation of the request body.setHeaders
(HttpHeaders httpHeaders) Sets the headers on the HTTP request.setHttpMethod
(HttpMethod httpMethod) Sets the HTTP method for this request.setPathParam
(String parameterName, String value) Sets the value for a specific path parameter in the URL.Sets the URL for the HTTP request.
-
Constructor Details
-
DynamicRequest
Creates an instance of the Dynamic request. TheobjectSerializer
provided to this constructor will be used to serialize the request and thehttpPipeline
configured with a series ofHttp pipeline policies
will be applied before sending the request.- Parameters:
objectSerializer
- a serializer for serializing and deserializing payloadshttpPipeline
- the pipeline to send the actual HTTP request- Throws:
NullPointerException
- if either of objectSerializer or httpPipeline is null
-
-
Method Details
-
getObjectSerializer
Returns theObjectSerializer
used for serializing this request.- Returns:
- the underlying serializer used by this DynamicRequest
-
getHttpPipeline
Returns theHttpPipeline
used for sending this request.- Returns:
- the pipeline to sending HTTP requests used by this DynamicRequest
-
setUrl
Sets the URL for the HTTP request.- Parameters:
url
- the URL for the request- Returns:
- the modified DynamicRequest object
-
setHttpMethod
Sets the HTTP method for this request.- Parameters:
httpMethod
- the HTTP method for the request- Returns:
- the modified DynamicRequest object
-
addHeader
Adds a header to the HTTP request.- Parameters:
header
- the header keyvalue
- the header value- Returns:
- the modified DynamicRequest object
-
addHeader
Adds a header to the HTTP request- Parameters:
httpHeader
- the header to add- Returns:
- the modified DynamicRequest object
- Throws:
NullPointerException
- if the httpHeader is null
-
setHeaders
Sets the headers on the HTTP request. This overwrites all existing HTTP headers for this request.- Parameters:
httpHeaders
- the new headers to replace all existing headers- Returns:
- the modified DynamicRequest object
-
setBody
Sets the string representation of the request body. TheObjectSerializer
is not used if body is represented as string.- Parameters:
body
- the serialized body content- Returns:
- the modified DynamicRequest object
-
setBody
Sets the body on the HTTP request. The object is serialized usingObjectSerializer
provided in the constructor of this request.- Parameters:
body
- the body object that will be serialized- Returns:
- the modified DynamicRequest object
- Throws:
UncheckedIOException
- if the body cannot be serialized
-
setPathParam
Sets the value for a specific path parameter in the URL. The path parameter must be wrapped in a pair of curly braces, like "{paramName}".- Parameters:
parameterName
- the path parameter's name in the curly bracesvalue
- the String value to replace the path parameter- Returns:
- the modified DynamicRequest object
- Throws:
IllegalArgumentException
- if the parameterName is not found in the endpoint URL
-
addQueryParam
Adds a query parameter to the request URL.- Parameters:
parameterName
- the name of the query parametervalue
- the value of the query parameter- Returns:
- the modified DynamicRequest object
-
send
Sends the request through the HTTP pipeline synchronously.- Returns:
- the dynamic response received from the API
-
send
Sends the request through the HTTP pipeline synchronously.- Parameters:
context
- the context to send with the request- Returns:
- the dynamic response received from the API
-
sendAsync
Sends the request through the HTTP pipeline asynchronously.- Returns:
- the reactor publisher for the dynamic response to subscribe to
-