forked from flashcode/impostman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impostman.el
621 lines (538 loc) · 22.9 KB
/
impostman.el
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
;;; impostman.el --- Import Postman collections -*- lexical-binding: t -*-
;; Copyright (C) 2020-2021 Sébastien Helleu <[email protected]>
;; Author: Sébastien Helleu <[email protected]>
;; Maintainer: Sébastien Helleu <[email protected]>
;; Created: 2020-12-24
;; Keywords: tools
;; URL: https://github.com/flashcode/impostman
;; Package-Version: 0.2.0-snapshot
;; Package-Requires: ((emacs "27.1"))
;; This file is not part of GNU Emacs.
;; Impostman is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; Impostman is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Impostman. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;; Import Postman collections/environments to use them with your favorite
;; Emacs HTTP client:
;; - verb
;; - restclient
;; - your custom output.
;;; Code:
(require 'subr-x)
;; customization
(defgroup impostman nil
"Import Postman collections and environments."
:prefix "impostman-"
:group 'tools)
(defcustom impostman-auth-basic-as-elisp-code t
"Convert Basic authentication header to elisp code so that the username
and password can be easily edited.
Example with verb:
Authorization: Basic {{(base64-encode-string (encode-coding-string \"username:password\" 'utf-8) t)}}
Example with restclient:
:auth := (format \"Basic %s\" (base64-encode-string (encode-coding-string \"username:password\" 'utf-8) t))
Authorization: :auth
If nil, the username and password are directly encoded in base64:
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ="
:type 'boolean)
(defcustom impostman-use-variables t
"Keep Postman variables in the output and define variables according to the
output.
If nil, no variables are used, they are directly replaced by their values
during the import of collection."
:type 'boolean)
(defconst impostman-version "0.2.0-snapshot"
"Impostman package version")
(defconst impostman-output-verb-alist
'((init . ignore)
(replace-vars . impostman-output-verb-replace-vars)
(header . impostman-output-verb-header)
(item . impostman-output-verb-item)
(request . impostman-output-verb-request)
(footer . impostman-output-verb-footer)
(end . impostman-output-verb-end))
"Emacs verb output")
(defconst impostman-output-restclient-alist
'((init . ignore)
(replace-vars . impostman-output-restclient-replace-vars)
(header . impostman-output-restclient-header)
(item . impostman-output-restclient-item)
(request . impostman-output-restclient-request)
(footer . impostman-output-restclient-footer)
(end . impostman-output-restclient-end))
"Emacs restclient output")
(defcustom impostman-outputs-alist
'(("verb" . impostman-output-verb-alist)
("restclient" . impostman-output-restclient-alist))
"Impostman outputs"
:type '(alist :key-type string :value-type function))
;; functions common to all outputs
(defun impostman-format-comment (comment &optional prefix)
"Format a comment, which can be on multiple lines.
COMMENT is a string, where multiple lines are separated by \"\\n\".
PREFIX is the prefix to add in front of each line (default is \"# \")."
(let ((comment (or comment ""))
(prefix (or prefix "# ")))
(if (string-empty-p comment)
""
(concat
prefix
(replace-regexp-in-string "\n" (concat "\n" prefix) comment)
"\n"))))
(defun impostman-get-auth-basic-plain (authorization)
"Get the plain-text \"username:password\" with the value of the
Authorization header, if it is Basic authentication.
For example with the value \"Basic dXNlcm5hbWU6cGFzc3dvcmQ=\",
the function returns \"username:password\".
Return nil if the authentication is not Basic or if the base64 is invalid."
(save-match-data
(when (string-match "^Basic \\(.*\\)" authorization)
(ignore-errors (base64-decode-string (match-string 1 authorization))))))
;; verb output
(defun impostman-output-verb-replace-vars (string variables)
"Replace variables in a string, using verb syntax.
STRING any string that can contain variables with format \"{{variable}}\".
VARIABLES is a alist with Postman environment variables."
(if impostman-use-variables
(replace-regexp-in-string
"{{\\([^}]+\\)}}" "{{(verb-var \\1)}}" (or string ""))
(replace-regexp-in-string
"{{[^}]+}}"
(lambda (s)
(let* ((name (substring s 2 -2))
(var (assoc name variables)))
(if var (cdr var) name)))
(or string ""))))
(defun impostman-output-verb-header (name description variables)
"Format the verb header.
NAME is the collection name.
DESCRIPTION is the collection description.
VARIABLES is a alist with Postman environment variables."
(ignore variables)
(concat
"* " name " :verb:" "\n"
(impostman-format-comment description)))
(defun impostman-output-verb-item (level name description variables)
"Format a verb item.
LEVEL is the level.
NAME is the item name.
DESCRIPTION is the item description."
(ignore variables)
(concat
(if (<= level 2) "\n" "")
(make-string (max level 1) ?*) " " name "\n"
(impostman-format-comment description)))
(defun impostman-output-verb-request (description method url headers body variables)
"Format a verb request.
DESCRIPTION is the request description.
METHOD is the HTTP method.
URL is the URL.
HEADERS is an alist with HTTP headers.
BODY is the request body."
(ignore variables)
(let (list-headers)
(dolist (header headers)
(let* ((header-name (car header))
(header-value (cdr header))
(new-value header-value))
(when (and impostman-auth-basic-as-elisp-code
(string= header-name "Authorization"))
(let ((auth-plain (impostman-get-auth-basic-plain header-value)))
(when auth-plain
(setq new-value
(concat
"Basic "
"{{(base64-encode-string (encode-coding-string \""
auth-plain "\" 'utf-8) t)}}")))))
(push (concat header-name ": " new-value) list-headers)))
(concat
(impostman-format-comment description)
(downcase method) " " url "\n"
(when list-headers
(concat (string-join (nreverse list-headers) "\n") "\n"))
(if (string-empty-p body) "" (concat "\n" body "\n")))))
(defun impostman-output-verb-footer (name variables)
"Format the verb footer.
NAME is the collection name.
VARIABLES is a alist with Postman environment variables."
(let (list-vars)
(when impostman-use-variables
(dolist (var variables)
(push
(format "# eval: (verb-set-var \"%s\" \"%s\")" (car var) (cdr var))
list-vars)))
(concat
"\n"
"* End of " name "\n"
"\n"
"# Local Variables:\n"
"# eval: (verb-mode)\n"
(when list-vars
(concat (string-join (nreverse list-vars) "\n") "\n"))
"# End:\n")))
(defun impostman-output-verb-end (variables)
"Function evaluated at the end."
(when (fboundp 'org-mode)
(org-mode))
(when (fboundp 'verb-mode)
(verb-mode))
;; evaluate variables now
(when (and impostman-use-variables (fboundp 'verb-set-var))
(dolist (var variables)
(verb-set-var (car var) (cdr var)))))
;; restclient output
(defun impostman-output-restclient-replace-vars (string variables)
"Replace variables in a string, using restclient syntax.
STRING any string that can contain variables with format \"{{variable}}\".
VARIABLES is a alist with Postman environment variables."
(if impostman-use-variables
(replace-regexp-in-string
"{{\\([^}]+\\)}}" ":\\1" (or string ""))
(replace-regexp-in-string
"{{[^}]+}}"
(lambda (s)
(let* ((name (substring s 2 -2))
(var (assoc name variables)))
(if var (cdr var) name)))
(or string ""))))
(defun impostman-output-restclient-header (name description variables)
"Format the restclient header.
NAME is the collection name.
DESCRIPTION is the collection description.
VARIABLES is a alist with Postman environment variables."
(let (list-vars)
(when impostman-use-variables
(dolist (var variables)
(push (format ":%s = %s" (car var) (cdr var)) list-vars)))
(concat
"# -*- restclient -*-\n"
"#\n"
"# " name "\n"
(impostman-format-comment description)
"#\n"
(when list-vars
(concat "\n" (string-join (nreverse list-vars) "\n") "\n")))))
(defun impostman-output-restclient-item (level name description variables)
"Format a restclient item.
LEVEL is the level.
NAME is the item name.
DESCRIPTION is the item description."
(ignore variables)
(concat
(if (<= level 2) "\n" "")
(make-string (max level 1) ?#) " " name "\n"
(impostman-format-comment description)))
(defun impostman-output-restclient-request (description method url headers body variables)
"Format a restclient request.
DESCRIPTION is the request description.
METHOD is the HTTP method.
URL is the URL.
HEADERS is an alist with HTTP headers.
BODY is the request body."
(ignore variables)
(let (list-variables list-headers)
(dolist (header headers)
(let* ((header-name (car header))
(header-value (cdr header))
(new-value header-value))
(when (and impostman-auth-basic-as-elisp-code
(string= header-name "Authorization"))
(let ((auth-plain (impostman-get-auth-basic-plain header-value)))
(when auth-plain
(push (concat
":auth := (format \"Basic %s\" "
"(base64-encode-string (encode-coding-string \""
auth-plain "\" 'utf-8) t))")
list-variables)
(setq new-value ":auth"))))
(push (concat header-name ": " new-value) list-headers)))
(concat
(impostman-format-comment description)
(when list-variables
(concat (string-join (nreverse list-variables) "\n") "\n"))
method " " url "\n"
(when list-headers
(concat (string-join (nreverse list-headers) "\n") "\n"))
(if (string-empty-p body) "" (concat body "\n")))))
(defun impostman-output-restclient-footer (name variables)
"Format the restclient footer.
NAME is the collection name.
VARIABLES is a alist with Postman environment variables."
(ignore variables)
(concat
"\n"
"# End of " name "\n"))
(defun impostman-output-restclient-end (variables)
"Function evaluated at the end."
(ignore variables)
(when (fboundp 'restclient-mode)
(restclient-mode)))
;; Postman collection parser
(defun impostman--build-auth-headers (auth)
"Return an alist with headers, based on the `auth' JSON item.
AUTH is a hash table."
(let* (headers
(auth (or auth (make-hash-table :test 'equal)))
(auth-type (gethash "type" auth "")))
(cond ((string= auth-type "basic")
(let (username password (basic (gethash "basic" auth [])))
(dotimes (i (length basic))
(let* ((item (elt basic i))
(key (gethash "key" item ""))
(value (gethash "value" item "")))
(cond ((string= key "username")
(setq username value))
((string= key "password")
(setq password value)))))
(when (or username password)
(let ((auth-base64
(base64-encode-string
(encode-coding-string
(concat username ":" password) 'utf-8))))
(push
(cons "Authorization" (concat "Basic " auth-base64))
headers)))))
((string= auth-type "apikey")
(let ((apikey (gethash "apikey" auth []))
apikey-key apikey-value apikey-in)
(dotimes (i (length apikey))
(let* ((apikey-item (elt apikey i))
(key (gethash "key" apikey-item ""))
(value (gethash "value" apikey-item "")))
(cond ((string= key "key")
(setq apikey-key value))
((string= key "value")
(setq apikey-value value))
((string= key "in")
(setq apikey-in value)))))
(when (and (not (string= apikey-in "query"))
(or apikey-key apikey-value))
(push (cons apikey-key apikey-value) headers)))))
(nreverse headers)))
(defun impostman--build-headers (header)
"Return an alist with headers, based on the `header' JSON item.
HEADER is a vector with hash tables."
(let (headers)
(dotimes (i (length header))
(let* ((header-item (elt header i))
(key (gethash "key" header-item ""))
(value (gethash "value" header-item "")))
(push (cons key value) headers)))
(nreverse headers)))
(defun impostman--build-auth-query-string (auth)
"Return query string parameter to add for authentication as an alist, for
example: (\"key\" . \"value\"), or nil if there's no query string to add.
AUTH is a hash table."
(let (query-string-items)
(when auth
(let ((auth-type (gethash "type" auth "")))
(when (string= auth-type "apikey")
(let ((apikey (gethash "apikey" auth []))
apikey-key apikey-value apikey-in)
(dotimes (i (length apikey))
(let* ((apikey-item (elt apikey i))
(key (gethash "key" apikey-item ""))
(value (gethash "value" apikey-item "")))
(cond ((string= key "key")
(setq apikey-key value))
((string= key "value")
(setq apikey-value value))
((string= key "in")
(setq apikey-in value)))))
(when (and (string= apikey-in "query")
(or apikey-key apikey-value))
(push (cons apikey-key apikey-value) query-string-items))))))
(nreverse query-string-items)))
(defun impostman--add-query-string-items-to-url (url query-string-items)
"Return the URL with updated query string parameters.
URL is a string.
QUERY-STRING is nil or an alist with query strings to add."
(dolist (query-string query-string-items)
(setq url (concat
url
(if (string-match-p "\\?" url) "&" "?")
(car query-string)
"="
(cdr query-string))))
url)
(defun impostman--build-variables (values)
"Return alist with variables using values from Postman environment.
VALUES is the \"values\" read from environment (vector)."
(let (variables)
(dotimes (i (length (or values [])))
(let* ((item (elt values i))
(key (gethash "key" item ""))
(value (gethash "value" item ""))
(enabled (equal t (gethash "enabled" item t))))
(when enabled
(push (cons key value) variables))))
(nreverse variables)))
(defun impostman--parse-item (items level variables output-alist)
"Parse a Postman collection item.
ITEMS is the \"item\" read from collection (vector).
LEVEL is the level.
VARIABLES is a alist with Postman environment variables.
OUTPUT-ALIST is an alist with the output callbacks."
(dotimes (i (length items))
(let* ((item (elt items i))
(name (gethash "name" item ""))
(description (gethash "description" item ""))
(subitem (gethash "item" item))
(request (gethash "request" item)))
(insert (funcall (alist-get 'item output-alist)
level name description variables))
(if subitem
(impostman--parse-item subitem (1+ level) variables output-alist)
(when request
(let* ((description (gethash "description" request ""))
(auth (gethash "auth" request (make-hash-table)))
(method (gethash "method" request ""))
(header (gethash "header" request []))
(body (gethash
"raw"
(gethash "body" request (make-hash-table)) ""))
(url (gethash
"raw"
(gethash "url" request (make-hash-table)) ""))
(auth-headers (impostman--build-auth-headers auth))
(other-headers (impostman--build-headers header))
(headers (append auth-headers other-headers))
(replace-vars (alist-get 'replace-vars output-alist)))
(setq url (impostman--add-query-string-items-to-url
url
(impostman--build-auth-query-string auth)))
(dolist (header headers)
(setf
(car header) (funcall replace-vars (car header) variables)
(cdr header) (funcall replace-vars (cdr header) variables)))
(let ((method2 (funcall replace-vars method variables))
(url2 (funcall replace-vars url variables))
(body2 (funcall replace-vars body variables)))
(insert (funcall
(alist-get 'request output-alist)
description method2 url2 headers body2 variables)))))))))
(defun impostman--parse-json (collection environment output-alist)
: "Parse a Postman collection using an optional Postman environment.
COLLECTION is a hash table with the Postman collection (parsed JSON).
ENVIRONMENT is a hash table with the Postman environment (parsed JSON).
OUTPUT-ALIST is an alist with the output callbacks."
(let* ((name (gethash "name"
(gethash "info" collection (make-hash-table))
"unknown"))
(description (gethash "description"
(gethash "info"
collection (make-hash-table)) ""))
(item (gethash "item" collection []))
(values (gethash "values" environment []))
(variables (impostman--build-variables values)))
(pop-to-buffer (generate-new-buffer (concat name ".org")))
(funcall (alist-get 'init output-alist) variables)
(insert (funcall (alist-get 'header output-alist)
name description variables))
(impostman--parse-item item 2 variables output-alist)
(insert (funcall (alist-get 'footer output-alist) name variables))
(goto-char (point-min))
(funcall (alist-get 'end output-alist) variables)
values))
;;;###autoload
(defun impostman-parse-file (collection environment output-alist)
"Parse a file with a Postman collection, using an optional file with
a Postman environment (for variables).
COLLECTION is a Postman collection filename.
ENVIRONMENT is a Postman environment filename (optional).
OUTPUT-ALIST is an alist with the output callbacks."
(let (json_col (json_env (make-hash-table :test 'equal)))
(with-temp-buffer
(insert-file-contents collection)
(setq json_col (json-parse-buffer)))
(unless (string-empty-p (or environment ""))
(with-temp-buffer
(insert-file-contents environment)
(setq json_env (json-parse-buffer))))
(impostman--parse-json json_col json_env output-alist)))
;;;###autoload
(defun impostman-parse-string (collection environment output-alist)
"Parse a string with a Postman collection, using an optional string with
a Postman environment (for variables).
COLLECTION is a string with a Postman collection.
ENVIRONMENT is a string with a Postman environment (optional).
OUTPUT-ALIST is an alist with the output callbacks."
(let ((json_col (json-parse-string collection))
(json_env (if (string-empty-p (or environment ""))
(make-hash-table :test 'equal)
(json-parse-string environment))))
(impostman--parse-json json_col json_env output-alist)))
;; Postman collection import
(defun impostman-read-collection-filename ()
"Read Postman collection filename."
(interactive)
(read-file-name "Postman collection file (JSON): "))
(defun impostman-read-environment-filename ()
"Read Postman environment filename."
(interactive)
(let (insert-default-directory)
(read-file-name "Postman environment file (JSON, optional): ")))
(defun impostman-read-output ()
"Read Postman output type, which must be a key from alist
`impostman-outputs-alist'.
If the alist size is 1, the value is immediately returned without asking
anything."
(interactive)
(let* ((default (caar impostman-outputs-alist))
(prompt (concat "Output format (default is " default "): ")))
(if (= (length impostman-outputs-alist) 1)
default
(completing-read
prompt impostman-outputs-alist nil t nil nil default))))
(defun impostman--get-output-alist (name)
"Get output alist with a given NAME. A key with this name must exist in
`impostman-outputs-alist'."
(let ((output-alist (assoc name impostman-outputs-alist)))
(if output-alist
(symbol-value (cdr output-alist))
(error (format "Output \"%s\" is not supported" name)))))
;;;###autoload
(defun impostman-import-file (&optional collection environment output-name)
"Import a file with a Postman collection, using optional file with
a Postman environment (for variables).
COLLECTION is a Postman collection filename.
ENVIRONMENT is a Postman environment filename (optional).
OUTPUT-NAME is a string with the desired output (eg: \"verb\")."
(interactive)
(let* ((collection (or collection (impostman-read-collection-filename)))
(environment (or environment (impostman-read-environment-filename)))
(output-name (or output-name (impostman-read-output)))
(output-alist (impostman--get-output-alist output-name)))
(impostman-parse-file collection environment output-alist)))
;;;###autoload
(defun impostman-import-string (collection environment &optional output-name)
"Import a string with a Postman collection, using optional file with
a Postman environment (for variables).
COLLECTION is a string with a Postman collection.
ENVIRONMENT is a string with a Postman environment (optional).
OUTPUT-NAME is a string with the desired output (eg: \"verb\")."
(interactive)
(let* ((output-name (or output-name (impostman-read-output)))
(output-alist (impostman--get-output-alist output-name)))
(impostman-parse-string collection environment output-alist)))
;; version
(defun impostman-version (&optional print-dest)
"Return the Impostman version.
PRINT-DEST is the output stream, by default the echo area.
With \\[universal-argument] prefix, output is in the current buffer."
(interactive (list (if current-prefix-arg (current-buffer) t)))
(when (called-interactively-p 'any)
(princ (format "impostman %s" impostman-version) print-dest))
impostman-version)
(provide 'impostman)
;;; impostman.el ends here