Created
November 14, 2012 23:19
-
-
Save duelinmarkers/4075541 to your computer and use it in GitHub Desktop.
Asynchronous HTTP in Clojure with Aleph
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
(defn get-fb-access-token [code redirect-uri] | |
(run-pipeline (access-token-url code redirect-uri) | |
{:error-handler (fn [_])} | |
#(http-request {:url %}) | |
read-access-token-from-response)) | |
(defn get-fb-user-with-token [access-token] | |
(run-pipeline access-token | |
{:error-handler (fn [_])} | |
fb-user-url | |
#(http-request {:url %}) | |
read-user-from-response | |
#(combine-results % access-token))) | |
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(run-pipeline (get-fb-access-token code redirect-uri) | |
{:error-handler error-callback} | |
get-fb-user-with-token | |
success-callback)) |
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
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(let [access-token-result (run-pipeline (access-token-url code redirect-uri) | |
{:error-handler (fn [_])} | |
#(http-request {:url %}) | |
read-access-token-from-response)] | |
(run-pipeline access-token-result | |
{:error-handler error-callback} | |
fb-user-url | |
#(http-request {:url %}) | |
read-user-from-response | |
#(combine-results % @access-token-result) | |
#(do | |
(success-callback %) | |
(lamina.connections/close-connection client))))) |
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
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(run-pipeline (access-token-url code redirect-uri) | |
{:error-handler error-callback} | |
#(http-request {:url %}) | |
read-access-token-from-response | |
(fn [access-token] | |
(run-pipeline access-token | |
{:error-handler (fn [_])} | |
fb-user-url | |
#(http-request {:url %}) | |
read-user-from-response | |
#(combine-results % access-token) | |
success-callback)))) |
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
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(run-pipeline (access-token-url code auth-redirect-uri) | |
{:error-handler error-callback} | |
#(http-request {:url %}) | |
read-access-token-from-response | |
fb-user-url | |
#(http-request {:url %}) | |
read-user-from-response | |
success-callback)) |
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
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(let [client (http-client {:url "https://graph.facebook.com"})] | |
(run-pipeline (get-fb-access-token client code redirect-uri) | |
{:error-handler (fn [throwable] | |
(try (error-callback throwable) | |
(finally (lamina.connections/close-connection client))))} | |
#(get-fb-user-with-token client %) | |
#(do | |
(try (success-callback %) | |
(finally (lamina.connections/close-connection client))))))) |
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
(defn fb-user-details-from-code [code redirect-uri] | |
(get-fb-user-with-token | |
(get-fb-access-token code redirect-uri))) |
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
(defn fb-user-details-from-code [code redirect-uri success-callback error-callback] | |
(on-realized | |
(http-request {:url (access-token-url code auth-redirect-uri)}) | |
#(on-realized | |
(http-request {:url (fb-user-url (read-access-token-from-response %))}) | |
#(success-callback (read-user-from-response %)) | |
error-callback) | |
error-callback)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are in support of this blog post: http://elhumidor.blogspot.com/2012/11/using-alephs-asynchronous-http-client.html