Package com.ibm.cloud.sdk.core.http
Class RequestBuilder
- java.lang.Object
-
- com.ibm.cloud.sdk.core.http.RequestBuilder
-
public class RequestBuilder extends java.lang.Object
Convenience class for constructing HTTP/HTTPS requests.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestBuilder
body(okhttp3.RequestBody body)
Sets the body.RequestBuilder
bodyContent(java.io.InputStream stream, java.lang.String contentType)
Sets the file content (InputStream) to the request (used with POST/PUT).RequestBuilder
bodyContent(java.lang.String contentType, java.lang.Object jsonContent, java.lang.Object jsonPatchContent, java.io.InputStream nonJsonContent)
Sets the request body content from one of three different sources.RequestBuilder
bodyContent(java.lang.String contentType, java.lang.Object jsonContent, java.lang.Object jsonPatchContent, java.lang.String nonJsonContent)
Sets the request body content from one of three different sources.RequestBuilder
bodyContent(java.lang.String content, java.lang.String contentType)
Sets the string content to the request (used with POST/PUT).RequestBuilder
bodyJson(com.google.gson.JsonObject json)
Adds a JSON content to the request (used with POST/PUT).RequestBuilder
bodyJson(com.google.gson.JsonObject json, java.lang.String mediaType)
Adds a JSON content to the request (used with POST/PUT/PATCH).okhttp3.Request
build()
Builds the request.static okhttp3.HttpUrl
constructHttpUrl(java.lang.String serviceUrl, java.lang.String[] pathSegments)
Creates a properly encoded HttpUrl object with no path parameters.static okhttp3.HttpUrl
constructHttpUrl(java.lang.String serviceUrl, java.lang.String[] pathSegments, java.lang.String[] pathParameters)
Creates a properly encoded HttpUrl object with path parameters.static RequestBuilder
delete(okhttp3.HttpUrl url)
The DELETE method requests that the origin server delete the resource identified by the Request-URI.RequestBuilder
form(java.lang.Object... args)
Adds form parameters.static RequestBuilder
get(okhttp3.HttpUrl url)
The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.static RequestBuilder
head(okhttp3.HttpUrl url)
The HEAD method means retrieve the headers for the resource identified by the Request-URI.RequestBuilder
header(java.lang.Object... args)
Adds header parameters.static RequestBuilder
patch(okhttp3.HttpUrl url)
The PUT method requests that the enclosed entity be stored under the supplied Request-URI.static RequestBuilder
post(okhttp3.HttpUrl url)
The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage.static RequestBuilder
put(okhttp3.HttpUrl url)
The PUT method requests that the enclosed entity be stored under the supplied Request-URI.RequestBuilder
query(java.lang.Object... args)
Adds query parameters.static okhttp3.HttpUrl
resolveRequestUrl(java.lang.String serviceUrl, java.lang.String path)
Similar to the three-arg version of resolveRequestUrl, but supports a "path" parameter with no path param references.static okhttp3.HttpUrl
resolveRequestUrl(java.lang.String serviceUrl, java.lang.String path, java.util.Map<java.lang.String,java.lang.String> pathParams)
Resolves a request URL by first resolving path parameter references within "path", then it combines the serviceUrl portion with the resolved path string.java.lang.String
toString()
java.lang.String
toUrl()
Return the request url including query parameters.
-
-
-
Method Detail
-
delete
public static RequestBuilder delete(okhttp3.HttpUrl url)
The DELETE method requests that the origin server delete the resource identified by the Request-URI.- Parameters:
url
- the URL- Returns:
- this
-
get
public static RequestBuilder get(okhttp3.HttpUrl url)
The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.- Parameters:
url
- the URL- Returns:
- this
-
post
public static RequestBuilder post(okhttp3.HttpUrl url)
The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage. It is often used when uploading a file or submitting a completed web form.- Parameters:
url
- the URL- Returns:
- this
-
put
public static RequestBuilder put(okhttp3.HttpUrl url)
The PUT method requests that the enclosed entity be stored under the supplied Request-URI.- Parameters:
url
- the URL- Returns:
- this
-
patch
public static RequestBuilder patch(okhttp3.HttpUrl url)
The PUT method requests that the enclosed entity be stored under the supplied Request-URI.- Parameters:
url
- the URL- Returns:
- this
-
head
public static RequestBuilder head(okhttp3.HttpUrl url)
The HEAD method means retrieve the headers for the resource identified by the Request-URI.- Parameters:
url
- the URL- Returns:
- this
-
constructHttpUrl
public static okhttp3.HttpUrl constructHttpUrl(java.lang.String serviceUrl, java.lang.String[] pathSegments)
Creates a properly encoded HttpUrl object with no path parameters.- Parameters:
serviceUrl
- the base service URL associated with the service instancepathSegments
- the path segments for a specific API call- Returns:
- the HttpUrl object
-
constructHttpUrl
public static okhttp3.HttpUrl constructHttpUrl(java.lang.String serviceUrl, java.lang.String[] pathSegments, java.lang.String[] pathParameters)
Creates a properly encoded HttpUrl object with path parameters.- Parameters:
serviceUrl
- the base service URL associated with the service instancepathSegments
- the path segments for a specific API callpathParameters
- the path parameters for a specific API call- Returns:
- the HttpUrl object
-
resolveRequestUrl
public static okhttp3.HttpUrl resolveRequestUrl(java.lang.String serviceUrl, java.lang.String path, java.util.Map<java.lang.String,java.lang.String> pathParams)
Resolves a request URL by first resolving path parameter references within "path", then it combines the serviceUrl portion with the resolved path string.- Parameters:
serviceUrl
- the base URL associated with a service instancepath
- the unresolved pathpathParams
- a map containing path parameters, keyed by the parameter name- Returns:
- an HttpUrl instance that holds the request URL
-
resolveRequestUrl
public static okhttp3.HttpUrl resolveRequestUrl(java.lang.String serviceUrl, java.lang.String path)
Similar to the three-arg version of resolveRequestUrl, but supports a "path" parameter with no path param references.- Parameters:
serviceUrl
- the base URL associated with a service instancepath
- the path string- Returns:
- an HttpUrl instance that holds the request URL
-
build
public okhttp3.Request build()
Builds the request.- Returns:
- the request
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
toUrl
public java.lang.String toUrl()
Return the request url including query parameters.- Returns:
- the string
-
body
public RequestBuilder body(okhttp3.RequestBody body)
Sets the body.- Parameters:
body
- the body- Returns:
- the request builder
-
bodyContent
public RequestBuilder bodyContent(java.lang.String content, java.lang.String contentType)
Sets the string content to the request (used with POST/PUT). This will encapsulate the string into aRequestBody
encoded with UTF-8- Parameters:
content
- the content to POST/PUTcontentType
- the HTTP contentType to use.- Returns:
- this
-
bodyContent
public RequestBuilder bodyContent(java.io.InputStream stream, java.lang.String contentType)
Sets the file content (InputStream) to the request (used with POST/PUT).- Parameters:
stream
- the InputStream to read the request body content fromcontentType
- the contentType associated with the data read from the InputStream- Returns:
- this
-
bodyContent
public RequestBuilder bodyContent(java.lang.String contentType, java.lang.Object jsonContent, java.lang.Object jsonPatchContent, java.io.InputStream nonJsonContent)
Sets the request body content from one of three different sources. The three input sources are used in this precedence order:- If 'jsonContent' is not null, then use that.
- If 'jsonPatchContent' is not null, then use that.
- Else use 'nonJsonContent'.
- Parameters:
contentType
- the value of the "Content-Type" header associated with the request bodyjsonContent
- a model instance to be serialized and used for the request bodyjsonPatchContent
- a collection of JsonPatchOperation instances to be serialized and used for the request bodynonJsonContent
- an InputStream whose contents should be used directly as the request body- Returns:
- this
-
bodyContent
public RequestBuilder bodyContent(java.lang.String contentType, java.lang.Object jsonContent, java.lang.Object jsonPatchContent, java.lang.String nonJsonContent)
Sets the request body content from one of three different sources. The three input sources are used in this precedence order:- If 'jsonContent' is not null, then use that.
- If 'jsonPatchContent' is not null, then use that.
- Else use 'nonJsonContent'.
- Parameters:
contentType
- the value of the "Content-Type" header associated with the request bodyjsonContent
- a model instance to be serialized and used for the request bodyjsonPatchContent
- a collection of JsonPatchOperation instances to be serialized and used for the request bodynonJsonContent
- a string to be used directly as the request body- Returns:
- this
-
bodyJson
public RequestBuilder bodyJson(com.google.gson.JsonObject json)
Adds a JSON content to the request (used with POST/PUT). This will encapsulate the json into aRequestBody
encoded with UTF-8 and use"application/json"
as Content-Type- Parameters:
json
- the JsonObject json- Returns:
- this
-
bodyJson
public RequestBuilder bodyJson(com.google.gson.JsonObject json, java.lang.String mediaType)
Adds a JSON content to the request (used with POST/PUT/PATCH). This will encapsulate the json into aRequestBody
encoded with UTF-8 and use"application/json"
as Content-Type- Parameters:
json
- the JsonObject jsonmediaType
- the contentType value- Returns:
- this
-
form
public RequestBuilder form(java.lang.Object... args)
Adds form parameters.- Parameters:
args
- a list of name-value form parameters- Returns:
- this
-
header
public RequestBuilder header(java.lang.Object... args)
Adds header parameters.- Parameters:
args
- a list of name-value headers- Returns:
- this
-
query
public RequestBuilder query(java.lang.Object... args)
Adds query parameters.- Parameters:
args
- a list of name-value query parameters- Returns:
- this
-
-