/** * This file is auto generated from JavaDoc. Do not modify it manually. */ import type { String } from "../../types/java/lang/String"; import type { RequesterChainable } from "../../types/senselogic/sitevision/api/script/RequesterChainable"; /** * Script utility for handling data (typically JSON) from an external website. * *

* This utility supports the official (RFC2616 and RFC5789) request methods: GET, POST, HEAD, PUT, DELETE and PATCH - but not the * OPTIONS, TRACE or CONNECT methods. Redirects will only be followed for the GET and HEAD methods. *

* *

* URL:s with an international domain name (IDN) will be converted (ACE:d) automatically internally by this utility, * e.g. invoking get("http://www.ringsjömingel.se") will do a HTTP GET using "http://www.xn--ringsjmingel-9ib.se". *

* *
*

Requester Options

*

* Each supported method in this utility comes in two flavours: a base method with the URL only (see example above) * and a method that also allows additional options. These are the supported options: *

*

Request options

* * *

Response options

* *

* Some methods have additional options. Consult the documentation of the specific method. *

*
* *

Basic JSON GET example

*

* Below is a example of how to use the Requester utility to execute a basic GET and process the JSON response body. *

   require('Requester')
 *        .get('http://jsonplaceholder.typicode.com/posts')
 *        .done(function(result) {
 *           // GET succeeded, handle JSON response result
 *           var i, count = result.length, item;
 *           for (i = 0; i < count; i++) {
 *              item = result[i];
 *              out.println(item.id + ' - ' + item.title + '<br>');
 *              out.println('<small>' + item.body + '</small><br>');
 *           }
 *        })
 *        .fail(function(message) {
 *           // GET failed, handle error message
 *           out.println(message);
 *        });
* *

GET and POST examples with options

*

* Below is another example that also demonstrates the options usage and further callback arguments. *

   var requester = require('Requester'),
 *        options = {
 *           username: 'thomas',
 *           password: 'nordahl_123',
 *           preemptiveAuthentication: true,
 *           headers: {
 *              'X-custom-header': 'Custom value'
 *           },
 *           data: {
 *              query: 'football',
 *              category: 'all'
 *           }
 *        },
 *        multipartOptions = {
 *           contentType: 'multipart/form-data',
 *           files: {
 *              file: fileNode // sv:file, sv:temporaryFile or sv:image
 *           },
 *           data: {
 *              name: 'foo'
 *           }
 *        };
 *
 *     requester.get('https://arestfulsite.se/endpoint/requires/authentication/search', options)
 *        .done(function(result, statusCode, headers) {
 *           // GET succeeded, handle potential JSON response result appropriately
 *           if (statusCode === 204) {
 *              ...
 *           } else {
 *              ...
 *           }
 *           ...
 *        })
 *        .fail(function(message, status) {
 *           // GET failed, handle appropriately
 *           if (status.statusCode === 401) {
 *              ...
 *           }
 *           if (status.headers) {
 *              ...
 *           }
 *           if (status.body) {
 *              ...
 *           }
 *           ...
 *        });
 *
 *        requester.post('https://arestfulsite.se/endpoint', multipartOptions)
 *        ...
* *

Example of how to POST XML

*

* To send XML as a string entity, you must set the contentType option to "text/plain". You should typically also set the * Content-Type header to an appropriate value that the remote system will accept. (Remember - if no Content-Type header is set, * Requester will fallback to the type indicated by the contentType option - i.e. "text/plain" in this case). *

   var requester = require('Requester'),
 *        options = {
 *           dataType: 'text',
 *           contentType: 'text/plain',
 *           headers: {
 *              'Content-Type': 'application/xml; charset=UTF-8'
 *           },
 *           data: '<?xml version="1.0" encoding="UTF-8"?><message>Hello backend system</message>'
 *        };
 *
 *     requester.post('https://xmlsite.se/endpoint', options)
 *        .done(function(result, statusCode, headers) {
 *           // POST succeeded, handle XML string response result appropriately
 *           ...
 *        })
 *        .fail(function(message, status) {
 *           // POST failed, handle appropriately
 *           ...
 *        });
*

* Tip! Use XmlParserUtil to process the XML response string! *

* *

Example of how to make OAuth2 requests

*

* To make a request to a protected resource URL, you must set the oauth2 option to a valid sv:oAuth2Configuration. Special * considerations must be made regarding error handling. See error handling below. *

* *

Perform OAuth2 request as user

*
   var requester = require('Requester'),
 *        oAuth2Configuration = (...),
 *        options = {
 *           oauth2: oAuth2Configuration
 *        };
 *
 *     requester.get('https://resourceserver.com/protected-endpoint', options)
 *        .done(function(result, statusCode, headers) {
 *           // Request succeeded, handle response appropriately
 *           ...
 *        })
 *        .fail(function(message, status) {
 *           // Request failed, handle appropriately
 *           ...
 *        });
* *

Perform OAuth2 request as application

*
   var requester = require('Requester'),
 *        oAuth2Configuration = (...),
 *        options = {
 *           oauth2: oAuth2Configuration,
 *           oauth2Type: 'app'
 *        };
 *
 *     requester.get('https://resourceserver.com/protected-endpoint', options)
 *        .done(function(result, statusCode, headers) {
 *           // Request succeeded, handle response appropriately
 *           ...
 *        })
 *        .fail(function(message, status) {
 *           // Request failed, handle appropriately
 *           ...
 *        });
*

OAuth2 error handling

*

* Different resource servers returns errors in different format. However, a few common error situations might occur in every single request * and needs proper handling for a smooth user experience. *

* *

* * Tip! Use OAuth2 requireable util to perform OAuth2 operations. * *

* *

HTTP Connections

*

* Note that outgoing socket connections is a shared (and limited) server resource! * All requests will be handled by connections that comes from a shared http connection pool. * The pool has a fixed number of connections (typically 100) and specified timeouts (typically 5 seconds). * A request will fail if a connection can not be made at all (exhausted pool) or if the external * website doesn't respond in time (request timeout). A failure is typically handled via * {@link RequesterChainable#fail(Object)}. *

* *

* Important Requireable note! "JsonRequester" is available in all Sitevision versions since 4.2 but * "Requester" was introduced as of Sitevision 4.5.5. *

* *

* REST siblings note! *

* * @param script object * @param script function * @author Magnus Lövgren * @since Sitevision 4.2 * @see RestApi * @see senselogic.sitevision.api.script.app.RestAppInvoker */ export interface Requester { /** * Execute a HTTP GET. * @param aURL the url * @return a chainable requester */ get(aURL: String | string): RequesterChainable; /** * Execute a HTTP GET with options. * @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester */ get(aURL: String | string, aOptions: unknown): RequesterChainable; /** * Execute a HTTP PUT. * @param aURL the url * @return a chainable requester */ put(aURL: String | string): RequesterChainable; /** * Execute a HTTP PUT with options. * @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester */ put(aURL: String | string, aOptions: unknown): RequesterChainable; /** * Execute a HTTP POST. * @param aURL the url * @return a chainable requester */ post(aURL: String | string): RequesterChainable; /** * Execute a HTTP POST with options. * @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester */ post(aURL: String | string, aOptions: unknown): RequesterChainable; /** * Execute a HTTP DELETE. * @param aURL the url * @return a chainable requester */ delete(aURL: String | string): RequesterChainable; /** * Execute a HTTP DELETE with options. * *

aOptions extension for delete

* * @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester */ delete(aURL: String | string, aOptions: unknown): RequesterChainable; /** * Execute a HTTP PATCH. * *

* Note! The RFC5789 PATCH method is neither safe nor idempotent as defined by RFC2616. *

* @param aURL the url * @return a chainable requester * @since Sitevision 7.2.3 */ patch(aURL: String | string): RequesterChainable; /** * Execute a HTTP PATCH with options. * *

* Note! The RFC5789 PATCH method is neither safe nor idempotent as defined by RFC2616. *

* @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester * @since Sitevision 7.2.3 */ patch(aURL: String | string, aOptions: unknown): RequesterChainable; /** * Execute a HTTP HEAD. * *

* Note! The response of a HEAD request does not have a body. *

* @param aURL the url * @return a chainable requester * @since Sitevision 7.2.3 */ head(aURL: String | string): RequesterChainable; /** * Execute a HTTP HEAD with options. * *

* Note! The response of a HEAD request does not have a body. *

* @param aURL the url * @param aOptions the options, see options example above * @return a chainable requester * @since Sitevision 7.2.3 */ head(aURL: String | string, aOptions: unknown): RequesterChainable; } declare namespace Requester {} declare var requester: Requester; export default requester;