Skip to content

Commit

Permalink
Initial support for API Key via #23.
Browse files Browse the repository at this point in the history
  • Loading branch information
eightysteele committed Jan 27, 2013
1 parent edacf51 commit eb2c66e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cheshire "4.0.0"]
[org.clojure/clojure-contrib "1.2.0"]
[org.clojure/data.json "0.1.2"]
[cartodb-java-client "1.0.0"]]
[eightysteele/cartodb-java-client "1.0.0"]]
:source-paths ["src/clj"]
:profiles {:dev
{:resource-paths
Expand Down
18 changes: 14 additions & 4 deletions src/clj/cartodb/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[cartodb.utils])
(:require [clj-http.client :as client]
[cheshire.core :as json])
(:import [com.cartodb.impl SecuredCartoDBClient]))
(:import [com.cartodb.impl SecuredCartoDBClient ApiKeyCartoDBClient]
[com.cartodb CartoDBClientIF]))

(defn- oauth-execute
"Execute an SQL command with supplied OAuth credentials; returns a
Expand All @@ -17,6 +18,14 @@
(if (true? return)
body)))

(defn- apikey-execute
"Execute supplied SQL command using an API Key and return response body."
[sql account api-key & {:keys [return] :or {return true}}]
(let [client (ApiKeyCartoDBClient. account api-key)
body (.executeQuery client sql)]
(when return
body)))

(defn- execute
"Query CartoDB with supplied SQL and return response body."
[sql account api-key format host api-version
Expand All @@ -33,9 +42,10 @@
[sql account & {:keys [api-key format host oauth api-version return]
:or {api-key nil format "json" host "cartodb.com"
oauth nil api-version "v2" return true}}]
(let [body (if oauth
(oauth-execute sql account oauth :return return)
(execute sql account api-key format host api-version :return return))]
(let [body (cond
(not= oauth nil) (oauth-execute sql account oauth :return return)
(not= api-key nil) (apikey-execute sql account api-key)
:else (execute sql account api-key format host api-version :return return))]
(try
(if (and
(true? return)
Expand Down

0 comments on commit eb2c66e

Please sign in to comment.