tfoldi

Tableau Server Rest API from Clojure - Example

Mar 20th, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns tabsync.tableau
  2.   (:require [clj-http.client :as client]
  3.             [clojure.zip :refer [xml-zip]]
  4.             [clojure.data.zip.xml :refer [xml-> xml1-> attr ]]
  5.             [clojure.data.xml :as xml])
  6.   )
  7.  
  8. (defn tableau-url-for
  9.   "Constructs server API url. Target can be hostname or session returned from logon-to-server"
  10.   [target api-path]
  11.   (if (= (type target) java.lang.String)
  12.     ; connect to host
  13.     (str target "/api/2.0/" api-path )
  14.     ; connect to already established session (target is a session)
  15.     (str (get target :host) "/api/2.0/sites/" (get target :siteid) "/" api-path)
  16.     )
  17.   )
  18.  
  19. (defn get-zip
  20.   "Get xml-zip from http.client response"
  21.   [http-response]
  22.   (-> (get http-response :body)
  23.       xml/parse-str
  24.       xml-zip
  25.       )
  26.   )
  27.  
  28. (defn http
  29.   "Perform a http call to tableau server. Host can be hostname or session"
  30.   [method host api-path http-params]
  31.   (get-zip
  32.     (
  33.       (resolve (symbol (str "client/" method)))
  34.       (tableau-url-for host api-path)
  35.       (merge http-params {:headers {"X-Tableau-Auth" (get host :token)}})
  36.       )
  37.     )
  38.   )
  39.  
  40. (defn logindata
  41.   "Creates XML request for logon call"
  42.   [name password]
  43.   (xml/emit-str
  44.     (xml/element :tsRequest {}
  45.                  (xml/element :credentials {:name     name
  46.                                             :password password}
  47.                               (xml/element :site {:contentUrl ""})))) ; "" means Default site
  48.   )
  49.  
  50.  
  51. (defn logon-to-server
  52.   "Logon to tableau server by invoking /auth/signin, returns map with token,
  53.  site id and hostname"
  54.   [host name password]
  55.   (let [ts-response (http "post" host "/auth/signin" {:multipart [{:name    "title"
  56.                                                                    :content (logindata name password)}]})
  57.         ]
  58.     {:token  (xml1-> ts-response
  59.                      :credentials (attr :token)
  60.                      )
  61.      :siteid (xml1-> ts-response
  62.                      :credentials
  63.                      :site (attr :id))
  64.      :host host
  65.      }
  66.     )
  67.   )
  68.  
  69.  
  70. (defn get-users
  71.   "Iterates on site users defined by session. Page size is 10"
  72.   [session]
  73.   (loop [page-number 1]
  74.     (let [ts-response (http "get" session "/users/"
  75.                             {:query-params {:pageSize   10
  76.                              :pageNumber page-number}})]
  77.       (println (str "On page " page-number " -> " ts-response))
  78.       (if (> (* 10 page-number) (read-string (xml1-> ts-response
  79.                                         :pagination
  80.                                         (attr :totalAvailable)
  81.                                         )))
  82.              (println "All users downloaded")
  83.              (recur (inc page-number ))
  84.              )
  85.       )
  86.     )
  87.   )
  88.  
  89. (defn signout
  90.   "Logoff from server"
  91.   [session]
  92.   (http "get" session "/auth/signout" {})
  93.   )
  94.  
  95. ; Well, logon to server, get users and log off
  96. (let [session (logon-to-server "http://server" "user" "pass")]
  97.   (get-users session )
  98.   (signout session)
  99.   )
Advertisement
Add Comment
Please, Sign In to add comment