Skip to content

Commit c607d90

Browse files
committed
Replaced httprequest logic with http-request2 logic for snippet generation
1 parent f00d54e commit c607d90

14 files changed

+41
-22
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

codegens/php-httprequest/lib/index.js codegens/php-httprequest2/lib/index.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ function getHeaders (request, indentation) {
1818
if (!_.isEmpty(headerArray)) {
1919
headerArray = _.reject(headerArray, 'disabled');
2020
headerMap = _.map(headerArray, function (header) {
21-
return `${indentation}'${sanitize(header.key, true)}' => ` +
21+
return `${indentation}'${sanitize(header.key)}' => ` +
2222
`'${sanitize(header.value)}'`;
2323
});
24-
return `$request->setHeaders(array(\n${headerMap.join(',\n')}\n));\n`;
24+
return `$request->setHeader(array(\n${headerMap.join(',\n')}\n));\n`;
2525
}
2626
return '';
2727
}
@@ -76,7 +76,7 @@ self = module.exports = {
7676
options = {};
7777
}
7878
if (!_.isFunction(callback)) {
79-
throw new Error('PHP-HttpRequest-Converter: callback is not valid function');
79+
throw new Error('PHP-HttpRequest2-Converter: callback is not valid function');
8080
}
8181
options = sanitizeOptions(options, self.getOptions());
8282

@@ -85,13 +85,26 @@ self = module.exports = {
8585
indentString = indentString.repeat(options.indentCount);
8686

8787
snippet = '<?php\n';
88-
snippet += '$request = new HttpRequest();\n';
88+
snippet += '$request = new HTTP_Request2();\n';
8989
snippet += `$request->setUrl('${request.url.toString()}');\n`;
90-
snippet += `$request->setMethod(HTTP_METH_${request.method});\n`;
91-
if (options.requestTimeout !== 0 || !options.followRedirect) {
90+
snippet += `$request->setMethod(HTTP_Request2::METHOD_${request.method});\n`;
91+
if (options.requestTimeout !== 0 || options.followRedirect) {
92+
let configArray = [];
9293
snippet += '$request->setOptions(array(';
93-
snippet += options.requestTimeout === 0 ? '' : `'timeout' => ${options.requestTimeout}`;
94-
snippet += options.followRedirect ? '' : ', \'redirect\' => false';
94+
95+
// PHP-HTTP_Request2 method accepts timeout in seconds and it must be an integer
96+
if (options.requestTimeout !== 0 && Number.isInteger(options.requestTimeout / 1000)) {
97+
let requestTimeout = options.requestTimeout;
98+
requestTimeout /= 1000;
99+
configArray.push(`${indentString}'timeout' => ${requestTimeout}`);
100+
}
101+
if (options.followRedirect) {
102+
configArray.push(`${indentString}'redirect' => TRUE`);
103+
}
104+
if (configArray.length) {
105+
snippet += '$request->setOptions(array(\n';
106+
snippet += configArray.join(',\n') + '\n';
107+
}
95108
snippet += '));\n';
96109
}
97110
if (request.body && !request.headers.has('Content-Type')) {
@@ -116,9 +129,15 @@ self = module.exports = {
116129
snippet += `${parseBody(request.toJSON(), indentString, options.trimRequestBody)}`;
117130
}
118131
snippet += 'try {\n';
119-
snippet += `${indentString}echo $request->send()->getBody();\n`;
120-
snippet += '} catch(HttpException $ex) {\n';
121-
snippet += `${indentString}echo $ex;\n}`;
132+
snippet += `${indentString}$response = $request->send();\n`;
133+
snippet += `${indentString}if ($response=>getStatus() == 200) {\n`;
134+
snippet += `${indentString.repeat(2)} echo $response->getBody();\n`;
135+
snippet += `${indentString}} else {\n`;
136+
snippet += `${indentString.repeat(2)}echo 'Unexpected HTTP status: . $response->getStatus() . ' ' .\n`;
137+
snippet += `${indentString.repeat(3)}$response->getReasonPhrase();\n`;
138+
snippet += `${indentString}}\n`;
139+
snippet += '} catch(HTTP_Request2_Exception $e) {\n';
140+
snippet += `${indentString}echo 'Error: ' . $e->getMessage();\n}`;
122141
return callback(null, snippet);
123142
}
124143
};
File renamed without changes.

codegens/php-httprequest/lib/util/parseBody.js codegens/php-httprequest2/lib/util/parseBody.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function (request, indentString, trim) {
99

1010
switch (request.body.mode) {
1111
case 'raw':
12-
bodySnippet += `$request->setBody(${sanitize(request.body[request.body.mode], trim)});\n`;
12+
bodySnippet += `$request->setBody('${sanitize(request.body[request.body.mode], trim)}');\n`;
1313
break;
1414
// eslint-disable-next-line no-case-declarations
1515
case 'graphql':
@@ -21,18 +21,18 @@ module.exports = function (request, indentString, trim) {
2121
catch (e) {
2222
graphqlVariables = {};
2323
}
24-
bodySnippet += `$request->setBody(${sanitize(JSON.stringify({
24+
bodySnippet += `$request->setBody('${sanitize(JSON.stringify({
2525
query: query,
2626
variables: graphqlVariables
27-
}), trim)});\n`;
27+
}), trim)}');\n`;
2828
break;
2929
case 'urlencoded':
3030
enabledBodyList = _.reject(request.body[request.body.mode], 'disabled');
3131
if (!_.isEmpty(enabledBodyList)) {
3232
bodyDataMap = _.map(enabledBodyList, (data) => {
3333
return `${indentString}'${sanitize(data.key, trim)}' => '${sanitize(data.value, trim)}'`;
3434
});
35-
bodySnippet += `$request->setPostFields(array(\n${bodyDataMap.join(',\n')}));\n`;
35+
bodySnippet += `$request->addPostParameter(array(\n${bodyDataMap.join(',\n')}));\n`;
3636
}
3737
break;
3838
case 'formdata':
@@ -45,10 +45,10 @@ module.exports = function (request, indentString, trim) {
4545
return `${indentString}'${sanitize(data.key, trim)}', '${data.src}', <Content-Type Header>`;
4646
});
4747
if (bodyDataMap.length) {
48-
bodySnippet += `$request->setPostFields(array(\n${bodyDataMap.join(',\n')}));\n`;
48+
bodySnippet += `$request->addPostParameter(array(\n${bodyDataMap.join(',\n')}));\n`;
4949
}
5050
if (bodyFileMap.length) {
51-
bodySnippet += `$request->setPostFiles(array(\n${bodyDataMap.join(',\n')}));\n`;
51+
bodySnippet += `$request->addUpload(array(\n${bodyDataMap.join(',\n')}));\n`;
5252
}
5353
}
5454
break;
File renamed without changes.

codegens/php-httprequest/package.json codegens/php-httprequest2/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "@postman/codegen-php-httprequest",
2+
"name": "@postman/codegen-php-httprequest2",
33
"version": "0.0.1",
4-
"description": "Converts Postman SDK Requests to PHP HttpRequest code snippet",
4+
"description": "Converts Postman SDK Requests to PHP HTTP_Request2 code snippet",
55
"main": "index.js",
66
"com_postman_plugin": {
77
"type": "code_generator",
88
"lang": "php",
9-
"variant": "httprequest",
9+
"variant": "HTTP_Request2",
1010
"syntax_mode": "php"
1111
},
1212
"directories": {
@@ -24,7 +24,7 @@
2424
},
2525
"author": "Postman Labs <help@getpostman.com>",
2626
"license": "Apache-2.0",
27-
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/php-httprequest",
27+
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/php-httprequest2",
2828
"dependencies": {},
2929
"devDependencies": {},
3030
"engines": {

codegens/php-httprequest/test/newman/newman.test.js codegens/php-httprequest2/test/newman/newman.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru
22
convert = require('../../index').convert;
33

44

5-
describe('PHP HttpRequest Converter', function () {
5+
describe('PHP HttpRequest2 Converter', function () {
66
describe('convert for different request types', function () {
77
var options = {
88
indentType: 'Space',

0 commit comments

Comments
 (0)