Created
August 17, 2014 05:17
-
-
Save lynxluna/fe76a86396afab6ecc1f to your computer and use it in GitHub Desktop.
Minimalist Facebook Graph API Client
This file contains hidden or 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
;; Depends on clj-http and cheshire | |
(ns com.ykode.social.fb-lite | |
(:require [clj-http.client :as client] | |
[cheshire.core :refer [parse-string]])) | |
(defn- fburl [ext] (str "https://graph.facebook.com/" ext)) | |
(defn- auth-get | |
[access-token url] | |
(client/get url {:headers {"Authorization" (str "Bearer " access-token)}})) | |
(defn- fb-auth-get | |
[access-token path] | |
(clojure.walk/keywordize-keys (parse-string (:body (auth-get access-token (fburl path)))))) | |
;; Programmatically create a MultiMethods For Each Graph EndPoint | |
(defmacro defgraph | |
[nm path] | |
`(def ~nm (doto (clojure.lang.MultiFn. ~(name nm) class :default #'clojure.core/global-hierarchy) | |
(.addMethod String #(fb-auth-get % ~path)) | |
(.addMethod clojure.lang.IPersistentMap #(:access-token %))))) | |
;; Example of using defgraph macro | |
;; Usage: | |
;; Can be used with access token parameter or a map | |
;; (fb/me "my-access-token") or (fb/me {:access-token "my-access-token"}) | |
(defgraph me "me") | |
(defgraph me-permissions "me/permissions") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment