Class 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.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 instance
        pathSegments - 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 instance
        pathSegments - the path segments for a specific API call
        pathParameters - 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 instance
        path - the unresolved path
        pathParams - 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 instance
        path - 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 class java.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 a RequestBody encoded with UTF-8
        Parameters:
        content - the content to POST/PUT
        contentType - 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 from
        contentType - 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:
        1. If 'jsonContent' is not null, then use that.
        2. If 'jsonPatchContent' is not null, then use that.
        3. Else use 'nonJsonContent'.
        Parameters:
        contentType - the value of the "Content-Type" header associated with the request body
        jsonContent - a model instance to be serialized and used for the request body
        jsonPatchContent - a collection of JsonPatchOperation instances to be serialized and used for the request body
        nonJsonContent - 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:
        1. If 'jsonContent' is not null, then use that.
        2. If 'jsonPatchContent' is not null, then use that.
        3. Else use 'nonJsonContent'.
        Parameters:
        contentType - the value of the "Content-Type" header associated with the request body
        jsonContent - a model instance to be serialized and used for the request body
        jsonPatchContent - a collection of JsonPatchOperation instances to be serialized and used for the request body
        nonJsonContent - 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 a RequestBody 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 a RequestBody encoded with UTF-8 and use "application/json" as Content-Type
        Parameters:
        json - the JsonObject json
        mediaType - 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