-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrequests.js
More file actions
147 lines (120 loc) · 3.16 KB
/
requests.js
File metadata and controls
147 lines (120 loc) · 3.16 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
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
/**
* @description
* HTTP code snippet generator for Python using Requests
*
* @author
* @montanaflynn
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'
const util = require('util')
const CodeBuilder = require('../../helpers/code-builder')
const helpers = require('./helpers')
const builtInMethods = [
'HEAD',
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
]
module.exports = function (source, options) {
const opts = Object.assign({
indent: ' ',
pretty: true
}, options)
// Start snippet
const code = new CodeBuilder(opts.indent)
// Import requests
code.push('import requests')
.blank()
// Set URL
code.push('url = "%s"', source.url)
.blank()
// Construct query string
let qs
if (Object.keys(source.queryObj).length) {
qs = 'querystring = ' + helpers.literalRepresentation(source.queryObj, opts)
code.push(qs)
.blank()
}
// Construct payload
let hasPayload = false
let jsonPayload = false
switch (source.postData.mimeType) {
case 'application/json':
if (source.postData.jsonObj) {
code.push('payload = %s', helpers.literalRepresentation(source.postData.jsonObj, opts))
jsonPayload = true
hasPayload = true
}
break
case 'application/x-www-form-urlencoded':
if (source.postData.paramsObj) {
code.push('payload = %s', helpers.literalRepresentation(source.postData.paramsObj, opts))
hasPayload = true
break
}
// Otherwise, fall through to treat as plain text:
default: {
const payload = JSON.stringify(source.postData.text)
if (payload) {
code.push('payload = %s', payload)
hasPayload = true
}
}
}
// Construct headers
const headers = source.allHeaders
const headerCount = Object.keys(headers).length
if (headerCount === 1) {
for (const header in headers) {
code.push('headers = { "%s": "%qd" }', header, headers[header])
.blank()
}
} else if (headerCount > 1) {
let count = 1
code.push('headers = {')
for (const header in headers) {
if (count++ !== headerCount) {
code.push(1, '"%s": "%qd",', header, headers[header])
} else {
code.push(1, '"%s": "%qd"', header, headers[header])
}
}
code.push('}')
.blank()
}
// Construct request
const method = source.method
let request = builtInMethods.includes(method)
? util.format('response = requests.%s(url', method.toLowerCase())
: util.format('response = requests.request("%s", url', method)
if (hasPayload) {
if (jsonPayload) {
request += ', json=payload'
} else {
request += ', data=payload'
}
}
if (headerCount > 0) {
request += ', headers=headers'
}
if (qs) {
request += ', params=querystring'
}
request += ')'
code.push(request)
.blank()
// Print response
.push('print(response.text)')
return code.join()
}
module.exports.info = {
key: 'requests',
title: 'Requests',
link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
description: 'Requests HTTP library'
}