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

Generating import file for postman #770

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .cspell
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ aoss
APIV
cbor
evals
formdata
lucene
millis
mxyz
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added `GET /_plugins/_ml/connectors/{connector_id}`, `_search`, `POST /_plugins/_ml/connectors/_search`, and `PUT /_plugins/_ml/connectors/{connector_id}` ([#764](https://github.com/opensearch-project/opensearch-api-specification/pull/764))
- Added the ability to skip an individual chapter test ([#765](https://github.com/opensearch-project/opensearch-api-specification/pull/765))
- Added uploading of test spec logs ([#767](https://github.com/opensearch-project/opensearch-api-specification/pull/767))
- Added generation of file for Postman ([#770](https://github.com/opensearch-project/opensearch-api-specification/pull/770))

### Removed
- Removed unsupported `_common.mapping:SourceField`'s `mode` field and associated `_common.mapping:SourceFieldMode` enum ([#652](https://github.com/opensearch-project/opensearch-api-specification/pull/652))
Expand Down
7 changes: 7 additions & 0 deletions tools/src/OpenSearchHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ export class OpenSearchHttpClient {
}
}

get_url(): string | undefined {
if (this._opts != null && this._opts.url != null && this._opts.url !== '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think lodash isEmpty can this this cleaner, maybe just _.isEmpty(this._opts?.url) ? DEFAULT_URL : this._opts?.url.

return this._opts.url;
}
return DEFAULT_URL;
}

async request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R> {
return await this._axios.request(config)
}
Expand Down
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 postman_manager: PostmanManager;

constructor (client: OpenSearchHttpClient, logger: Logger) {
constructor (client: OpenSearchHttpClient, logger: Logger, collection_path: string = './postman_collection.json') {
this._client = client
this.logger = logger
this.postman_manager = new PostmanManager(collection_path);
}

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.postman_manager.add_to_collection(this._client.get_url(), chapter.method, url_path, headers, params, request_data, content_type);

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.postman_manager.save_collection();
return response as ActualResponse
}

Expand Down
57 changes: 57 additions & 0 deletions tools/src/tester/PostmanManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import fs from 'fs';

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

constructor(collection_path: string = './postman_collection.json') {
this.collection_path = collection_path;
this.collection = {
info: {
name: "Generated Collection",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
},
item: [],
};
}

add_to_collection(
url: string | undefined,
method: string,
path: string,
headers: Record<string, any> | undefined,
params: Record<string, any>,
body: any,
content_type: string
): void {
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: String(value) })),
},
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
body: body ? { mode: content_type === 'application/json' ? 'raw' : 'formdata', raw: JSON.stringify(body) } : undefined,
},
};

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

save_collection(): void {
fs.writeFileSync(this.collection_path, JSON.stringify(this.collection, null, 2));
}
}
Loading