Skip to content

Instantly share code, notes, and snippets.

@duelinmarkers
Created November 14, 2012 23:19
Show Gist options
  • Save duelinmarkers/4075541 to your computer and use it in GitHub Desktop.
Save duelinmarkers/4075541 to your computer and use it in GitHub Desktop.
Asynchronous HTTP in Clojure with Aleph
(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))
(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)))))
(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))))
(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))
(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)))))))
(defn fb-user-details-from-code [code redirect-uri]
(get-fb-user-with-token
(get-fb-access-token code redirect-uri)))
(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))
@duelinmarkers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment