forked from angel-popov/cl-selenium-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp.lisp
68 lines (49 loc) · 1.73 KB
/
http.lisp
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
(in-package :webdriver-client)
;; uri
(defparameter *uri* "http://127.0.0.1:4444/")
(defparameter *prefix* "/wd/hub")
(defun make-uri (path)
(quri:make-uri :defaults *uri*
:path (format nil "~a~a" *prefix* path)))
;; json
(defmethod json:encode-json ((object (eql :false)) &optional (stream json:*json-output*))
(write-string "false" stream))
(defun encode (plist)
(cl-json:encode-json-plist-to-string plist))
(defun decode (json)
(cl-json:decode-json-from-string json))
;; http
(defmacro with-decode-and-handler (body)
`(handler-case (decode ,body)
(dex:http-request-failed (err)
(error 'protocol-error
:body (decode (dex:response-body err))
:status (dex:response-status err)))))
(defun check (response)
"Validates the status of Selenium Webdriver response."
(and (assoc :status response)
(zerop (assoc-value response :status))))
(defun value (response)
(assoc-value response :value))
(defun http-get (path)
(with-decode-and-handler
(dex:get (make-uri path))))
(defun http-get-value (path)
(value (http-get path)))
(defun http-get-check (path)
(check (http-get path)))
(defun http-post (path &rest params)
;; (format t "Sending ~a -> ~a~%" (encode params) path)
(with-decode-and-handler
(dex:post (make-uri path)
:content (encode params)
:headers '(("Content-Type" . "application/json;charset=UTF-8")))))
(defun http-post-value (path &rest params)
(value (apply #'http-post path params)))
(defun http-post-check (path &rest params)
(check (apply #'http-post path params)))
(defun http-delete (path)
(with-decode-and-handler
(dexador:delete (make-uri path))))
(defun http-delete-check (path)
(check (http-delete path)))