forked from postmanlabs/postman-code-generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestsharp.js
221 lines (204 loc) · 8.61 KB
/
restsharp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var _ = require('./lodash'),
parseRequest = require('./parseRequest'),
sanitize = require('./util').sanitize,
sanitizeOptions = require('./util').sanitizeOptions,
addFormParam = require('./util').addFormParam,
self;
/**
* Generates snippet in csharp-restsharp by parsing data from Postman-SDK request object
*
* @param {Object} request - Postman SDK request object
* @param {Object} options - Options to tweak code snippet
* @returns {String} csharp-restsharp code snippet for given request object
*/
function makeSnippet (request, options) {
const UNSUPPORTED_METHODS_LIKE_POST = ['LINK', 'UNLINK', 'LOCK', 'PROPFIND'],
UNSUPPORTED_METHODS_LIKE_GET = ['PURGE', 'UNLOCK', 'VIEW', 'COPY'];
var snippet = `var client = new RestClient("${sanitize(request.url.toString())}");\n`,
isUnSupportedMethod = UNSUPPORTED_METHODS_LIKE_GET.includes(request.method) ||
UNSUPPORTED_METHODS_LIKE_POST.includes(request.method);
if (options.requestTimeout) {
snippet += `client.Timeout = ${options.requestTimeout};\n`;
}
else {
snippet += 'client.Timeout = -1;\n';
}
if (!options.followRedirect) {
snippet += 'client.FollowRedirects = false;\n';
}
snippet += `var request = new RestRequest(${isUnSupportedMethod ? '' : ('Method.' + request.method)});\n`;
if (request.body && request.body.mode === 'graphql' && !request.headers.has('Content-Type')) {
request.addHeader({
key: 'Content-Type',
value: 'application/json'
});
}
snippet += parseRequest.parseHeader(request.toJSON(), options.trimRequestBody);
if (request.body && request.body.mode === 'formdata') {
let isFile = false,
formdata = request.body.formdata,
formdataArray = [];
request.body.toJSON().formdata.forEach((data) => {
if (!data.disabled && data.type === 'file') {
isFile = true;
}
});
// The following statement needs to be added else the multipart/form-data request where there is no file
// is being sent as x-www-form-urlencoded by default
if (!isFile) {
snippet += 'request.AlwaysMultipartFormData = true;\n';
}
// The following code handles multiple files in the same formdata param.
// It removes the form data params where the src property is an array of filepath strings
// Splits that array into different form data params with src set as a single filepath string
formdata.members.forEach((param) => {
let key = param.key,
type = param.type,
disabled = param.disabled,
contentType = param.contentType;
// check if type is file or text
if (type === 'file') {
// if src is not of type string we check for array(multiple files)
if (typeof param.src !== 'string') {
// if src is an array(not empty), iterate over it and add files as separate form fields
if (Array.isArray(param.src) && param.src.length) {
param.src.forEach((filePath) => {
addFormParam(formdataArray, key, param.type, filePath, disabled, contentType);
});
}
// if src is not an array or string, or is an empty array, add a placeholder for file path(no files case)
else {
addFormParam(formdataArray, key, param.type, '/path/to/file', disabled, contentType);
}
}
// if src is string, directly add the param with src as filepath
else {
addFormParam(formdataArray, key, param.type, param.src, disabled, contentType);
}
}
// if type is text, directly add it to formdata array
else {
addFormParam(formdataArray, key, param.type, param.value, disabled, contentType);
}
});
request.body.update({
mode: 'formdata',
formdata: formdataArray
});
}
snippet += parseRequest.parseBody(request, options.trimRequestBody);
if (isUnSupportedMethod) {
(UNSUPPORTED_METHODS_LIKE_GET.includes(request.method)) &&
(snippet += `IRestResponse response = client.ExecuteAsGet(request, "${request.method}");\n`);
(UNSUPPORTED_METHODS_LIKE_POST.includes(request.method)) &&
(snippet += `IRestResponse response = client.ExecuteAsPost(request, "${request.method}");\n`);
}
else {
snippet += 'IRestResponse response = client.Execute(request);\n';
}
snippet += 'Console.WriteLine(response.Content);';
return snippet;
}
self = module.exports = {
/**
* Used in order to get additional options for generation of C# code snippet (i.e. Include Boilerplate code)
*
* @module getOptions
*
* @returns {Array} Additional options specific to generation of csharp-restsharp code snippet
*/
getOptions: function () {
return [
{
name: 'Include boilerplate',
id: 'includeBoilerplate',
type: 'boolean',
default: false,
description: 'Include class definition and import statements in snippet'
},
{
name: 'Set indentation count',
id: 'indentCount',
type: 'positiveInteger',
default: 2,
description: 'Set the number of indentation characters to add per code level'
},
{
name: 'Set indentation type',
id: 'indentType',
type: 'enum',
availableOptions: ['Tab', 'Space'],
default: 'Space',
description: 'Select the character used to indent lines of code'
},
{
name: 'Set request timeout',
id: 'requestTimeout',
type: 'positiveInteger',
default: 0,
description: 'Set number of milliseconds the request should wait for a response ' +
'before timing out (use 0 for infinity)'
},
{
name: 'Follow redirects',
id: 'followRedirect',
type: 'boolean',
default: true,
description: 'Automatically follow HTTP redirects'
},
{
name: 'Trim request body fields',
id: 'trimRequestBody',
type: 'boolean',
default: false,
description: 'Remove white space and additional lines that may affect the server\'s response'
}
];
},
/**
* Converts Postman sdk request object to csharp-restsharp code snippet
*
* @module convert
*
* @param {Object} request - Postman-SDK request object
* @param {Object} options - Options to tweak code snippet generated in C#
* @param {String} options.indentType - type for indentation eg: Space, Tab (default: Space)
* @param {String} options.indentCount - number of spaces or tabs for indentation. (default: 4 for indentType:
Space, default: 1 for indentType: Tab)
* @param {Boolean} [options.includeBoilerplate] - indicates whether to include class definition in C#
* @param {Boolean} options.followRedirect - whether to enable followredirect
* @param {Boolean} options.trimRequestBody - whether to trim fields in request body or not (default: false)
* @param {Number} options.requestTimeout - time in milli-seconds after which request will bail out
(default: 0 -> never bail out)
* @param {Function} callback - Callback function with parameters (error, snippet)
* @returns {String} Generated C# snippet via callback
*/
convert: function (request, options, callback) {
if (!_.isFunction(callback)) {
throw new Error('C#-RestSharp-Converter: Callback is not valid function');
}
// String representing value of indentation required
var indentString,
// snippets to include C# class definition according to options
headerSnippet = '',
footerSnippet = '',
// snippet to create request in csharp-restsharp
snippet = '';
options = sanitizeOptions(options, self.getOptions());
indentString = options.indentType === 'Tab' ? '\t' : ' ';
indentString = indentString.repeat(options.indentCount);
if (options.includeBoilerplate) {
headerSnippet = 'using System;\n' +
'using RestSharp;\n' +
'namespace HelloWorldApplication {\n' +
indentString + 'class HelloWorld {\n' +
indentString.repeat(2) + 'static void Main(string[] args) {\n';
footerSnippet = indentString.repeat(2) + '}\n' + indentString + '}\n}\n';
}
snippet = makeSnippet(request, options);
// if boilerplate is included then two more indentString needs to be added in snippet
(options.includeBoilerplate) &&
(snippet = indentString.repeat(3) + snippet.split('\n').join('\n' + indentString.repeat(3)) + '\n');
return callback(null, headerSnippet + snippet + footerSnippet);
}
};