Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backstage http request plugin #40

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions packages/backend/src/plugins/http-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';

export type Methods =
| 'GET'
| 'OPTIONS'
| 'POST'
| 'UPDATE'
| 'DELETE'
| 'PUT'
| 'PATCH';

export type Headers = {
[key: string]: string;
};
export type Params = {
[key: string]: string;
};
export type Body = string | undefined;

export interface HttpOptions {
url: string;
method: string;
headers: Headers;
body?: Body;
}

export const createHttpRequest = () => {
return createTemplateAction<{
api: string;
method: Methods;
headers?: Headers;
params?: Params;
body?: any;
}>({
id: 'cnoe:http:request',
description: 'Sends a HTTP request to APIs with headers and payload',
schema: {
input: {
type: 'object',
required: ['api', 'method'],
properties: {
api: {
type: 'string',
title: 'API Url',
description: 'The url path you want to query',
},
method: {
type: 'string',
title: 'Method',
description: 'The method type of the request',
enum: [
'GET',
'OPTIONS',
'POST',
'UPDATE',
'DELETE',
'PUT',
'PATCH',
],
},
headers: {
title: 'Request headers',
description: 'The headers you would like to pass to your request',
type: 'object',
},
params: {
title: 'Request query params',
description:
'The query parameters you would like to pass to your request',
type: 'object',
},
body: {
title: 'Request body',
description: 'The body you would like to pass to your request',
type: ['object', 'string', 'array'],
},
},
},
output: {
type: 'object',
properties: {
code: {
title: 'Response Code',
type: 'string',
},
},
},
},
async handler(ctx) {
let inputBody: Body = undefined;
let response: any;

if (
ctx.input.body &&
typeof ctx.input.body !== 'string' &&
ctx.input.headers &&
ctx.input.headers['content-type'] &&
ctx.input.headers['content-type'].includes('application/json')
) {
inputBody = JSON.stringify(ctx.input.body);
} else {
inputBody = ctx.input.body;
}

const queryParams: string = ctx.input.params
? new URLSearchParams(ctx.input.params).toString()
: '';
const url =
queryParams ? `${ctx.input.api}?${queryParams}` : ctx.input.api;

ctx.logger.info(`Request Url: ${url}`);

try {
response = await fetch(url, {
method: ctx.input.method,
headers: ctx.input.headers ? (ctx.input.headers as Headers) : {},
body: inputBody,
});
if (!response) {
throw new Error(`Request was aborted as it took longer time`);
}
} catch (e) {
throw new Error(`There was an issue with the request: ${e}`);
}

if (response.status >= 200 && response.status <= 300) {
ctx.logger.info(
`Request was successful. Status code: ${response.status}`,
);
} else {
ctx.logger.error(
`There was an issue with request. Status code: ${response.status}`,
);
throw new Error(
`The Url request is failed with ${response.status} code.`,
);
}

ctx.output('code', response.status);
},
});
};
2 changes: 2 additions & 0 deletions packages/backend/src/plugins/scaffolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getRootLogger } from '@backstage/backend-common';
import { createKubernetesApply } from './k8s-apply';
import { createSanitizeResource } from './sanitize';
import { createVerifyDependency } from './verify';
import { createHttpRequest } from './http-request';

export const cnoeScaffolderActions = createBackendModule({
pluginId: 'scaffolder',
Expand All @@ -37,6 +38,7 @@ export const cnoeScaffolderActions = createBackendModule({
createKubernetesApply(config),
createSanitizeResource(),
createVerifyDependency(),
createHttpRequest(),
);
},
});
Expand Down