Skip to content

Commit

Permalink
Split out headers to reduce boilerplate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan McGreggor committed Dec 9, 2013
1 parent 5481c90 commit da92fcb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/lferax-http.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
(export all))


(defun get-default-headers (content-type)
(list (tuple (: lferax-const content-type)
content-type)
(tuple (: lferax-const user-agent)
(: lferax-const user-agent-string))))

(defun get-auth-headers (content-type auth-token)
(: lists merge (get-default-headers content-type)
(list (tuple (: lferax-const x-auth-token)
auth-token))))

; XXX all of these need to be refactored to reduce boilerplate
(defun post (url payload)
(: lferax-util start-services)
(let* ((method 'post)
(content-type '"application/json")
(headers (list (tuple (: lferax-const content-type)
content-type)
(tuple (: lferax-const user-agent)
(: lferax-const user-agent-string))))
(headers (get-default-headers content-type))
(request-data (tuple url headers content-type payload))
(http-options ())
(options ()))
Expand All @@ -20,12 +28,7 @@
(: lferax-util start-services)
(let* ((method 'post)
(content-type '"application/json")
(headers (list (tuple (: lferax-const content-type)
content-type)
(tuple (: lferax-const x-auth-token)
auth-token)
(tuple (: lferax-const user-agent)
(: lferax-const user-agent-string))))
(headers (get-auth-headers content-type auth-token))
(request-data (tuple url headers content-type payload))
(http-options ())
(options ()))
Expand All @@ -36,12 +39,7 @@
(let* ((method 'get)
(content-type '"application/json")
(payload '"")
(headers (list (tuple (: lferax-const content-type)
content-type)
(tuple (: lferax-const x-auth-token)
auth-token)
(tuple (: lferax-const user-agent)
(: lferax-const user-agent-string))))
(headers (get-auth-headers content-type auth-token))
(request-data (tuple url headers))
(http-options ())
(options ()))
Expand Down
14 changes: 14 additions & 0 deletions test/lferax-http_tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
(assert-exit 2))))


(defun get-default-headers_test()
(let* ((result (: lferax-http get-default-headers '"my-content-type"))
(data-check (list_to_binary (element 2 (car result))))
(count (length result)))
(assert-equal '"my-content-type" `(binary_to_list ,data-check))
(assert-equal 2 count)))

(defun get-auth-headers_test()
(let* ((result (: lferax-http get-auth-headers '"my-content-type" '"abc-tk"))
(data-check (list_to_binary (element 2 (: lists last result))))
(count (length result)))
(assert-equal '"abc-tk" `(binary_to_list ,data-check))
(assert-equal 3 count)))

(defun get_test ()
(: inets start)
(: meck new 'httpc)
Expand Down

0 comments on commit da92fcb

Please sign in to comment.