Hubspot integration

* support for creating contacts and listing contacts using Hubspot's
  HTTP API
* create a Hubspot contact when a new user signs up (commented out for now)
This commit is contained in:
Teemu Patja 2017-10-29 19:31:53 +02:00
parent e7dc29f895
commit cb28db4fee
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
2 changed files with 42 additions and 1 deletions

View File

@ -6,7 +6,7 @@
[commiteth.db.users :as users]
[commiteth.config :refer [env]]
[ring.util.http-response :refer [content-type ok found]]
[commiteth.layout :refer [render]]
[commiteth.util.hubspot :as hubspot]
[cheshire.core :refer [generate-string]]
[clojure.tools.logging :as log]
[clojure.string :as str]))
@ -43,7 +43,14 @@
(found (str (env :server-address) "/"))
(let [admin-token? (str/includes? scope "repo")
token-key (if admin-token? :admin-token :token)
gh-user (github/get-user access-token)
new-user? (nil? (users/get-user (:id gh-user 0)))
user (assoc (get-or-create-user access-token)
token-key access-token)]
(when new-user?
;; TODO: uncomment when hubspot workflow config finalized
#_(hubspot/create-hubspot-contact (:email user)
(:name user "")
(:login user)))
(assoc (found (str (env :server-address) "/"))
:session {:identity user}))))))

View File

@ -0,0 +1,34 @@
(ns commiteth.util.hubspot
(:require [commiteth.config :refer [env]]
[clojure.tools.logging :as log]
[clj-http.client :as http]))
(defn hubspot-api-key []
(let [key (env :hubspot-api-key)]
(assert (not-empty key))
key))
(def BASE-URL "https://api.hubapi.com")
(defn get-all-contacts []
(let [url (str BASE-URL
"/contacts/v1/lists/all/contacts/all?hapikey="
(hubspot-api-key))]
(http/get url
{:accept :json
:as :json})))
(defn create-hubspot-contact
"Create a hubspot contact using data for a newly signed up user.
https://developers.hubspot.com/docs/methods/contacts/create_contact"
[email name github-login]
(let [endpoint-url (str BASE-URL "/contacts/v1/contact/?hapikey=" (hubspot-api-key))
payload {:properties [{:property :email
:value email}
{:property "github_display_name"
:value name}
{:property "github_username"
:value github-login}]}]
(http/post endpoint-url {:form-params payload
:content-type :json})))