Class: HttpAgentImpl

HttpAgentImpl

Implementation of the HttpAgent interface with internal caching of completed and ongoing HTTP requests and cookie storage.


new HttpAgentImpl(proxy, cache, cookie, config)

Initializes the HTTP handler.

Parameters:
Name Type Description
proxy HttpProxy

The low-level HTTP proxy for sending the HTTP requests.

cache Cache

Cache to use for caching ongoing and completed requests.

cookie CookieStorage

The cookie storage to use internally.

config Object.<string, *>

Configuration of the HTTP handler for the current application environment, specifying the various default request option values and cache option values.

Members


_cache :Cache

Internal request cache, used to cache completed request results.

Type:

_cacheOptions :Object.<string, string>

Cache options.

Type:
  • Object.<string, string>

Cookie storage, used to keep track of cookies received from the server and send them with the subsequent requests to the server.

Type:

_defaultRequestOptions :Object

Default request options.

Type:
  • Object

_internalCacheOfPromises :Map.<string, Promise.<{status: number, body: *, params: {method: string, url: string, transformedUrl: string, data: Object.<string, (boolean|number|string)>}, headers: Object.<string, string>, cached: boolean}>>

Internal request cache, used to cache ongoing requests.

Type:
  • Map.<string, Promise.<{status: number, body: *, params: {method: string, url: string, transformedUrl: string, data: Object.<string, (boolean|number|string)>}, headers: Object.<string, string>, cached: boolean}>>

_proxy :HttpProxy

HTTP proxy, used to execute the HTTP requests.

Type:

Methods


_getCachedData(method, url, data)

Tests whether an ongoing or completed HTTP request for the specified URL and data is present in the internal cache and, if it is, the method returns a promise that resolves to the response body parsed as JSON.

The method returns null if no such request is present in the cache.

Parameters:
Name Type Description
method string

The HTTP method used by the request.

url string

The URL to which the request was made.

data Object.<string, (boolean|number|string|Date)>

The data sent to the server with the request.

Returns:

A promise that will resolve to the server response with the body parsed as JSON, or null if no such request is present in the cache.

Type
Promise.<HttpAgent~Response>

_getCacheKeySuffix(method, url, data)

Generates cache key suffix for an HTTP request to the specified URL with the specified data.

Parameters:
Name Type Description
method string

The HTTP method used by the request.

url string

The URL to which the request is sent.

data Object.<string, (boolean|number|string|Date)>

The data sent with the request.

Returns:

The suffix of a cache key to use for a request to the specified URL, carrying the specified data.

Type
string

_prepareOptions(options)

Prepares the provided request options object by filling in missing options with default values and addding extra options used internally.

Parameters:
Name Type Description
options HttpAgent~RequestOptions

Optional request options.

Returns:

Request options with set filled-in default values for missing fields, and extra options used internally.

Type
HttpAgent~RequestOptions

_proxyRejected(error)

Handles rejection of the HTTP request by the HTTP proxy. The method tests whether there are any remaining tries for the request, and if there are any, it attempts re-send the request.

The method rejects the internal request promise if there are no tries left.

Parameters:
Name Type Description
error GenericError

The error provided by the HttpProxy, carrying the error parameters, such as the request url, data, method, options and other useful data.

Returns:

A promise that will either resolve to a server's response (with the body parsed as JSON) if there are any tries left and the re-tried request succeeds, or rejects with an error containing details of the cause of the request's failure.

Type
Promise.<HttpAgent~Response>

_proxyResolved(response)

Handles successful completion of an HTTP request by the HTTP proxy.

The method also updates the internal cookie storage with the cookies received from the server.

Parameters:
Name Type Description
response HttpAgent~Response

Server response.

Returns:

The post-processed server response.

Type
HttpAgent~Response

_request(method, url, data [, options])

Sends a new HTTP request using the specified method to the specified url. The request will carry the provided data as query parameters if the HTTP method is GET, but the data will be sent as request body for any other request method.

Parameters:
Name Type Argument Description
method string

HTTP method to use.

url string

The URL to which the request is sent.

data Object.<string, (boolean|number|string|Date)>

The data sent with the request.

options HttpAgent~RequestOptions <optional>

Optional request options.

Returns:

A promise that resolves to the response with the body parsed as JSON.

Type
Promise.<HttpAgent~Response>

_requestWithCheckCache(method, url, data [, options])

Check cache and if data isnt available then make real request.

Parameters:
Name Type Argument Description
method string

The HTTP method to use.

url string

The URL to which the request should be sent.

data Object.<string, (boolean|number|string|Date)>

The data to send with the request.

options HttpAgent~RequestOptions <optional>

Optional request options.

Returns:

A promise that resolves to the response with body parsed as JSON.

Type
Promise.<HttpAgent~Response>

_saveAgentResponseToCache(agentResponse)

Saves the server response to the cache to be used as the result of the next request of the same properties.

Parameters:
Name Type Description
agentResponse HttpAgent~Response

The response of the server.


_setCookiesFromResponse(agentResponse)

Sets all cookies from the Set-Cookie response header to the cookie storage.

Parameters:
Name Type Description
agentResponse HttpAgent~Response

The response of the server.


clearDefaultHeaders()


delete()


get()


getCacheKey()


patch()


post()


put()


setDefaultHeader()