/*! * Copyright (c) 2020 Ville de Montreal. All rights reserved. * Licensed under the MIT license. * See LICENSE file in the project root for full license information. */ import * as http from 'http'; import { ILogger } from '../logging/ILogger'; import { HttpClientError } from './HttpClientError'; import { IHttpClient } from './IHttpClient'; import { IHttpDefaults } from './IHttpDefaults'; import { IHttpRequest } from './IHttpRequest'; import { IHttpResponse } from './IHttpResponse'; import { ISerializers } from './serialization/ISerializers'; /** * Describes a payload that can be sent or received using the HTTP protocol */ export interface IHttpContent { /** * the content object */ content: string | Buffer; /** * the content-type of the content object */ contentType: string; /** * the length of the content object in bytes */ contentLength: number; } /** * the HTTP context used while sending a request */ export interface IHttpContext { /** * the request being sent */ request: Readonly; /** * the HTTP request options */ options: http.RequestOptions; /** * the method of the request (GET/PUT/POST...) */ method: string; /** * the url of the request */ url: URL; /** * the headers of the request */ headers: http.OutgoingHttpHeaders; /** * the serializers that can be used to serialize the outgoing body * and deserialize the incoming body. */ serializers: ISerializers; } /** * Default implementation of the IHttpClient interface. * Each http client binding should provide its own implementation of IHttpClient. */ export declare class DefaultHttpClient implements IHttpClient { private readonly logger; private readonly defaults; constructor(logger: ILogger, defaults?: IHttpDefaults); /** * Sends a HTTP request to a remote server * @param request the HTTP request to send to a remote server * @returns a HTTP response from the server * @throws HttpClientError when response status code is not within 200 to 299 range, * or for any other exception. */ send(request: Readonly): Promise; private doSendAsync; private doSendWithCallback; private createRequestContext; private createRequestContent; private injectCorrelationId; private processResponse; private logStart; private logEnd; private logError; } /** * Serializes the submitted content either as a string or as a buffer, * depending on the content type and the serializer. * @param context the http context * @param content the content that needs to be serialized * @param contentType the content-type of the content */ export declare function serialize(context: IHttpContext, content: unknown, contentType: string): any; /** * deserializes the payload of the received http response * @param context the http context * @param res the internal http response object * @param content the received content * @param [contentType] the content-type of the received content */ export declare function deserialize(context: IHttpContext, res: unknown, content: Buffer, contentType?: string): any; /** * maps internal errors into standard HttpClientError objects * @param err the original error */ export declare function remapError(err: any): HttpClientError; /** * gets the value of a http request or response header * @param value the name of the header */ export declare function getHeaderAsString(value: number | string | string[] | undefined): string | undefined; /** * Tries to extract the message property from * the submitted error object. * @param error the error object */ export declare function extractMessageFromError(error: any): any; /** * formats a message by adding information about the request beeing executed * @param context the http context * @param message the message to format */ export declare function formatErrorMessage(context: IHttpContext, message: string): string; /** * tells if the content was already serialized or not * @param content an instance */ export declare function isAlreadySerialized(content: unknown): boolean; /** * tells if the submitted status code is invalid * @param [statusCode] a standard HTTP status code */ export declare function isInvalidStatusCode(statusCode?: number): boolean;