                      (get-instance-props instance-id auth-token)
                      (catch clojure.lang.ExceptionInfo e)))
                  instances))))
+
+(defn get-alias-map [instances-map]
+  (reduce (fn [m [instance-id props]]
+            (let [url (get props "alias")
+                  alias (first (str/split url #"\."))]
+              (assoc m alias instance-id)))
+          {}
+          instances-map))
+
+
+
+(defn fetch-p12
+  "Fetch the p12 file from github. Save it and return the File.
+  Returns nil if the file can't be found"
+  [instance-id auth-token]
+  (try
+    (let [bytes (-> (http/get (format
+                               "https://api.github.com/repos/akvo/akvo-flow-server-config/contents/%1$s/%1$s.p12"
+                               instance-id)
+                              {:headers {"Authorization" (str "token " auth-token)
+                                         "Content-Type" "application/json"}
+                               :as :json})
+                    :body
+                    :content
+                    Base64/decodeBase64)
+          file (io/file p12-dir (str instance-id ".p12"))]
+      (io/copy bytes file)
+      file)
+    (catch clojure.lang.ExceptionInfo e)))
+
+(defn get-p12 [instance-id auth-token]
+  (let [file (io/file p12-dir (str instance-id ".p12"))]
+    (if (.exists file)
+      file
+      (fetch-p12 instance-id auth-token))))

                      :content))))
        ae-reader (AppEngineWebXmlReader. (System/getProperty "java.io.tmpdir")
                                          (.getName temp-file))]
    (.getSystemProperties (.readAppEngineWebXml ae-reader))))

(defn get-instance-map [auth-token]
  (let [instances (get-instances auth-token)]

           [com.google.apphosting.utils.config AppEngineWebXmlReader]
           [java.io ByteArrayInputStream])
  (:require [clojure.string :as str]
            [clojure.java.io :as io]
            [clj-http.client :as http]))

(def p12-dir (System/getProperty "java.io.tmpdir"))

#_(let [auth-token ""
        email ""
        alias-map (get-alias-map instance-map)
        instance-id (get alias-map "uat1")
        host (str instance-id ".appspot.com")
        port 443
        iam-account (get-in instance-map [instance-id "serviceAccountId"])
        p12-file (get-p12 instance-id auth-token)]
    (get-filtered-surveys host iam-account (.getAbsolutePath p12-file) email))

(defn get-filtered-surveys
  [host iam-account p12-path email]
