Skip to content
Snippets Groups Projects
Commit d9f45491 authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

Fix axios HTTP wrapper, finish group+proj cleanup

parent 00ed4cb0
No related branches found
No related tags found
1 merge request!210Rebuild GL sync in TS, add arb. group nesting
This diff is collapsed.
...@@ -251,7 +251,7 @@ const testProjects: EclipseProject[] = [ ...@@ -251,7 +251,7 @@ const testProjects: EclipseProject[] = [
], ],
gitlab_repos: [ gitlab_repos: [
{ {
url: 'https://gitlab.eclipse.org/eclipsefdn/webdev/gitlab-testing', url: 'http://localhost/eclipse/spider.pig',
}, },
], ],
contributors: [], contributors: [],
......
import {RequesterType} from '@gitbeaker/requester-utils';
import axios from 'axios'; import axios from 'axios';
import { decamelizeKeys } from 'xcase';
import {
DefaultResourceOptions,
DefaultRequestReturn,
DefaultRequestOptions,
createRequesterFn,
defaultOptionsHandler as baseOptionsHandler,
} from '@gitbeaker/requester-utils';
export class AxiosRequester implements RequesterType { export function defaultOptionsHandler(
get(endpoint: string, options?: Record<string, unknown>): Promise<any> { resourceOptions: DefaultResourceOptions,
throw new Error('Method not implemented.'); { body, query, sudo, method }: DefaultRequestOptions = {}
} ): DefaultRequestReturn & {
post(endpoint: string, options?: Record<string, unknown>): Promise<any> { json?: Record<string, unknown>;
throw new Error('Method not implemented.'); https?: { rejectUnauthorized: boolean };
} } {
put(endpoint: string, options?: Record<string, unknown>): Promise<any> { const options: DefaultRequestReturn & {
throw new Error('Method not implemented.'); json?: Record<string, unknown>;
} https?: { rejectUnauthorized: boolean };
delete(endpoint: string, options?: Record<string, unknown>): Promise<any> { } = baseOptionsHandler(resourceOptions, { body, query, sudo, method });
throw new Error('Method not implemented.');
} // FIXME: Not the best comparison, but...it will have to do for now.
stream?(endpoint: string, options?: Record<string, unknown>): NodeJS.ReadableStream { if (typeof body === 'object' && body.constructor.name !== 'FormData') {
throw new Error('Method not implemented.'); options.json = decamelizeKeys(body);
delete options.body;
}
if (resourceOptions.url.includes('https') && resourceOptions.rejectUnauthorized != null && resourceOptions.rejectUnauthorized === false) {
options.https = {
rejectUnauthorized: resourceOptions.rejectUnauthorized,
};
}
return options;
}
export function processBody({ data, headers }: { data: Buffer; headers: Record<string, unknown> }) {
// Split to remove potential charset info from the content type
const contentType = ((headers['content-type'] as string) || '').split(';')[0].trim();
if (contentType === 'application/json') {
return data.length === 0 ? {} : data;
}
if (contentType.startsWith('text/')) {
return data.toString();
}
return Buffer.from(data);
}
export async function handler(endpoint: string, options: Record<string, unknown>) {
const retryCodes = [429, 502];
const maxRetries = 10;
let response;
// map the Got prefix to the axios base url
options['baseURL'] = options['prefixUrl'];
// map the qs glob into an object
options['params'] = Object.fromEntries(new URLSearchParams(options['searchParams'] as string));
options['data'] = options['json'];
delete options['json'];
for (let i = 0; i < maxRetries; i += 1) {
const waitTime = 2 ** i;
try {
response = await axios(endpoint, { ...options }); // eslint-disable-line
break;
} catch (e) {
if (e.response) {
if (retryCodes.includes(e.response.statusCode)) {
await new Promise(resolve => setTimeout(resolve, waitTime));
continue; // eslint-disable-line
}
if (typeof e.response.body === 'string' && e.response.body.length > 0) {
try {
const output = JSON.parse(e.response.body);
e.description = output.error || output.message;
} catch (err) {
e.description = e.response.body;
}
}
}
throw e;
} }
}
const { status, headers } = response;
const body = processBody(response);
return { body, headers, status: status };
}
} export const requesterFn = createRequesterFn(defaultOptionsHandler, handler);
\ No newline at end of file
...@@ -50,8 +50,6 @@ let args = yargs(process.argv) ...@@ -50,8 +50,6 @@ let args = yargs(process.argv)
.version('0.1') .version('0.1')
.alias('v', 'version') .alias('v', 'version')
.epilog('Copyright 2019 Eclipse Foundation inc.').argv; .epilog('Copyright 2019 Eclipse Foundation inc.').argv;
const runner =
run(); run();
async function run() { async function run() {
......
This diff is collapsed.
{ {
"compilerOptions": { "compilerOptions": {
"module": "CommonJS",
"esModuleInterop": true, "esModuleInterop": true,
"sourceMap": true "sourceMap": true
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment