Skip to content

Commit

Permalink
Add source/read-uri-json multimethod.
Browse files Browse the repository at this point in the history
Issue #44 - Add read-uri-json multimethod which dispatches on the URI
scheme. Add implementions for HTTP(S) and file schemes and raise an
error in the default implementation. Delegate to this method within
the URI implementation of JSONSource.
  • Loading branch information
lkitching committed May 25, 2020
1 parent b193ef9 commit 985930c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
29 changes: 23 additions & 6 deletions src/csv2rdf/source.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[csv2rdf.util :as util]
[clojure.spec.alpha :as s]
[csv2rdf.http :as http]
[clojure.data.json :as json])
[clojure.data.json :as json]
[clojure.string :as string])
(:import [java.net URI]
[java.io File InputStream ByteArrayInputStream]))

Expand All @@ -29,13 +30,29 @@
(with-open [r (io/reader source)]
(json/read r)))

(defn- http-get-json [uri]
(let [{:keys [body]} (http/get-uri uri)]
(get-json body)))

(defmulti read-uri-json
"Reads a JSON document from a URI"
(fn [^URI uri] (keyword (.getScheme uri))))

(defmethod read-uri-json :file [uri] (read-json uri))
(defmethod read-uri-json :http [uri] (http-get-json uri))
(defmethod read-uri-json :https [uri] (http-get-json uri))
(defmethod read-uri-json :default [uri]
(let [supported-schemes (keys (dissoc (methods read-uri-json) :default))]
(throw (ex-info
(format "Unable to read JSON from URI %s: unsupported scheme.%nSupported schemes: %s"
uri
(string/join ", " (map name supported-schemes)))
{:type ::unsupported-uri-scheme-error
:uri uri}))))

(extend-protocol JSONSource
URI
(get-json [uri]
(if (= (keyword (.getScheme uri)) :file)
(read-json uri)
(let [{:keys [body]} (http/get-uri uri)]
(get-json body))))
(get-json [uri] (read-uri-json uri))

File
(get-json [f] (read-json f))
Expand Down
16 changes: 13 additions & 3 deletions test/csv2rdf/source_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
[clojure.java.io :as io]
[clojure.test :refer :all]
[csv2rdf.http :as http]
[csv2rdf.source :refer :all]
[csv2rdf.source :refer :all :as source]
[csv2rdf.test-common :refer [->TestHttpClient]])
(:import java.net.URI))
(:import java.net.URI
[clojure.lang ExceptionInfo]))

(deftest get-json-test
(testing "URI"
Expand All @@ -17,8 +18,17 @@
(->TestHttpClient requests)
(let [result (get-json uri)]
(is (= json result))))))

(testing "file scheme"
(let [uri (.toURI (io/file "w3c-csvw/tests/test104.json"))
json {}
result (get-json uri)]
(is (= json result))))))
(is (= json result))))

(testing "unsupported scheme"
(let [uri (URI. "unsupported:example.com")]
(try
(get-json uri)
(is false "Expected exception to be thrown")
(catch ExceptionInfo ex
(is (= ::source/unsupported-uri-scheme-error (:type (ex-data ex))))))))))

0 comments on commit 985930c

Please sign in to comment.