client REST

2 minute read

This class provides methods required to support a simple rest client for various HTTP interactions. Class is a configuration-driven wrapper for the groovyx.net.http.HTTPBuilder

The following is an example of its usage.

import com.cloudbees.flowpdf.* import com.cloudbees.flowpdf.client.* ... def stepGetContent(StepParameters parameters, StepResult sr) { // Retrieving context object Context context = getContext() // Creating new REST object from the plugin configuration // Base URI will be set to the 'endpoint' configuration value REST rest = context.newRESTClient() // Creating new HTTPRequest object using FlowPDF APIs HTTPRequest request = rest.newRequest([method : 'GET', path: '/api/v2/query']) // Performing request and getting the response. // Entity encoding will be performed by the HTTPBuilder def response = rest.doRequest(request) // Printing response content println("Content: " + response.toString()); } ...

REST(Map<String, Object> params)

Class Constructor. Creates new REST object.

To use REST with a proxy, provide a Map with a proxy parameters to the constructor:

  • url

    Address of the proxy to be used as an http proxy.

  • username

    username for proxy authorization.

  • password

    password for proxy authorization.

Parameters

  • (Optional)(Map<String, Object> ref) Map with the REST object configuration.

Returns

REST

Usage

import com.cloudbees.flowpdf.client.REST REST rest = new REST([ proxy : [ url : 'http://squid:3128', username : 'user1', password : 'user2' ] ]);

newRequest(Map<String, Object> requestParameters)

Creates new HTTPRequest template object.

This wrapper has been created to help with a conditional configuration of the request. REST will apply additional transformations (e.g. adding the proxy authorization header ) before the request execution. This method has the same interface and usage as new HTTPRequest()

Parameters

  • (Required) (Map<String, Object>) parameters for the the request template.

    • (Required) (String|groovyx.net.http.Method) method - Name of the request method.

    • (Optional) (String|groovyx.net.http.ContentType) contentType - Name of the request content type. Will be used for both Accept and Content-Type headers. JSON is used by default.

    • (Optional) (String) path - Path part of the URI

    • (Optional) (String) content - Request entity body.

    • (Optional) (Map<String, String>) query - query parameters.

    • (Optional) (Map<String, String>) headers - HTTP headers for the request.

Returns

HTTPRequest instance.

Usage

HTTPRequest request = rest.newRequest([method : 'GET, path : 'https://electric-cloud.com'])

doRequest(HTTPRequest httpRequest)

Performs HTTP request, using HTTPRequest object as parameter, leveraging the HTTPBuilder method.

Parameters

  • (Required)(HTTPRequest) predefined HTTPRequest instance

Returns

(Object) Encoded entity of the of the request result. Actual class of the response depends on the content type. If server responded with a JSON the result will be a Map/List representation of the response.

Usage

import com.cloudbees.flowpdf.client.* def request = rest.newRequest([ method: 'GET', contentType: 'JSON', path: '/api/v2/query', query: [ search : parametersMap['searchQuery']], ) if (parametersMap['limitResults']){ request.setQueryParameter('limitResults', 50) } def response = rest.doRequest(request) println(response['results'][0].toString())

request(Map<String, Object> requestParameters)

This is a shorthand method for the subsequent newRequest() and doRequest() calls. All parameters are the same as newRequest()

Parameters

  • (Required) (Map<String, Object>) parameters for the the request template.

    • (Required) (String|groovyx.net.http.Method) method - Name of the request method.

    • (Optional) (String|groovyx.net.http.ContentType) contentType - Name of the request content type. Will be used for both Accept and Content-Type headers. JSON is used by default.

    • (Optional) (String) path - Path part of the URI

    • (Optional) (String) content - Request entity body.

    • (Optional) (Map<String, String>) query - query parameters.

    • (Optional) (Map<String, String>) headers - HTTP headers for the request.

Returns

(Object) Encoded response representation.

Usage

def response = rest.request([method : 'GET', path : '/api/v2/objects/2'])