|
| 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 { GovernanceInsightsResponse } from "../models/GovernanceInsightsResponse"; |
| 18 | +import { JSONAPIErrorResponse } from "../models/JSONAPIErrorResponse"; |
| 19 | + |
| 20 | +export class GovernanceInsightsApiRequestFactory extends BaseAPIRequestFactory { |
| 21 | + public async listGovernanceInsights( |
| 22 | + withValues?: boolean, |
| 23 | + orgUuid?: string, |
| 24 | + filterProduct?: Array<string>, |
| 25 | + _options?: Configuration |
| 26 | + ): Promise<RequestContext> { |
| 27 | + const _config = _options || this.configuration; |
| 28 | + |
| 29 | + logger.warn("Using unstable operation 'listGovernanceInsights'"); |
| 30 | + if (!_config.unstableOperations["v2.listGovernanceInsights"]) { |
| 31 | + throw new Error( |
| 32 | + "Unstable operation 'listGovernanceInsights' is disabled" |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + // Path Params |
| 37 | + const localVarPath = "/api/v2/governance/insights"; |
| 38 | + |
| 39 | + // Make Request Context |
| 40 | + const requestContext = _config |
| 41 | + .getServer("v2.GovernanceInsightsApi.listGovernanceInsights") |
| 42 | + .makeRequestContext(localVarPath, HttpMethod.GET); |
| 43 | + requestContext.setHeaderParam("Accept", "application/json"); |
| 44 | + requestContext.setHttpConfig(_config.httpConfig); |
| 45 | + |
| 46 | + // Query Params |
| 47 | + if (withValues !== undefined) { |
| 48 | + requestContext.setQueryParam( |
| 49 | + "withValues", |
| 50 | + ObjectSerializer.serialize(withValues, "boolean", ""), |
| 51 | + "" |
| 52 | + ); |
| 53 | + } |
| 54 | + if (orgUuid !== undefined) { |
| 55 | + requestContext.setQueryParam( |
| 56 | + "orgUuid", |
| 57 | + ObjectSerializer.serialize(orgUuid, "string", ""), |
| 58 | + "" |
| 59 | + ); |
| 60 | + } |
| 61 | + if (filterProduct !== undefined) { |
| 62 | + requestContext.setQueryParam( |
| 63 | + "filter[product]", |
| 64 | + ObjectSerializer.serialize(filterProduct, "Array<string>", ""), |
| 65 | + "multi" |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // Apply auth methods |
| 70 | + applySecurityAuthentication(_config, requestContext, [ |
| 71 | + "apiKeyAuth", |
| 72 | + "appKeyAuth", |
| 73 | + ]); |
| 74 | + |
| 75 | + return requestContext; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export class GovernanceInsightsApiResponseProcessor { |
| 80 | + /** |
| 81 | + * Unwraps the actual response sent by the server from the response context and deserializes the response content |
| 82 | + * to the expected objects |
| 83 | + * |
| 84 | + * @params response Response returned by the server for a request to listGovernanceInsights |
| 85 | + * @throws ApiException if the response code was not in [200, 299] |
| 86 | + */ |
| 87 | + public async listGovernanceInsights( |
| 88 | + response: ResponseContext |
| 89 | + ): Promise<GovernanceInsightsResponse> { |
| 90 | + const contentType = ObjectSerializer.normalizeMediaType( |
| 91 | + response.headers["content-type"] |
| 92 | + ); |
| 93 | + if (response.httpStatusCode === 200) { |
| 94 | + const body: GovernanceInsightsResponse = ObjectSerializer.deserialize( |
| 95 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 96 | + "GovernanceInsightsResponse" |
| 97 | + ) as GovernanceInsightsResponse; |
| 98 | + return body; |
| 99 | + } |
| 100 | + if ( |
| 101 | + response.httpStatusCode === 400 || |
| 102 | + response.httpStatusCode === 401 || |
| 103 | + response.httpStatusCode === 403 |
| 104 | + ) { |
| 105 | + const bodyText = ObjectSerializer.parse( |
| 106 | + await response.body.text(), |
| 107 | + contentType |
| 108 | + ); |
| 109 | + let body: JSONAPIErrorResponse; |
| 110 | + try { |
| 111 | + body = ObjectSerializer.deserialize( |
| 112 | + bodyText, |
| 113 | + "JSONAPIErrorResponse" |
| 114 | + ) as JSONAPIErrorResponse; |
| 115 | + } catch (error) { |
| 116 | + logger.debug(`Got error deserializing error: ${error}`); |
| 117 | + throw new ApiException<JSONAPIErrorResponse>( |
| 118 | + response.httpStatusCode, |
| 119 | + bodyText |
| 120 | + ); |
| 121 | + } |
| 122 | + throw new ApiException<JSONAPIErrorResponse>( |
| 123 | + response.httpStatusCode, |
| 124 | + body |
| 125 | + ); |
| 126 | + } |
| 127 | + if (response.httpStatusCode === 429) { |
| 128 | + const bodyText = ObjectSerializer.parse( |
| 129 | + await response.body.text(), |
| 130 | + contentType |
| 131 | + ); |
| 132 | + let body: APIErrorResponse; |
| 133 | + try { |
| 134 | + body = ObjectSerializer.deserialize( |
| 135 | + bodyText, |
| 136 | + "APIErrorResponse" |
| 137 | + ) as APIErrorResponse; |
| 138 | + } catch (error) { |
| 139 | + logger.debug(`Got error deserializing error: ${error}`); |
| 140 | + throw new ApiException<APIErrorResponse>( |
| 141 | + response.httpStatusCode, |
| 142 | + bodyText |
| 143 | + ); |
| 144 | + } |
| 145 | + throw new ApiException<APIErrorResponse>(response.httpStatusCode, body); |
| 146 | + } |
| 147 | + |
| 148 | + // Work around for missing responses in specification, e.g. for petstore.yaml |
| 149 | + if (response.httpStatusCode >= 200 && response.httpStatusCode <= 299) { |
| 150 | + const body: GovernanceInsightsResponse = ObjectSerializer.deserialize( |
| 151 | + ObjectSerializer.parse(await response.body.text(), contentType), |
| 152 | + "GovernanceInsightsResponse", |
| 153 | + "" |
| 154 | + ) as GovernanceInsightsResponse; |
| 155 | + return body; |
| 156 | + } |
| 157 | + |
| 158 | + const body = (await response.body.text()) || ""; |
| 159 | + throw new ApiException<string>( |
| 160 | + response.httpStatusCode, |
| 161 | + 'Unknown API Status Code!\nBody: "' + body + '"' |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +export interface GovernanceInsightsApiListGovernanceInsightsRequest { |
| 167 | + /** |
| 168 | + * Whether to compute and include the current and previous value of each insight. |
| 169 | + * Defaults to `false`, in which case only insight metadata is returned. |
| 170 | + * @type boolean |
| 171 | + */ |
| 172 | + withValues?: boolean; |
| 173 | + /** |
| 174 | + * The UUID of the organization to compute insights for. Defaults to the organization of |
| 175 | + * the authenticated user. Used to retrieve insights for a child organization from a |
| 176 | + * parent organization. |
| 177 | + * @type string |
| 178 | + */ |
| 179 | + orgUuid?: string; |
| 180 | + /** |
| 181 | + * Restrict the results to insights belonging to the given products. May be repeated to |
| 182 | + * filter by multiple products. Matching is case-insensitive. |
| 183 | + * @type Array<string> |
| 184 | + */ |
| 185 | + filterProduct?: Array<string>; |
| 186 | +} |
| 187 | + |
| 188 | +export class GovernanceInsightsApi { |
| 189 | + private requestFactory: GovernanceInsightsApiRequestFactory; |
| 190 | + private responseProcessor: GovernanceInsightsApiResponseProcessor; |
| 191 | + private configuration: Configuration; |
| 192 | + |
| 193 | + public constructor( |
| 194 | + configuration: Configuration, |
| 195 | + requestFactory?: GovernanceInsightsApiRequestFactory, |
| 196 | + responseProcessor?: GovernanceInsightsApiResponseProcessor |
| 197 | + ) { |
| 198 | + this.configuration = configuration; |
| 199 | + this.requestFactory = |
| 200 | + requestFactory || new GovernanceInsightsApiRequestFactory(configuration); |
| 201 | + this.responseProcessor = |
| 202 | + responseProcessor || new GovernanceInsightsApiResponseProcessor(); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Retrieve the list of governance insights available to the organization. By default, only |
| 207 | + * insight metadata is returned; pass `withValues=true` to also compute and include each |
| 208 | + * insight's current and previous values. Insights can be filtered by product. |
| 209 | + * @param param The request object |
| 210 | + */ |
| 211 | + public listGovernanceInsights( |
| 212 | + param: GovernanceInsightsApiListGovernanceInsightsRequest = {}, |
| 213 | + options?: Configuration |
| 214 | + ): Promise<GovernanceInsightsResponse> { |
| 215 | + const requestContextPromise = this.requestFactory.listGovernanceInsights( |
| 216 | + param.withValues, |
| 217 | + param.orgUuid, |
| 218 | + param.filterProduct, |
| 219 | + options |
| 220 | + ); |
| 221 | + return requestContextPromise.then((requestContext) => { |
| 222 | + return this.configuration.httpApi |
| 223 | + .send(requestContext) |
| 224 | + .then((responseContext) => { |
| 225 | + return this.responseProcessor.listGovernanceInsights(responseContext); |
| 226 | + }); |
| 227 | + }); |
| 228 | + } |
| 229 | +} |
0 commit comments