Skip to content

Commit be23f30

Browse files
committed
Read variables in collection (closes #2)
1 parent b622e59 commit be23f30

12 files changed

+152
-77
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Version 0.2.0 (under dev)
44

5+
- Read variables in collection (issue #2).
56
- Import Postman environment with a collection, add support of variables, add option `impostman-use-variables` (issue #1).
67

78
## Version 0.1.0 (2021-01-11)

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Function called when the output buffer is created and before parsing the collect
144144

145145
Arguments:
146146

147-
- `variables` (alist): variables from environment.
147+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
148148

149149
#### replace-vars
150150

@@ -160,7 +160,7 @@ Note: according to the option `impostman-use-variables`, a variable is either re
160160
Arguments:
161161

162162
- `string` (string): any string
163-
- `variables` (alist): variables from environment.
163+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
164164

165165
#### header
166166

@@ -175,7 +175,7 @@ Arguments:
175175

176176
- `name` (string): collection name (`unknown` if not found)
177177
- `description` (string): collection description
178-
- `variables` (alist): variables from environment.
178+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
179179

180180
#### item
181181

@@ -191,7 +191,7 @@ Arguments:
191191
- `level` (integer): folder level (≥ 2)
192192
- `name` (string): item name
193193
- `description` (string): item description
194-
- `variables` (alist): variables from environment.
194+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
195195

196196
#### request
197197

@@ -207,9 +207,9 @@ Arguments:
207207
- `description` (string): request description
208208
- `method` (string): the HTTP method (`GET`, `POST`, `PUT`, …)
209209
- `url` (string): request URL
210-
- `headers` (alist): request headers
210+
- `headers` (alist): request headers (in reverse order: latest header read is the first one in alist)
211211
- `body` (string): request body
212-
- `variables` (alist): variables from environment.
212+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
213213

214214
#### footer
215215

@@ -223,7 +223,7 @@ Function called at the end of parsing. It must return a string which is inserted
223223
Arguments:
224224

225225
- `name` (string): collection name (`unknown` if not found)
226-
- `variables` (alist): variables from environment.
226+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
227227

228228
#### end
229229

@@ -236,7 +236,7 @@ Function called at the end. It can be used to enable a major or minor mode.
236236

237237
Arguments:
238238

239-
- `variables` (alist): variables from environment.
239+
- `variables` (alist): variables from collection and environment (in reverse order: latest variable read is the first one in alist).
240240

241241
## Known limitations
242242

impostman.el

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Return nil if the authentication is not Basic or if the base64 is invalid."
129129
"Replace variables in a string, using verb syntax.
130130
131131
STRING any string that can contain variables with format \"{{variable}}\".
132-
VARIABLES is a alist with Postman environment variables."
132+
VARIABLES is a alist with Postman variables."
133133
(if impostman-use-variables
134134
(replace-regexp-in-string
135135
"{{\\([^}]+\\)}}" "{{(verb-var \\1)}}" (or string ""))
@@ -146,7 +146,7 @@ VARIABLES is a alist with Postman environment variables."
146146
147147
NAME is the collection name.
148148
DESCRIPTION is the collection description.
149-
VARIABLES is a alist with Postman environment variables."
149+
VARIABLES is a alist with Postman variables."
150150
(ignore variables)
151151
(concat
152152
"* " name " :verb:" "\n"
@@ -157,7 +157,8 @@ VARIABLES is a alist with Postman environment variables."
157157
158158
LEVEL is the level.
159159
NAME is the item name.
160-
DESCRIPTION is the item description."
160+
DESCRIPTION is the item description.
161+
VARIABLES is a alist with Postman variables."
161162
(ignore variables)
162163
(concat
163164
(if (<= level 2) "\n" "")
@@ -171,10 +172,11 @@ DESCRIPTION is the request description.
171172
METHOD is the HTTP method.
172173
URL is the URL.
173174
HEADERS is an alist with HTTP headers.
174-
BODY is the request body."
175+
BODY is the request body.
176+
VARIABLES is a alist with Postman variables."
175177
(ignore variables)
176178
(let (list-headers)
177-
(dolist (header headers)
179+
(dolist (header (nreverse headers))
178180
(let* ((header-name (car header))
179181
(header-value (cdr header))
180182
(new-value header-value))
@@ -199,10 +201,10 @@ BODY is the request body."
199201
"Format the verb footer.
200202
201203
NAME is the collection name.
202-
VARIABLES is a alist with Postman environment variables."
204+
VARIABLES is a alist with Postman variables."
203205
(let (list-vars)
204206
(when impostman-use-variables
205-
(dolist (var variables)
207+
(dolist (var (nreverse variables))
206208
(push
207209
(format "# eval: (verb-set-var \"%s\" \"%s\")" (car var) (cdr var))
208210
list-vars)))
@@ -217,14 +219,16 @@ VARIABLES is a alist with Postman environment variables."
217219
"# End:\n")))
218220

219221
(defun impostman-output-verb-end (variables)
220-
"Function evaluated at the end."
222+
"Function evaluated at the end.
223+
224+
VARIABLES is a alist with Postman variables."
221225
(when (fboundp 'org-mode)
222226
(org-mode))
223227
(when (fboundp 'verb-mode)
224228
(verb-mode))
225229
;; evaluate variables now
226230
(when (and impostman-use-variables (fboundp 'verb-set-var))
227-
(dolist (var variables)
231+
(dolist (var (nreverse variables))
228232
(verb-set-var (car var) (cdr var)))))
229233

230234
;; restclient output
@@ -253,7 +257,7 @@ DESCRIPTION is the collection description.
253257
VARIABLES is a alist with Postman environment variables."
254258
(let (list-vars)
255259
(when impostman-use-variables
256-
(dolist (var variables)
260+
(dolist (var (nreverse variables))
257261
(push (format ":%s = %s" (car var) (cdr var)) list-vars)))
258262
(concat
259263
"# -*- restclient -*-\n"
@@ -286,7 +290,7 @@ HEADERS is an alist with HTTP headers.
286290
BODY is the request body."
287291
(ignore variables)
288292
(let (list-variables list-headers)
289-
(dolist (header headers)
293+
(dolist (header (nreverse headers))
290294
(let* ((header-name (car header))
291295
(header-value (cdr header))
292296
(new-value header-value))
@@ -381,7 +385,7 @@ HEADER is a vector with hash tables."
381385
(key (gethash "key" header-item ""))
382386
(value (gethash "value" header-item "")))
383387
(push (cons key value) headers)))
384-
(nreverse headers)))
388+
headers))
385389

386390
(defun impostman--build-auth-query-string (auth)
387391
"Return query string parameter to add for authentication as an alist, for
@@ -424,25 +428,28 @@ QUERY-STRING is nil or an alist with query strings to add."
424428
url)
425429

426430
(defun impostman--build-variables (values)
427-
"Return alist with variables using values from Postman environment.
431+
"Return alist with variables using values from Postman collection and
432+
environment.
428433
429-
VALUES is the \"values\" read from environment (vector)."
434+
VALUES is the \"variable\" read from collection (vector) or \"values\" read
435+
from environment (vector) (or concatenation of both)."
430436
(let (variables)
431437
(dotimes (i (length (or values [])))
432438
(let* ((item (elt values i))
433439
(key (gethash "key" item ""))
434440
(value (gethash "value" item ""))
435-
(enabled (equal t (gethash "enabled" item t))))
436-
(when enabled
441+
(enabled (equal t (gethash "enabled" item t)))
442+
(disabled (equal t (gethash "disabled" item nil))))
443+
(when (and enabled (not disabled))
437444
(push (cons key value) variables))))
438-
(nreverse variables)))
445+
variables))
439446

440447
(defun impostman--parse-item (items level variables output-alist)
441448
"Parse a Postman collection item.
442449
443450
ITEMS is the \"item\" read from collection (vector).
444451
LEVEL is the level.
445-
VARIABLES is a alist with Postman environment variables.
452+
VARIABLES is a alist with Postman variables.
446453
OUTPUT-ALIST is an alist with the output callbacks."
447454
(dotimes (i (length items))
448455
(let* ((item (elt items i))
@@ -467,7 +474,7 @@ OUTPUT-ALIST is an alist with the output callbacks."
467474
(gethash "url" request (make-hash-table)) ""))
468475
(auth-headers (impostman--build-auth-headers auth))
469476
(other-headers (impostman--build-headers header))
470-
(headers (append auth-headers other-headers))
477+
(headers (append other-headers auth-headers))
471478
(replace-vars (alist-get 'replace-vars output-alist)))
472479
(setq url (impostman--add-query-string-items-to-url
473480
url
@@ -496,16 +503,18 @@ OUTPUT-ALIST is an alist with the output callbacks."
496503
(gethash "info"
497504
collection (make-hash-table)) ""))
498505
(item (gethash "item" collection []))
506+
(variable (gethash "variable" collection []))
499507
(values (gethash "values" environment []))
500-
(variables (impostman--build-variables values)))
508+
(all-variables (impostman--build-variables (vconcat
509+
variable values))))
501510
(pop-to-buffer (generate-new-buffer (concat name ".org")))
502-
(funcall (alist-get 'init output-alist) variables)
511+
(funcall (alist-get 'init output-alist) all-variables)
503512
(insert (funcall (alist-get 'header output-alist)
504-
name description variables))
505-
(impostman--parse-item item 2 variables output-alist)
506-
(insert (funcall (alist-get 'footer output-alist) name variables))
513+
name description all-variables))
514+
(impostman--parse-item item 2 all-variables output-alist)
515+
(insert (funcall (alist-get 'footer output-alist) name all-variables))
507516
(goto-char (point-min))
508-
(funcall (alist-get 'end output-alist) variables)
517+
(funcall (alist-get 'end output-alist) all-variables)
509518
values))
510519

511520
;;;###autoload

tests/custom.org

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
# - Authentication
55
# - Anything.
66

7+
VAR(var1) = value1_collection
78
VAR(url) = https://httpbin.org
89
VAR(variable_delete) = value_delete
910
VAR(variable_get) = value_get
1011
VAR(header-api-key) = x-api-key
1112
VAR(query-api-key) = key
1213
VAR(api-key) = my_secret_key
14+
VAR(var1) = value1_env
1315

1416
** http_methods
1517
*** delete
@@ -38,7 +40,8 @@ x-test2: second_value
3840
"list": [
3941
"first",
4042
"second"
41-
]
43+
],
44+
"var1": "$(var1)"
4245
}
4346
*** put
4447
# A PUT request.

tests/custom_no_vars.org

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ x-test2: second_value
3131
"list": [
3232
"first",
3333
"second"
34-
]
34+
],
35+
"var1": "value1_env"
3536
}
3637
*** put
3738
# A PUT request.

tests/httpbin.postman_collection.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
],
147147
"body": {
148148
"mode": "raw",
149-
"raw": "{\n \"test\": \"value\",\n \"list\": [\n \"first\",\n \"second\"\n ]\n}"
149+
"raw": "{\n \"test\": \"value\",\n \"list\": [\n \"first\",\n \"second\"\n ],\n \"var1\": \"{{var1}}\"\n}"
150150
},
151151
"url": {
152152
"raw": "{{url}}/post?search=test",
@@ -379,5 +379,16 @@
379379
]
380380
}
381381
}
382+
],
383+
"variable": [
384+
{
385+
"key": "var1",
386+
"value": "value1_collection"
387+
},
388+
{
389+
"key": "var2-not-enabled",
390+
"value": "value2_collection",
391+
"disabled": true
392+
}
382393
]
383394
}

tests/httpbin.postman_environment.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@
3232
"value": "my_secret_key",
3333
"enabled": true
3434
},
35+
{
36+
"key": "var1",
37+
"value": "value1_env",
38+
"enabled": true
39+
},
3540
{
3641
"key": "not-enabled",
3742
"value": "some_value",
3843
"enabled": false
3944
}
4045
],
4146
"_postman_variable_scope": "environment",
42-
"_postman_exported_at": "2021-01-17T11:54:06.814Z",
43-
"_postman_exported_using": "Postman/7.36.1"
47+
"_postman_exported_at": "2021-08-14T10:06:37.886Z",
48+
"_postman_exported_using": "Postman/8.10.0"
4449
}

0 commit comments

Comments
 (0)