forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
99 lines (86 loc) · 2.77 KB
/
client.ts
File metadata and controls
99 lines (86 loc) · 2.77 KB
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
/**
* @description
* HTTP code snippet generator for native Python3.
*
* @author
* @montanaflynn
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';
export interface Python3Options {
insecureSkipVerify?: boolean;
}
export const python3: Client<Python3Options> = {
info: {
key: 'python3',
title: 'http.client',
link: 'https://docs.python.org/3/library/http.client.html',
description: 'Python3 HTTP Client',
},
convert: ({ uriObj: { path, protocol, host }, postData, allHeaders, method }, options = {}) => {
const { insecureSkipVerify = false } = options;
const { push, blank, join } = new CodeBuilder();
// Start Request
push('import http.client');
if (insecureSkipVerify) {
push('import ssl');
}
blank();
// Check which protocol to be used for the client connection
if (protocol === 'https:') {
const sslContext = insecureSkipVerify ? ', context = ssl._create_unverified_context()' : '';
push(`conn = http.client.HTTPSConnection("${host}"${sslContext})`);
blank();
} else {
push(`conn = http.client.HTTPConnection("${host}")`);
blank();
}
// Create payload string if it exists
const payload = JSON.stringify(postData.text);
if (payload) {
push(`payload = ${payload}`);
blank();
}
// Create Headers
const headers = allHeaders;
const headerCount = Object.keys(headers).length;
if (headerCount === 1) {
for (const header in headers) {
push(`headers = { '${header}': "${escapeForDoubleQuotes(headers[header])}" }`);
blank();
}
} else if (headerCount > 1) {
let count = 1;
push('headers = {');
for (const header in headers) {
if (count++ !== headerCount) {
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}",`);
} else {
push(` '${header}': "${escapeForDoubleQuotes(headers[header])}"`);
}
}
push('}');
blank();
}
// Make Request
if (payload && headerCount) {
push(`conn.request("${method}", "${path}", payload, headers)`);
} else if (payload && !headerCount) {
push(`conn.request("${method}", "${path}", payload)`);
} else if (!payload && headerCount) {
push(`conn.request("${method}", "${path}", headers=headers)`);
} else {
push(`conn.request("${method}", "${path}")`);
}
// Get Response
blank();
push('res = conn.getresponse()');
push('data = res.read()');
blank();
push('print(data.decode("utf-8"))');
return join();
},
};