Commit d9f45491 authored by Martin Lowe's avatar Martin Lowe 🇨🇦
Browse files

Fix axios HTTP wrapper, finish group+proj cleanup

parent 00ed4cb0
This diff is collapsed.
......@@ -251,7 +251,7 @@ const testProjects: EclipseProject[] = [
],
gitlab_repos: [
{
url: 'https://gitlab.eclipse.org/eclipsefdn/webdev/gitlab-testing',
url: 'http://localhost/eclipse/spider.pig',
},
],
contributors: [],
......
import {RequesterType} from '@gitbeaker/requester-utils';
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 {
get(endpoint: string, options?: Record<string, unknown>): Promise<any> {
throw new Error('Method not implemented.');
}
post(endpoint: string, options?: Record<string, unknown>): Promise<any> {
throw new Error('Method not implemented.');
}
put(endpoint: string, options?: Record<string, unknown>): Promise<any> {
throw new Error('Method not implemented.');
}
delete(endpoint: string, options?: Record<string, unknown>): Promise<any> {
throw new Error('Method not implemented.');
}
stream?(endpoint: string, options?: Record<string, unknown>): NodeJS.ReadableStream {
throw new Error('Method not implemented.');
export function defaultOptionsHandler(
resourceOptions: DefaultResourceOptions,
{ body, query, sudo, method }: DefaultRequestOptions = {}
): DefaultRequestReturn & {
json?: Record<string, unknown>;
https?: { rejectUnauthorized: boolean };
} {
const options: DefaultRequestReturn & {
json?: Record<string, unknown>;
https?: { rejectUnauthorized: boolean };
} = baseOptionsHandler(resourceOptions, { body, query, sudo, method });
// FIXME: Not the best comparison, but...it will have to do for now.
if (typeof body === 'object' && body.constructor.name !== 'FormData') {
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 };
}
}
\ No newline at end of file
export const requesterFn = createRequesterFn(defaultOptionsHandler, handler);
......@@ -50,8 +50,6 @@ let args = yargs(process.argv)
.version('0.1')
.alias('v', 'version')
.epilog('Copyright 2019 Eclipse Foundation inc.').argv;
const runner =
run();
async function run() {
......
This diff is collapsed.
{
"compilerOptions": {
"module": "CommonJS",
"esModuleInterop": true,
"sourceMap": true
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment