Skip to content

Instantly share code, notes, and snippets.

@someshkoli
Last active July 17, 2020 21:17
Show Gist options
  • Save someshkoli/6ae0ecedcfe59a5faa83bc91d7bdf529 to your computer and use it in GitHub Desktop.
Save someshkoli/6ae0ecedcfe59a5faa83bc91d7bdf529 to your computer and use it in GitHub Desktop.
Sample sdkgen output from postman-collection-code-generator
var request = require('request');
const configVariables = {
'var1': 'valur',
'var2': 'value2',
'var3': 'value3',
'var4': 'value4',
'var5': 'value5',
'url': 'https://postman-echo.com',
};
function SDK(environment = {}) {
var self = this;
/**
HTTP has multiple request "verbs", such as `GET`, `PUT`, `POST`, `DELETE`,
`PATCH`, `HEAD`, etc.
An HTTP Method (verb) defines how a request should be interpreted by a server.
The endpoints in this section demonstrate various HTTP Verbs. Postman supports
all the HTTP Verbs, including some rarely used ones, such as `PROPFIND`, `UNLINK`,
etc.
For details about HTTP Verbs, refer to [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9)
*/
this.REQUEST_METHODS = {
/**
A set of `/time/*` mounted requests to perform date-time manipulations, among other operations.
*/
"UTILITIES_/_DATE_AND_TIME": {
/**
A simple `GET` request to `/time/between` to check if the provided timestamp is between a range specified by the `start` and `end` query parameters. A resolution limit can also be specified by the `unit` query parameter.
For instance, for a resolution `unit` of `month`,
`2016-10-05` does lie between `2016-11-02` and `2016-09-01`.
This endpoint also accepts `timestamp`, `locale`, `format`, `strict`, and `target` query parameters to construct the date time instance to check against.
Responses are provided in JSON format, with a `between` key to indicate the result. The response code is `200` for valid query parameters, and `400` otherwise.
```
{
between: true/false
}
```
Auth api key added as query param
@param {String} variables.url
@param {Function} callback - Callback function to return response (err, res)
*/
"BETWEEN_TIMESTAMPS": function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var request = require('request');
var options = {
'method': 'GET',
'url': '' + url + '/time/between?timestamp=2016-10-10&start=2017-10-10&end=2019-10-10',
'headers': {}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
},
/**
A simple `GET` request to `/time/leap` to check if the provided/current timestamp belongs to a leap year.
This endpoint also accepts `timestamp`, `locale`, `format`, `strict`, and `target` query parameters to construct the date time instance to check against.
Responses are provided in JSON format, with a `leap` key to indicate the result. The response code is `200` for valid query parameters, and `400` otherwise.
```
{
leap: true/false
}
```
Auth api key added as query param
@param {String} variables.url
@param {Function} callback - Callback function to return response (err, res)
*/
"LEAP_YEAR_CHECK": function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var request = require('request');
var options = {
'method': 'GET',
'url': '' + url + '/time/leap?timestamp=2016-10-10',
'headers': {}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
},
},
/**
The HTTP `POST` request method is meant to transfer data to a server
(and elicit a response). What data is returned depends on the implementation
of the server.
A `POST` request can pass parameters to the server using "Query String
Parameters", as well as the Request Body. For example, in the following request,
> POST /hi/there?hand=wave
>
> <request-body>
The parameter "hand" has the value "wave". The request body can be in multiple
formats. These formats are defined by the MIME type of the request. The MIME
Type can be set using the ``Content-Type`` HTTP header. The most commonly used
MIME types are:
* `multipart/form-data`
* `application/x-www-form-urlencoded`
* `application/json`
This endpoint echoes the HTTP headers, request parameters, the contents of
the request body and the complete URI requested when data is sent as a form parameter.
Auth api key added as query param
@param {String} variables.url
@param {String} variables.var4
@param {String} variables.var5
@param {Function} callback - Callback function to return response (err, res)
*/
"POST_FORM_DATA": function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var var4 = variables.var4 ? variables.var4 : self.variables.var4;
var var5 = variables.var5 ? variables.var5 : self.variables.var5;
var request = require('request');
var options = {
'method': 'POST',
'url': '' + url + '/post',
'headers': {},
form: {
'foo1': '' + var4 + '',
'foo2': '' + var5 + ''
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
},
/**
The HTTP `DELETE` method is used to delete resources on a server. The exact
use of `DELETE` requests depends on the server implementation. In general,
`DELETE` requests support both, Query String parameters as well as a Request
Body.
This endpoint accepts an HTTP `DELETE` request and provides debug information
such as the HTTP headers, Query String arguments, and the Request Body.
Auth api key added as Header
@param {String} variables.url
@param {String} variables.var1
@param {Function} callback - Callback function to return response (err, res)
*/
"DELETE_REQUEST": function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var var1 = variables.var1 ? variables.var1 : self.variables.var1;
var request = require('request');
var options = {
'method': 'DELETE',
'url': '' + url + '/delete',
'headers': {},
body: 'This is expected to be sent back as part of response body.' + var1 + ''
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
},
};
/**
The HTTP `POST` request method is meant to transfer data to a server
(and elicit a response). What data is returned depends on the implementation
of the server.
A `POST` request can pass parameters to the server using "Query String
Parameters", as well as the Request Body. For example, in the following request,
> POST /hi/there?hand=wave
>
> <request-body>
The parameter "hand" has the value "wave". The request body can be in multiple
formats. These formats are defined by the MIME type of the request. The MIME
Type can be set using the ``Content-Type`` HTTP header. The most commonly used
MIME types are:
* `multipart/form-data`
* `application/x-www-form-urlencoded`
* `application/json`
This endpoint echoes the HTTP headers, request parameters, the contents of
the request body and the complete URI requested.
Auth api key added as Header
@param {String} variables.url
@param {String} variables.var3
@param {String} variables.var2
@param {Function} callback - Callback function to return response (err, res)
*/
this.POST_RAW_TEXT = function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var var3 = variables.var3 ? variables.var3 : self.variables.var3;
var var2 = variables.var2 ? variables.var2 : self.variables.var2;
var request = require('request');
var options = {
'method': 'POST',
'url': '' + url + '/post',
'headers': {},
body: 'This is expected to be sent back as part of response body ' + var3 + ' ' + var2 + ''
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
};
/**
The HTTP `POST` request method is meant to transfer data to a server
(and elicit a response). What data is returned depends on the implementation
of the server.
A `POST` request can pass parameters to the server using "Query String
Parameters", as well as the Request Body. For example, in the following request,
> POST /hi/there?hand=wave
>
> <request-body>
The parameter "hand" has the value "wave". The request body can be in multiple
formats. These formats are defined by the MIME type of the request. The MIME
Type can be set using the ``Content-Type`` HTTP header. The most commonly used
MIME types are:
* `multipart/form-data`
* `application/x-www-form-urlencoded`
* `application/json`
This endpoint echoes the HTTP headers, request parameters, the contents of
the request body and the complete URI requested.
Auth api key added as Header
@param {String} variables.url
@param {Function} callback - Callback function to return response (err, res)
*/
this.POST_RAW_TEXT_COPY = function (variables, callback) {
if (typeof variables === 'function') {
callback = variables;
variables = {};
}
var url = variables.url ? variables.url : self.variables.url;
var request = require('request');
var options = {
'method': 'POST',
'url': '' + url + '/post',
'headers': {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: '{\n body {\n graphql {\n \n }\n }\n}',
variables: {}
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
};
this.variables = JSON.parse(JSON.stringify(configVariables));
this.setVariables(environment);
}
/**
Function to set environment variables. These variables will override the collection variables
@param {Object} env Object containing env variables
*/
SDK.prototype.setVariables = function (vars) {
let variables = JSON.parse(JSON.stringify(configVariables));
Object.keys(vars).forEach(function (key) {
variables[key] = vars[key];
});
this.variables = variables;
return this.variables;
};
/**
Method to retrieve current variable config
@returns {Object} object containing variables
*/
SDK.prototype.getVariables = function (vars) {
return this.variables;
};
var x = new SDK();
x.POST_RAW_TEXT({
url: 'https://www.google.com'
}, (err, result) => {
console.log(err);
console.log(result.body);
});
console.log(x.getVariables());
module.exports = SDK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment