Created
August 12, 2011 22:42
-
-
Save dirtyvagabond/1143156 to your computer and use it in GitHub Desktop.
An OK Clojure simple API example
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
; | |
; Required dependencies: | |
; [org.clojure/clojure-contrib "1.2.0"] | |
; [com.google.api.client/google-api-client "1.4.1-beta"] | |
; [com.google.guava/guava "r09"] | |
; | |
(ns funnyplaces.core | |
(:import (com.google.api.client.auth.oauth OAuthHmacSigner OAuthParameters)) | |
(:import (com.google.api.client.http GenericUrl HttpRequest HttpRequestFactory HttpResponse HttpTransport)) | |
(:import (com.google.api.client.http.javanet NetHttpTransport)) | |
(:use [clojure.contrib.duck-streams :only [read-lines]])) | |
(defn make-params | |
"Returns configured OAuth params for the specified request" | |
[client-key client-secret method url] | |
(let [signer (OAuthHmacSigner.) | |
params (OAuthParameters.)] | |
(set! (. params consumerKey) client-key) | |
(doto params | |
(.computeNonce) | |
(.computeTimestamp)) | |
(set! (. signer clientSharedSecret) client-secret) | |
(set! (. params signer) signer) | |
(.computeSignature params method url) | |
params)) | |
(defn get-response | |
"Returns the response as a sequence of lines. You should consume the entire sequence." | |
[client-key client-secret url] | |
(let [req (.buildGetRequest | |
(.createRequestFactory | |
(NetHttpTransport.) | |
(make-params client-key client-secret "GET" url)) | |
url)] | |
(.getContent (.execute req)))) | |
(def *url* (GenericUrl. "http://api.v3.factual.com/t/places?limit=10")) | |
;;; Make the request and print out each line of the response | |
(doseq [line (read-lines (get-response "YOUR_FACTUAL_KEY" "YOUR_FACTUAL_SECRET" *url*))] | |
(println line)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment