HttpAgentImpl
Implementation of the HttpAgent
interface with internal caching
of completed and ongoing HTTP requests and cookie storage.
Kind: global class
- HttpAgentImpl
- new HttpAgentImpl(proxy, cache, cookie, config)
- ._proxy :
HttpProxy
- ._cache :
Cache
- ._cookie :
CookieStorage
- ._cacheOptions :
Object.<string, string>
- ._defaultRequestOptions :
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}>>
- .get()
- .post()
- .put()
- .patch()
- .delete()
- .getCacheKey()
- .setDefaultHeader()
- .clearDefaultHeaders()
- ._requestWithCheckCache(method, url, data, [options]) ⇒
Promise.<HttpAgent~Response>
- ._getCachedData(method, url, data) ⇒
Promise.<HttpAgent~Response>
- ._request(method, url, data, [options]) ⇒
Promise.<HttpAgent~Response>
- ._proxyResolved(response) ⇒
HttpAgent~Response
- ._proxyRejected(error) ⇒
Promise.<HttpAgent~Response>
- ._prepareOptions(options) ⇒
HttpAgent~RequestOptions
- ._getCacheKeySuffix(method, url, data) ⇒
string
- ._setCookiesFromResponse(agentResponse)
- ._saveAgentResponseToCache(agentResponse)
new HttpAgentImpl(proxy, cache, cookie, config)
Initializes the HTTP handler.
Param | 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. |
Example
http
.get('url', { data: data }, {
ttl: 2000,
repeatRequest: 1,
withCredentials: true,
timeout: 2000,
accept: 'application/json',
language: 'en',
listeners: { 'progress': callbackFunction }
})
.then((response) => {
//resolve
}
.catch((error) => {
//catch
});
Example
http
.setDefaultHeader('Accept-Language', 'en')
.clearDefaultHeaders();
httpAgentImpl._proxy : HttpProxy
HTTP proxy, used to execute the HTTP requests.
Kind: instance property of HttpAgentImpl
httpAgentImpl._cache : Cache
Internal request cache, used to cache completed request results.
Kind: instance property of HttpAgentImpl
httpAgentImpl._cookie : CookieStorage
Cookie storage, used to keep track of cookies received from the server and send them with the subsequent requests to the server.
Kind: instance property of HttpAgentImpl
httpAgentImpl._cacheOptions : Object.<string, string>
Cache options.
Kind: instance property of HttpAgentImpl
httpAgentImpl._defaultRequestOptions : Object
Default request options.
Kind: instance property of HttpAgentImpl
httpAgentImpl._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.
Kind: instance property of HttpAgentImpl
httpAgentImpl.get()
Kind: instance method of HttpAgentImpl
httpAgentImpl.post()
Kind: instance method of HttpAgentImpl
httpAgentImpl.put()
Kind: instance method of HttpAgentImpl
httpAgentImpl.patch()
Kind: instance method of HttpAgentImpl
httpAgentImpl.delete()
Kind: instance method of HttpAgentImpl
httpAgentImpl.getCacheKey()
Kind: instance method of HttpAgentImpl
httpAgentImpl.setDefaultHeader()
Kind: instance method of HttpAgentImpl
httpAgentImpl.clearDefaultHeaders()
Kind: instance method of HttpAgentImpl
httpAgentImpl._requestWithCheckCache(method, url, data, [options]) ⇒ Promise.<HttpAgent~Response>
Check cache and if data isnt available then make real request.
Kind: instance method of HttpAgentImpl
Returns: Promise.<HttpAgent~Response>
- A promise that resolves to the response
with body parsed as JSON.
Param | Type | 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 request options. |
httpAgentImpl._getCachedData(method, url, data) ⇒ Promise.<HttpAgent~Response>
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.
Kind: instance method of HttpAgentImpl
Returns: Promise.<HttpAgent~Response>
- 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.
Param | 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. |
httpAgentImpl._request(method, url, data, [options]) ⇒ Promise.<HttpAgent~Response>
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.
Kind: instance method of HttpAgentImpl
Returns: Promise.<HttpAgent~Response>
- A promise that resolves to the response
with the body parsed as JSON.
Param | Type | 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 request options. |
httpAgentImpl._proxyResolved(response) ⇒ HttpAgent~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.
Kind: instance method of HttpAgentImpl
Returns: HttpAgent~Response
- The post-processed server response.
Param | Type | Description |
---|---|---|
response | HttpAgent~Response |
Server response. |
httpAgentImpl._proxyRejected(error) ⇒ Promise.<HttpAgent~Response>
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.
Kind: instance method of HttpAgentImpl
Returns: Promise.<HttpAgent~Response>
- 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.
Param | 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. |
httpAgentImpl._prepareOptions(options) ⇒ HttpAgent~RequestOptions
Prepares the provided request options object by filling in missing options with default values and addding extra options used internally.
Kind: instance method of HttpAgentImpl
Returns: HttpAgent~RequestOptions
- Request options with set filled-in
default values for missing fields, and extra options used
internally.
Param | Type | Description |
---|---|---|
options | HttpAgent~RequestOptions |
Optional request options. |
httpAgentImpl._getCacheKeySuffix(method, url, data) ⇒ string
Generates cache key suffix for an HTTP request to the specified URL with the specified data.
Kind: instance method of HttpAgentImpl
Returns: string
- The suffix of a cache key to use for a request to the
specified URL, carrying the specified data.
Param | 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. |
httpAgentImpl._setCookiesFromResponse(agentResponse)
Sets all cookies from the Set-Cookie
response header to the
cookie storage.
Kind: instance method of HttpAgentImpl
Param | Type | Description |
---|---|---|
agentResponse | HttpAgent~Response |
The response of the server. |
httpAgentImpl._saveAgentResponseToCache(agentResponse)
Saves the server response to the cache to be used as the result of the next request of the same properties.
Kind: instance method of HttpAgentImpl
Param | Type | Description |
---|---|---|
agentResponse | HttpAgent~Response |
The response of the server. |