Skip to content

Instantly share code, notes, and snippets.

@lynxluna
Created August 17, 2014 05:17
Show Gist options
  • Save lynxluna/fe76a86396afab6ecc1f to your computer and use it in GitHub Desktop.
Save lynxluna/fe76a86396afab6ecc1f to your computer and use it in GitHub Desktop.
Minimalist Facebook Graph API Client
;; 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