Skip to content

Instantly share code, notes, and snippets.

@swlkr
Created February 25, 2017 06:07
Show Gist options
  • Save swlkr/c65f25316246d60a6f4c925465eb57a9 to your computer and use it in GitHub Desktop.
Save swlkr/c65f25316246d60a6f4c925465eb57a9 to your computer and use it in GitHub Desktop.
(ns monolith.stripe.http
(:require [monolith.utils :as utils]
[cheshire.core :as json])
(:refer-clojure :exclude [get]))
(defn make-url [& args]
(clojure.string/replace (apply str args) #"(\w+)\/+" "$1/"))
(defn req-body [m]
(condp (utils/flip contains?) m
:body (json/generate-string (:body m))
:form-params (clojure.walk/stringify-keys (:form-params m))))
(defn req
([method url env]
(let [{:keys [stripe-url stripe-token]} env]
{:url (make-url stripe-url url)
:headers {"Authorization" (str "Bearer " stripe-token)}
:method method}))
([method url params env]
(let [k (-> params keys first)
v (req-body params)]
(-> (req method url env)
(assoc k v)))))
(def get (partial req :get))
(def post (partial req :post))
(defn res [{:keys [error status body]}]
(let [parsed-body (json/parse-string body true)
msg (-> parsed-body :error :message)
err (or msg error)]
(if (= status 200)
parsed-body
(utils/throw+ err))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment