Repository: wilkerlucio/pathom
Pull request #57 (2018-10-30 01:26:25)

implement clj-http driver


Patch:
diff --git a/src/com/wsscode/pathom/diplomat/http.cljc b/src/com/wsscode/pathom/diplomat/http.cljc
index 398dd3f0..984ce7ce 100644
--- a/src/com/wsscode/pathom/diplomat/http.cljc
+++ b/src/com/wsscode/pathom/diplomat/http.cljc
@@ -19,7 +19,9 @@
                                ::content-type
                                ::accept
                                ::as
-                               ::debug?]))
+                               ::form-params
+                               ::debug?
+                               ::body]))
 
 (s/def ::response (s/keys :req [::headers]
                           :opt [::body]))
@@ -37,3 +39,15 @@
 (s/fdef request
   :args (s/cat :req ::request)
   :ret ::response)
+
+(defn request-method [{::keys [method form-params]}]
+  (or (some-> method name)
+      (if form-params "post" "get")))
+
+(defn encode-type->header [encode-type]
+  (cond
+    (string? encode-type)
+    encode-type
+
+    (keyword? encode-type)
+    (str "application/" (name encode-type))))
diff --git a/src/com/wsscode/pathom/diplomat/http/clj_http.clj b/src/com/wsscode/pathom/diplomat/http/clj_http.clj
index c50d945e..483ff2f5 100644
--- a/src/com/wsscode/pathom/diplomat/http/clj_http.clj
+++ b/src/com/wsscode/pathom/diplomat/http/clj_http.clj
@@ -3,11 +3,23 @@
             [com.wsscode.pathom.diplomat.http :as http]
             [clojure.spec.alpha :as s]))
 
-; TODO not working at this point, will be implemented later
+;; TODO: ::http/form-params, ::http/debug?
+(defn build-request-map
+  [{::http/keys [url content-type accept as body headers] :as req}]
+  (let [q? (partial contains? req)]
+    (cond-> {:url    url
+             :method (http/request-method req)}
+            (q? ::http/headers) (assoc :headers headers)
+            (q? ::http/content-type) (assoc :content-type (http/encode-type->header content-type))
+            (q? ::http/accept) (assoc :accept (http/encode-type->header accept))
+            (q? ::http/as) (assoc :as (http/encode-type->header as))
+            (q? ::http/body) (assoc :body body))))
 
 (defn request [req]
   (s/assert ::http/request req)
-  (client/request req))
+  (-> req
+      build-request-map
+      client/request))
 
 (s/fdef request
   :args (s/cat :request ::http/request)
diff --git a/src/com/wsscode/pathom/diplomat/http/fetch.cljs b/src/com/wsscode/pathom/diplomat/http/fetch.cljs
index 6d36e005..1c80cb2c 100644
--- a/src/com/wsscode/pathom/diplomat/http/fetch.cljs
+++ b/src/com/wsscode/pathom/diplomat/http/fetch.cljs
@@ -4,25 +4,10 @@
             [goog.object :as gobj]
             [cljs.spec.alpha :as s]))
 
-(defn request-method [{::http/keys [method form-params]}]
-  (or (some-> method name)
-      (if form-params "post" "get")))
-
-(defn content-type-header [content-type]
-  (cond
-    (string? content-type)
-    content-type
-
-    (keyword? content-type)
-    (case content-type
-      ::http/json "application/json"
-      ::http/edn "application/edn"
-      ::http/transit+json "application/transit+json")))
-
 (defn build-headers [{::http/keys [headers content-type]}]
   (let [base-headers
         (cond-> nil
-          content-type (assoc :content-type (content-type-header content-type)))]
+          content-type (assoc :content-type (http/encode-type->header content-type)))]
     (merge base-headers headers)))
 
 (defn build-body [{::http/keys [form-params content-type]}]
@@ -37,7 +22,7 @@
 (defn build-request-map [{::http/keys [form-params] :as req}]
   (let [req     (normalize-as req)
         headers (build-headers req)]
-    (cond-> {:method (request-method req)}
+    (cond-> {:method (http/request-method req)}
       headers (assoc :headers headers)
       form-params (assoc :body (build-body req)))))
 


Test patch:
diff --git a/test/com/wsscode/pathom/diplomat/http/clj_http_test.clj b/test/com/wsscode/pathom/diplomat/http/clj_http_test.clj
new file mode 100644
index 00000000..46ae22fe
--- /dev/null
+++ b/test/com/wsscode/pathom/diplomat/http/clj_http_test.clj
@@ -0,0 +1,21 @@
+(ns com.wsscode.pathom.diplomat.http.clj-http-test
+  (:require [clojure.test :refer [deftest is are]]
+            [com.wsscode.pathom.diplomat.http :as p.http]
+            [com.wsscode.pathom.diplomat.http.clj-http :as clj-http]))
+
+(deftest test-build-request-map
+  (are [req out] (= (clj-http/build-request-map req) out)
+                 {::p.http/url "/foo"}
+                 {:url    "/foo"
+                  :method "get"}
+
+                 {::p.http/url    "/foo"
+                  ::p.http/method ::p.http/post}
+                 {:url    "/foo"
+                  :method "post"}
+
+                 {::p.http/url          "/foo"
+                  ::p.http/content-type ::p.http/json}
+                 {:url          "/foo"
+                  :method       "get"
+                  :content-type "application/json"}))