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