Last active
December 16, 2015 17:59
-
-
Save lmarburger/5474390 to your computer and use it in GitHub Desktop.
Playing around with Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cljcloud.core | |
(:require [clj-http.client :as http] | |
[clojure.data.json :as json])) | |
(defn parse | |
"Parse a Collection+JSON response body into a map." | |
[resp] | |
(-> resp | |
:body | |
(json/read-str :key-fn keyword) | |
:collection)) | |
; Examples: | |
; [{:name "first-name" :value "Arthur"} {:name "last-name" :value "Dent"}] | |
; [{:rel "next" :href "/next"} {:rel "prev" :href "/prev"}] | |
(defn datum | |
"Retrieve a datum in the given data by name." | |
([data name] (datum data name :name)) | |
([data name key] | |
(first (filter #(= name (key %)) data)))) | |
(defn- auth-header | |
"Create an authorization header using the given token." | |
[token] | |
{"Authorization" (str "Token token=" token)}) | |
(defn- get-collection | |
([url email pass] (parse (http/get url {:basic-auth [email pass]}))) | |
([url token] (parse (http/get url {:headers (auth-header token)})))) | |
(defn- follow-link | |
[coll link token] | |
(let [href (:href (datum (:links coll) "drops" :rel))] | |
(get-collection href token))) | |
(defn- authorize | |
[email pass] | |
(get-collection "http://api.getcloudapp.dev/authorization" email pass)) | |
(defn- root | |
[token] | |
(get-collection "http://api.getcloudapp.dev/" token)) | |
(defn token | |
"Retrieve the authorization token for an account." | |
[email pass] | |
(-> (authorize email pass) | |
(get-in [:items 0 :data]) | |
(datum "token") | |
:value)) | |
(defn drops | |
"Retrieve the latest drops for an account." | |
[token] | |
(-> (root token) | |
(follow-link "drops" token))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cljcloud.core-test | |
(:require [clojure.test :refer :all] | |
[cljcloud.core :as client] | |
[clojure.data.json :as json] | |
[clj-http.client :as http])) | |
(deftest parse-test | |
(testing "basic collection+json response" | |
(let [coll {:collection {:version "1.0" :href "http://example.org"}} | |
resp {:body (json/write-str coll)}] | |
(is (= {:version "1.0" :href "http://example.org"} | |
(client/parse resp)))))) | |
(deftest datum-test | |
(testing "retrieves datum by name by default" | |
(let [first-name {:name "first-name" :value "Arthur" } | |
last-name {:name "last-name" :value "Dent" } | |
data [first-name last-name]] | |
(is (= first-name (client/datum data "first-name"))))) | |
(testing "retrieves datum by given key" | |
(let [next {:rel "next" :href "/next" } | |
prev {:rel "prev" :href "/prev" } | |
data [next prev]] | |
(is (= next (client/datum data "next" :rel)))))) | |
(deftest token-test | |
(testing "fetch token for account" | |
(let [coll {:collection {:items [{:data [{:name "token" | |
:value "abc123"}]}]}} | |
resp {:body (json/write-str coll)}] | |
(with-redefs [http/get (constantly resp)] ; This seems bad. | |
(is (= "abc123" (client/token "[email protected]" "towel"))))))) | |
(deftest drops-test | |
(testing "i have no idea how to test this")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment