Created
February 14, 2016 22:45
-
-
Save munk/a333e4d64e3de2d146d2 to your computer and use it in GitHub Desktop.
Understanding Protocols
This file contains 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
; HTTP Client for OpenHDS-REST | |
(ns ohds.client | |
(:require [clojure.edn :as edn] | |
[clojure.tools.logging :as log] | |
[org.httpkit.client :as http] | |
[clojure.data.json :as json] | |
[schema.core :as s]) | |
(:import (java.time LocalDateTime))) | |
(defmacro def- [item value] | |
`(def ^{:private true} ~item ~value)) | |
;;; Get configuration data | |
(def- config (edn/read-string (slurp "application.edn"))) | |
(def- apihost (:apihost config)) | |
(def- apiuser (:apiuser config)) | |
(def- apipass (:apipass config)) | |
(def- auth {:basic-auth [apiuser apipass]}) | |
(def- urls | |
{:in-migrations "/inMigrations" | |
:individuals "/individuals" | |
:fieldworkers "/fieldworkers" | |
:deaths "/deaths" | |
:errorLogs "/errorLogs" | |
:memberships "/memberships" | |
:pregnancy-results "/pregnancyResults" | |
:social-groups "/socialGroups" | |
:events "/events" | |
:relationships "/relationships" | |
:location-hierarchies "/locationHierarchies" | |
:out-migrations "/outMigrations" | |
:pregnancy-outcomes "/pregnancyOutcomes" | |
:location-hierarchy-levels "/locationHierarchyLevels" | |
:locations "/locations" | |
:residencies "/resdiencies" | |
:visits "/visits" | |
:users "/users" | |
:pregnancy-observations "/pregnancyObservations" | |
:project-codes "/projectCodes"}) | |
(defn- now [] | |
(str (LocalDateTime/now) "Z")) | |
(defn- bulk [url] | |
(str url "/bulk.json")) | |
(defn- json->clj [data] | |
(json/read-str data :key-fn keyword)) | |
(defn- get-entity [url] | |
(-> (str apihost url) (http/get auth) deref :body json->clj)) | |
(defn- create-entity [url entity] | |
(let [body (json/write-str entity) | |
params (assoc auth :body body :headers {"Content-Type" "application/json"})] | |
(-> (str apihost url) (http/post params) deref :body json->clj))) | |
(defprotocol Create | |
(create [entity] "Create an Entity with OpenHDS-Rest")) | |
(defmacro defendpoint [msg-type url-key schema] | |
`(defrecord ~msg-type [] | |
Create | |
(create [entity#] | |
(s/validate ~schema entity#) | |
(println "validated") | |
(create-entity (~url-key urls) entity#)))) | |
(defn- query [url-key & [{:keys [id bulk?]}]] | |
(let [base-url (url-key urls) | |
url (cond | |
id (str base-url "/" id) | |
bulk? (str base-url "/bulk.json") | |
:else base-url)] | |
(log/debug "GET" url) | |
(get-entity url))) | |
(defn- uuid-length [] | |
(s/pred (fn [uuid] (or | |
(= uuid "UNKNOWN") | |
(= 36 (count uuid)))))) | |
(def- uuid-schema (s/both s/Str (uuid-length))) | |
(def- date-schema (s/both s/Str #(re-matches #"\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ" %))) | |
(defendpoint Location :locations | |
{:collectedByUuid uuid-schema | |
:locationHierarchyUuid uuid-schema | |
:location {:name s/Str | |
:extId s/Str | |
:type s/Str | |
:collectionDateTime s/Str}}) | |
(defendpoint LocationHierarchy :location-hierarchies | |
{:collectedByUuid uuid-schema | |
:parentUuid uuid-schema | |
:levelUuid uuid-schema | |
:locationHierarchy {:extId s/Str | |
:name s/Str | |
:collectionDateTime date-schema}}) | |
(comment | |
(create (map->Location | |
{:collectedByUuid "fa6bb290-533d-4a02-b9c1-141e93723cfc" | |
:locationHierarchyUuid "178b00cb-289e-4a24-8e90-13a5f8d076c9" | |
:location {:name "123 Mockingbird Ln." | |
:extId "123 Mockingbird Ln." | |
:type "RURAL" | |
:collectionDateTime (now)}})) | |
(create (map->LocationHierarchy | |
{:collectedByUuid "UNKNOWN" | |
:levelUuid "99c1c6fe-c9e5-4c76-9ee8-becc2ce0d933" | |
:parentUuid "UNKNOWN" | |
:locationHierarchy {:extId "a-test" | |
:name "a-test" | |
:collectionDateTime (now) | |
}})) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment