-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
144 lines (125 loc) · 4.71 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* tslint:disable */
/* eslint-disable */
/**
* IONOS DBaaS MariaDB REST API
* An enterprise-grade Database is provided as a Service (DBaaS) solution that can be managed through a browser-based \"Data Center Designer\" (DCD) tool or via an easy to use API. The API allows you to create additional MariaDB database clusters or modify existing ones. It is designed to allow users to leverage the same power and flexibility found within the DCD visual tool. Both tools are consistent with their concepts and lend well to making the experience smooth and intuitive.
*
* The version of the OpenAPI document: 0.1.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
import { Configuration, DEFAULT_MAX_RETRIES, DEFAULT_MAX_WAIT_TIME } from "./configuration";
// Some imports not used depending on template conditions
// @ts-ignore
import globalAxios, { AxiosPromise, AxiosInstance } from 'axios';
export const BASE_PATH = "https://mariadb.de-txl.ionos.com".replace(/\/+$/, "");
/**
*
* @export
*/
export const COLLECTION_FORMATS = {
csv: ",",
ssv: " ",
tsv: "\t",
pipes: "|",
};
/**
*
* @export
* @interface RequestArgs
*/
export interface RequestArgs {
url: string;
options: any;
}
/**
*
* @export
* @class BaseAPI
*/
export class BaseAPI {
protected configuration: Configuration | undefined;
constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected axios: AxiosInstance = globalAxios) {
if (configuration) {
this.configuration = configuration;
this.basePath = configuration.basePath || this.basePath;
}
}
};
/**
*
* @export
* @class RequiredError
* @extends {Error}
*/
export class RequiredError extends Error {
name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
super(msg);
}
}
const debug = (message: string, configuration?: Configuration) => {
if (configuration && configuration.debug) {
console.log(`[ ${configuration.getUserAgent()} ][debug] ${message}`);
}
}
export const backOff = async (iteration: number, seconds?: number, configuration?: Configuration): Promise<any> => {
let sleepTime = 0;
if (seconds === undefined) {
const maxWaitTime = (configuration !== undefined) ? configuration.getMaxWaitTime() : DEFAULT_MAX_WAIT_TIME;
sleepTime = Math.min(Math.pow(2, iteration) * 1000, maxWaitTime);
} else {
sleepTime = seconds * 1000;
}
if (sleepTime > 0) {
debug(`backing off ${sleepTime/1000}s before retrying`, configuration);
}
return await new Promise((res, rej) => setTimeout(res, sleepTime));
}
export const runRequest = (axiosArgs: RequestArgs, configuration?: Configuration) => async (axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || basePath) + axiosArgs.url};
let retries = 0;
let retryAfter = undefined;
const maxRetries = (configuration !== undefined) ? configuration.getMaxRetries() : DEFAULT_MAX_RETRIES;
do {
try {
await backOff(retries, retryAfter, configuration);
retryAfter = undefined;
retries ++;
return await axios.request(axiosRequestArgs);
} catch (error) {
if (error.response === undefined) {
throw error;
}
if (retries >= maxRetries) {
debug(`maximum number of retries (${maxRetries} exhausted`);
throw error;
}
switch (error.response.status) {
case 502: /* bad gateway */
case 503: /* service unavailable */
case 504: /* gateway timeout */
/* backoff exponentially and retry */
debug(`got ${error.response.status} from the API, retrying`, configuration);
break;
case 429: /* too many requests */
/* use the value of the Retry-After header as backoff seconds */
debug(`got ${error.response.status} from the API, retrying`, configuration);
if (error.response.headers !== undefined && error.response.headers['Retry-After'] !== undefined) {
retryAfter = Number(error.response.headers['Retry-After'])
if (isNaN(retryAfter)) {
retryAfter = undefined;
} else {
debug(`Retry-After = ${retryAfter}`, configuration);
}
}
break;
default:
throw error;
}
}
} while (retries < maxRetries);
};