Skip to content

Commit 1e6294d

Browse files
committed
use sdk.Url.parse in python-http.client
1 parent 43203b0 commit 1e6294d

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

codegens/python-http.client/lib/python-httpclient.js

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('./lodash'),
2+
sdk = require('postman-collection'),
23
sanitize = require('./util/sanitize').sanitize,
34
sanitizeOptions = require('./util/sanitize').sanitizeOptions,
45
addFormParam = require('./util/sanitize').addFormParam,
@@ -35,24 +36,6 @@ function getheaders (request, indentation) {
3536
return 'headers = {}\n';
3637
}
3738

38-
/**
39-
* Generates URL's path with query string
40-
*
41-
* @param {Object} requestUrl - Postman Sdk Request's Url object
42-
* @returns {String} - Url path with query (no host)
43-
*/
44-
function getUrlPathWithQuery (requestUrl) {
45-
var path = sanitize(requestUrl.getPath()),
46-
query = requestUrl.getQueryString({ ignoreDisabled: true }),
47-
urlPathWithQuery = '';
48-
49-
urlPathWithQuery += (path === '/' ? '' : path);
50-
if (query !== '') {
51-
urlPathWithQuery += '?' + sanitize(query);
52-
}
53-
return urlPathWithQuery;
54-
}
55-
5639
self = module.exports = {
5740
/**
5841
* Used to return options which are specific to a particular plugin
@@ -115,7 +98,29 @@ self = module.exports = {
11598
convert: function (request, options, callback) {
11699
var snippet = '',
117100
indentation = '',
118-
identity = '';
101+
identity = '',
102+
url, host, path, query;
103+
104+
try {
105+
url = sdk.Url.parse(request.url);
106+
}
107+
catch (e) {
108+
url = request.url;
109+
}
110+
111+
host = url.host ? url.host.join('.') : '';
112+
path = url.path ? '/' + url.path.join('/') : '/';
113+
query = url.query ? _.reduce(url.query, (accum, q) => {
114+
accum.push(`${q.key}=${q.value}`);
115+
return accum;
116+
}, []) : [];
117+
118+
if (query.length > 0) {
119+
query = '?' + query.join('&');
120+
}
121+
else {
122+
query = '';
123+
}
119124

120125
if (_.isFunction(options)) {
121126
callback = options;
@@ -135,8 +140,8 @@ self = module.exports = {
135140
snippet += 'from codecs import encode\n';
136141
}
137142
snippet += '\n';
138-
snippet += `conn = http.client.HTTPSConnection("${sanitize(request.url.host ? request.url.host.join('.') : '')}"`;
139-
snippet += request.url.port ? `, ${request.url.port}` : '';
143+
snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`;
144+
snippet += url.port ? `, ${request.url.port}` : '';
140145
snippet += options.requestTimeout !== 0 ? `, timeout = ${options.requestTimeout})\n` : ')\n';
141146

142147
// The following code handles multiple files in the same formdata param.
@@ -196,7 +201,8 @@ self = module.exports = {
196201
}
197202
}
198203
snippet += getheaders(request, indentation);
199-
snippet += `conn.request("${request.method}", "${getUrlPathWithQuery(request.url)}", payload, headers)\n`;
204+
snippet += `conn.request("${request.method}",` +
205+
` "${sanitize(path)}${sanitize(encodeURI(query))}", payload, headers)\n`;
200206
snippet += 'res = conn.getresponse()\n';
201207
snippet += 'data = res.read()\n';
202208
snippet += 'print(data.decode("utf-8"))';

0 commit comments

Comments
 (0)