|
| 1 | +import { BaseAPIRequestFactory } from "../../datadog-api-client-common/baseapi"; |
| 2 | +import { |
| 3 | + Configuration, |
| 4 | + applySecurityAuthentication, |
| 5 | +} from "../../datadog-api-client-common/configuration"; |
| 6 | +import { |
| 7 | + RequestContext, |
| 8 | + HttpMethod, |
| 9 | + ResponseContext, |
| 10 | +} from "../../datadog-api-client-common/http/http"; |
| 11 | + |
| 12 | +import { logger } from "../../../logger"; |
| 13 | +import { ObjectSerializer } from "../models/ObjectSerializer"; |
| 14 | +import { ApiException } from "../../datadog-api-client-common/exception"; |
| 15 | + |
| 16 | +import { APIErrorResponse } from "../models/APIErrorResponse"; |
| 17 | +import { ServiceList } from "../models/ServiceList"; |
| 18 | + |
| 19 | +export class ApmApiRequestFactory extends BaseAPIRequestFactory { |
| 20 | + public async getServiceList( |
| 21 | + _options?: Configuration |
| 22 | + ): Promise<RequestContext> { |
| 23 | + const _config = _options || this.configuration; |
| 24 | + |
| 25 | + // Path Params |
| 26 | + const localVarPath = "/api/v2/apm/services"; |
| 27 | + |
| 28 | + // Make Request Context |
| 29 | + const requestContext = _config |
| 30 | + .getServer("v2.ApmApi.getServiceList") |
| 31 | + .makeRequestContext(localVarPath, HttpMethod.GET); |
| 32 | + requestContext.setHeaderParam("Accept", "application/json"); |
| 33 | + requestContext.setHttpConfig(_config.httpConfig); |
| 34 | + |
| 35 | + // Apply auth methods |
| 36 | + applySecurityAuthentication(_config, requestContext, [ |
| 37 | + "apiKeyAuth", |
| 38 | + "appKeyAuth", |
| 39 | + "AuthZ", |
| 40 | + ]); |
| 41 | + |
| 42 | + return requestContext; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +export class ApmApiResponseProcessor { |
| 47 | + /** |
| 48 | + * Unwraps the actual response sent by the server from the response context and deserializes the response content |
| 49 | + * to the expected objects |
| 50 | + * |
| 51 | + * @params response Response returned by the server for a request to getServiceList |
| 52 | + * @throws ApiException if the response code was not in [200, 299] |
| 53 | + */ |
| 54 | + public async getServiceList(response: ResponseContext): Promise<ServiceList> { |
| 55 | + const contentType = ObjectSerializer.normalizeMediaType( |
| 56 | + response.headers["content-type"] |
| 57 | + ); |
| 58 | + if (response.httpStatusCode === 200) { |
| 59 | + const body: ServiceList = ObjectSerializer.deserialize( |
| 60 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 61 | + "ServiceList" |
| 62 | + ) as ServiceList; |
| 63 | + return body; |
| 64 | + } |
| 65 | + if (response.httpStatusCode === 429) { |
| 66 | + const bodyText = ObjectSerializer.parse( |
| 67 | + await response.body.text(), |
| 68 | + contentType |
| 69 | + ); |
| 70 | + let body: APIErrorResponse; |
| 71 | + try { |
| 72 | + body = ObjectSerializer.deserialize( |
| 73 | + bodyText, |
| 74 | + "APIErrorResponse" |
| 75 | + ) as APIErrorResponse; |
| 76 | + } catch (error) { |
| 77 | + logger.debug(`Got error deserializing error: ${error}`); |
| 78 | + throw new ApiException<APIErrorResponse>( |
| 79 | + response.httpStatusCode, |
| 80 | + bodyText |
| 81 | + ); |
| 82 | + } |
| 83 | + throw new ApiException<APIErrorResponse>(response.httpStatusCode, body); |
| 84 | + } |
| 85 | + |
| 86 | + // Work around for missing responses in specification, e.g. for petstore.yaml |
| 87 | + if (response.httpStatusCode >= 200 && response.httpStatusCode <= 299) { |
| 88 | + const body: ServiceList = ObjectSerializer.deserialize( |
| 89 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 90 | + "ServiceList", |
| 91 | + "" |
| 92 | + ) as ServiceList; |
| 93 | + return body; |
| 94 | + } |
| 95 | + |
| 96 | + const body = (await response.body.text()) || ""; |
| 97 | + throw new ApiException<string>( |
| 98 | + response.httpStatusCode, |
| 99 | + 'Unknown API Status Code!\nBody: "' + body + '"' |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export class ApmApi { |
| 105 | + private requestFactory: ApmApiRequestFactory; |
| 106 | + private responseProcessor: ApmApiResponseProcessor; |
| 107 | + private configuration: Configuration; |
| 108 | + |
| 109 | + public constructor( |
| 110 | + configuration: Configuration, |
| 111 | + requestFactory?: ApmApiRequestFactory, |
| 112 | + responseProcessor?: ApmApiResponseProcessor |
| 113 | + ) { |
| 114 | + this.configuration = configuration; |
| 115 | + this.requestFactory = |
| 116 | + requestFactory || new ApmApiRequestFactory(configuration); |
| 117 | + this.responseProcessor = responseProcessor || new ApmApiResponseProcessor(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param param The request object |
| 122 | + */ |
| 123 | + public getServiceList(options?: Configuration): Promise<ServiceList> { |
| 124 | + const requestContextPromise = this.requestFactory.getServiceList(options); |
| 125 | + return requestContextPromise.then((requestContext) => { |
| 126 | + return this.configuration.httpApi |
| 127 | + .send(requestContext) |
| 128 | + .then((responseContext) => { |
| 129 | + return this.responseProcessor.getServiceList(responseContext); |
| 130 | + }); |
| 131 | + }); |
| 132 | + } |
| 133 | +} |
0 commit comments