Last active
May 12, 2020 05:00
-
-
Save jackcallister/8d4c48aba43435e12ae4191d408bcf32 to your computer and use it in GitHub Desktop.
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 icp-api.core | |
(:require [org.httpkit.server :as s] | |
[clojure.data.json :as json] | |
[clj-http.client :as client] | |
[inflections.core :as i])) | |
(def api-key "XXXXXXXXXXXXXXXXXXXXX") | |
(def uri "https://emi.azure-api.net/ICPConnectionData/single") | |
(defn- get-icp | |
(i/hyphenate-keys | |
(first (json/read-str (:body (client/get uri {:timeout 200 | |
:query-params { :id "0007153356RN6A1" } | |
:headers {:Ocp-Apim-Subscription-Key api-key}})))))) | |
(defn icp [id] | |
(let [body (get-icp)] | |
(conj {} | |
{:id (get body "icp-identifier")} | |
{:trader-code (get-in body ["trader" "trader"])} | |
{:smart-meter-enabled (let [installation (first (get-in body ["metering" "installation-information"])) | |
components (get installation "component-information") | |
ami-components (filter (fn [c] (get c "ami-flag")) components) | |
hhr-components (filter (fn [ac] (= "HHR" (get ac "meter-type"))) ami-components)] | |
(any? hhr-components))}))) | |
;; (icp '0007153356RN6A1') => {:smart-meter-enabled true, :trader "MERX", :id "0007153356RN6A1"} | |
;; Server | |
(defn handler [req] | |
{:status 200 | |
:headers {"Content-Type" "text/json"} | |
:body (json/write-str (clean-icp "0007153356RN6A1")) }) ;; Use the actual req | |
(defonce server (atom nil)) | |
(defn stop-server [] | |
(when-not (nil? @server) | |
(@server :timeout 100) | |
(reset! server nil))) | |
(defn -main [& args] | |
(println "Running server on port 9191") | |
(reset! server (s/run-server handler {:port 9191}))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment