36 lines
1.4 KiB
Fennel
36 lines
1.4 KiB
Fennel
(local hr (require :http.request))
|
|
(local tablex (require :pl.tablex))
|
|
(local pretty (require :pl.pretty))
|
|
(local rj (require :rapidjson))
|
|
|
|
(var api-prefix "https://bag.fontkeming.fail")
|
|
|
|
(fn load-client-credentials [path]
|
|
((loadfile path)))
|
|
|
|
(fn get-token [token-endpoint credentials]
|
|
(let [req (hr.new_from_uri token-endpoint)
|
|
body (tablex.copy credentials)]
|
|
(set body.grant_type "password")
|
|
(req:set_body (rj.encode body))
|
|
(: (. req :headers) :upsert ":method" "POST")
|
|
(: (. req :headers) :append "content-type" "application/json")
|
|
(match (req:go)
|
|
(headers stream) (let [{ :access_token at }
|
|
(rj.decode (stream:get_body_as_string 2))]
|
|
at))))
|
|
|
|
(fn api-req [cred method uri body]
|
|
(let [req (hr.new_from_uri uri)]
|
|
(req:set_body (rj.encode body))
|
|
(: (. req :headers) :upsert ":method" method)
|
|
(: (. req :headers) :append "content-type" "application/json")
|
|
(: (. req :headers) :append "authorization" (.. "Bearer " cred))
|
|
(match (req:go)
|
|
{:_data [{:name ":status" :value "404"}]} (print 404)
|
|
{:_data [{:name ":status" :value "503"}]} (print 404)
|
|
(headers stream) (values headers (rj.decode (stream:get_body_as_string 2)))
|
|
_ (print "req failed"))))
|
|
|
|
{ :api-req api-req :get-token get-token :api-prefix api-prefix :load-client-credentials load-client-credentials }
|