-
Notifications
You must be signed in to change notification settings - Fork 25
/
conversion.clj
82 lines (65 loc) · 2.77 KB
/
conversion.clj
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(ns hato.conversion
(:require [clojure.edn :as edn])
(:import (java.io InputStream)))
;;; protocols
(defprotocol Decoder
(-decode [decoder response]))
;;; json
(def json-enabled?
(try
(require 'cheshire.core)
true
(catch Throwable _ false)))
;;; transit
(def transit-enabled?
(try
(require 'cognitect.transit)
true
(catch Throwable _ false)))
;;; Decoding
(defmulti decode
"Extensible content-type based decoder."
:content-type)
(defmethod decode :default
[{:keys [content-type] :as resp}]
; Throw for types that we would support if dependencies existed.
(when (#{:application/json :application/transit+json :application/transit+msgpack} content-type)
(throw (IllegalArgumentException.
(format "Unable to decode content-type %s. Add optional dependencies or provide alternative decoder."
(:content-type resp)))))
; Return strings for text, or the original result otherwise.
(if (= "text" (namespace content-type))
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")]
(slurp (:body resp) :encoding charset))
(:body resp)))
(defmethod decode :application/edn
[resp]
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")]
(edn/read-string (slurp (:body resp) :encoding charset))))
(when json-enabled?
(let [decode-fn (ns-resolve 'cheshire.core 'decode-strict)]
(defmethod decode :application/json
[resp]
(let [^String charset (or (-> resp :content-type-params :charset) "UTF-8")]
(decode-fn (slurp (:body resp) :encoding charset) true)))))
(when transit-enabled?
(let [reader (ns-resolve 'cognitect.transit 'reader)
read (ns-resolve 'cognitect.transit 'read)
parse-transit (fn [type resp]
(with-open [^InputStream bs (:body resp)]
(when (pos? (.available bs))
(read (reader bs type)))))]
(defmethod decode :application/transit+json [resp]
(parse-transit :json resp))
(defmethod decode :application/transit+msgpack [resp]
(parse-transit :msgpack resp))))
(defrecord DefaultDecoder []
Decoder
(-decode [_ response] (decode response)))
(comment
(def d (DefaultDecoder.))
(-decode d {:content-type :text/plain :body (clojure.java.io/input-stream (.getBytes "hello world"))})
(-decode d {:content-type :application/foo :body (clojure.java.io/input-stream (.getBytes "{:yo 3}"))})
(-decode d {:content-type :application/edn :body (clojure.java.io/input-stream (.getBytes "{:yo 3}"))})
(-decode d {:content-type :application/json :body (clojure.java.io/input-stream (.getBytes "{\"hello\": 2}"))})
(-decode d {:content-type :application/transit+json :body (clojure.java.io/input-stream (.getBytes "[\"^ \",\"~:a\",[1,2]]"))}))