Skip to content

Commit

Permalink
feat: generating import file for postman
Browse files Browse the repository at this point in the history
Signed-off-by: Tokesh <[email protected]>
  • Loading branch information
Tokesh committed Jan 7, 2025
1 parent 7ff1821 commit f561040
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools/src/tester/ChapterReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import CBOR from 'cbor'
import SMILE from 'smile-js'
import { APPLICATION_CBOR, APPLICATION_JSON, APPLICATION_SMILE, APPLICATION_YAML, TEXT_PLAIN } from "./MimeTypes";
import _ from 'lodash'
import { PostmanManager } from './PostmanManager'

export default class ChapterReader {
private readonly _client: OpenSearchHttpClient
private readonly logger: Logger
private readonly postmanManager: PostmanManager;

constructor (client: OpenSearchHttpClient, logger: Logger) {
constructor (client: OpenSearchHttpClient, logger: Logger, collectionPath: string = './postman_collection.json') {

Check failure on line 28 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `collectionPath` must match one of the following formats: snake_case, UPPER_CASE
this._client = client
this.logger = logger
this.postmanManager = new PostmanManager(collectionPath);
}

async read (chapter: ChapterRequest, story_outputs: StoryOutputs): Promise<ActualResponse> {
Expand All @@ -37,6 +40,9 @@ export default class ChapterReader {
story_outputs.resolve_value(chapter.request.payload),
content_type
) : undefined

this.postmanManager.addToCollection(this._client.getUrl(), chapter.method, url_path, headers, params, request_data, content_type);

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `string | undefined`

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=1.3.17, hub=opensearchproject, tests=default)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.0.0, hub=opensearchproject, tests=default)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=snapshot)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/replication)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=remote_store)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=dangling)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/index_state_management)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=default)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/workload-management)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=routing)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/security)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/ml)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/query_insights)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=3.0.0, hub=opensearchstaging, tests=default)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/analysis)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/streaming)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.19.0, hub=opensearchstaging, tests=default)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

Check failure on line 44 in tools/src/tester/ChapterReader.ts

View workflow job for this annotation

GitHub Actions / test-opensearch-spec (version=2.18.0, hub=opensearchproject, tests=plugins/notifications)

Property 'getUrl' does not exist on type 'OpenSearchHttpClient'.

this.logger.info(`=> ${chapter.method} ${url_path} (${to_json(params)}) [${content_type}] ${_.compact([to_json(headers), to_json(request_data)]).join(' | ')}`)
await this._client.request({
url: url_path,
Expand Down Expand Up @@ -66,6 +72,7 @@ export default class ChapterReader {
this.logger.info(`<= ${response.status} (${response.content_type}) | ${response.payload !== undefined ? to_json(response.payload) : response.message}`)
}
})
this.postmanManager.saveCollection();
return response as ActualResponse
}

Expand Down
65 changes: 65 additions & 0 deletions tools/src/tester/PostmanManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'fs';

Check failure on line 1 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Missing license header

export class PostmanManager {
private readonly collection: any;
private readonly collectionPath: string;

constructor(collectionPath: string = './postman_collection.json') {

Check failure on line 7 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `collectionPath` must match one of the following formats: snake_case, UPPER_CASE
this.collectionPath = collectionPath;
this.collection = {
info: {
name: "Generated Collection",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
},
item: [],
};
}

addToCollection(

Check failure on line 18 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Class Method name `addToCollection` must match one of the following formats: snake_case
url: string | undefined,
method: string,
path: string,
headers: Record<string, any> | undefined,
params: Record<string, any>,
body: any,
contentType: string

Check failure on line 25 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `contentType` must match one of the following formats: snake_case, UPPER_CASE
): void {
const bodyContent = body

Check failure on line 27 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Variable name `bodyContent` must match one of the following formats: snake_case, UPPER_CASE

Check failure on line 27 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any value in conditional. An explicit comparison or type cast is required
? (() => {

Check failure on line 28 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
switch (contentType) {

Check failure on line 29 in tools/src/tester/PostmanManager.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 10
case 'application/json':
return { mode: 'raw', raw: JSON.stringify(body) };
case 'text/plain':
return { mode: 'raw', raw: body.toString() };
case 'application/x-www-form-urlencoded':
return {
mode: 'urlencoded',
urlencoded: Object.entries(body).map(([key, value]) => ({ key, value: value.toString() })),
};
default:
throw new Error(`Unsupported content type: ${contentType}`);
}
})()
: undefined;

const item = {
name: path,
request: {
method,
header: Object.entries(headers || {}).map(([key, value]) => ({ key, value })),
url: {
raw: `${url}${path}`,
path: path.split('/').filter(Boolean),
query: Object.entries(params).map(([key, value]) => ({ key, value: value.toString() })),
},
body: bodyContent,
},
};

this.collection.item.push(item);
}

saveCollection(): void {
fs.writeFileSync(this.collectionPath, JSON.stringify(this.collection, null, 2));
}
}

0 comments on commit f561040

Please sign in to comment.