Sendgrid first api requests.

This commit is contained in:
Oliver Marks 2024-01-10 18:47:58 +00:00
parent d1bbfc3b3b
commit 9fb6b33ffc
11 changed files with 110 additions and 4 deletions

View File

@ -16,6 +16,15 @@
(defn build-url [path] (defn build-url [path]
(str (:api-host @api-config) version path)) (str (:api-host @api-config) version path))
(defn verify-email [email]
(merge default-authed-payload
{:uri (build-url "/email-verification/verify")
:method :post
:format (json-request-format)
:response-format (json-response-format {:keywords? true})
:keywords? true
:params email}))
(defn send-email [email] (defn send-email [email]
(merge default-authed-payload (merge default-authed-payload
{:uri (build-url "/email") {:uri (build-url "/email")

View File

@ -4,6 +4,9 @@
(defn init-config [cfg] (defn init-config [cfg]
(c/init-config cfg)) (c/init-config cfg))
(defn verify-email [email]
(c/verify-email email))
(defn send-email [email] (defn send-email [email]
(c/send-email email)) (c/send-email email))

View File

@ -0,0 +1,4 @@
{:paths ["src" "resources"]
:deps {}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,2 @@
(ns org.aplex.rest-sendgrid.oly@fry)

View File

@ -0,0 +1,30 @@
(ns org.aplex.rest-sendgrid.core
(:require
[ajax.core :refer [json-request-format json-response-format]]
[org.aplex.helpers.interface :as h]))
(def version "v3")
(def api-config (atom {:api-host "https://api.sendgrid.com/"
:api-key (h/get-env "SENDGRID_API_KEY" "NO API KEY SUPPLIED")}))
(def default-authed-payload {:headers {"Authorization" (str "Bearer " (:api-key @api-config))
"content-type" "application/json"}})
(defn init-config [cfg]
(reset! api-config (merge @api-config cfg)))
(defn build-url [path]
(str (:api-host @api-config) version path))
(defn send-email
"https://docs.sendgrid.com/api-reference/mail-send/mail-send"
[email]
(merge default-authed-payload
{:uri (build-url "/mail/send")
:method :post
:format (json-request-format)
:response-format (json-response-format {:keywords? true})
:keywords? true
:params email}))
(comment (send-email {:to ""}))

View File

@ -0,0 +1,5 @@
(ns org.aplex.rest-sendgrid.interface
(:require [org.aplex.rest-sendgrid.core :as c]))
(defn send-email [email]
(c/send-email email))

View File

@ -0,0 +1,6 @@
(ns org.aplex.rest-sendgrid.interface-test
(:require [clojure.test :as test :refer :all]
[org.aplex.rest-sendgrid.interface :as rest-sendgrid]))
(deftest dummy-test
(is (= 1 1)))

View File

@ -1,6 +1,7 @@
{:paths ["development/src" {:paths ["development/src"
"components/helpers/src" "components/helpers/src"
"components/rest-stability-ai/src" "components/rest-stability-ai/src"
"components/rest-sendgrid/src"
"components/rest-mailersend/src"] "components/rest-mailersend/src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"} :deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/clojurescript {:mvn/version "1.11.60"} org.clojure/clojurescript {:mvn/version "1.11.60"}

View File

@ -2,7 +2,7 @@
(:require (:require
[org.aplex.helpers.interface :as h] [org.aplex.helpers.interface :as h]
[ajax.core [ajax.core
:refer [ajax-request GET json-request-format json-response-format POST ]] :refer [ajax-request]]
[org.aplex.mailersend.interface :as m])) [org.aplex.mailersend.interface :as m]))
(def response (atom nil)) (def response (atom nil))
@ -21,14 +21,22 @@
(m/send-email) (m/send-email)
(h/attach-handler prn)) (h/attach-handler prn))
(-> {:to [{:email "olymk2@gmail.com" (-> {:email "olymk2@gmail.com"}
(m/verify-email)
(h/attach-handler (partial reset! response))
(ajax-request)
(deref))
@response
(-> {:to [{:email #_"test@theplantscape.com" "oly@digitaloctave.com"
:name "oly"}] :name "oly"}]
:from {:email "test@theplantscape.com" :from {:email "hello@theplantscape.com"
:name "test"} :name "test"}
:subject "Test mailersend api" :subject "Test mailersend api"
:text "Hi {$name}" :text "Hi {$name}"
:html "Hi {$name}" :html "Hi {$name}"
:variables [{:email "olymk2@gmail.com" #_:variables #_[{:email "olymk2@gmail.com"
:substitutions [{:var "name" :substitutions [{:var "name"
:value "Test name"}]}]} :value "Test name"}]}]}

View File

@ -0,0 +1,37 @@
(ns backend.sendgrid
(:require
[org.aplex.helpers.interface :as h]
[ajax.core
:refer [ajax-request]]
[org.aplex.rest-sendgrid.interface :as m]))
(def response (atom nil))
(-> {:personalizations:to [{:email "oly@digitaloctave.com"
:name "oly"}]
:from {:email "hello@theplantscape.com"
:name "test"}
:subject "Test sendgrid api"
:content [{:type "text/plain" :value "body"}]
}
(m/send-email)
(h/attach-handler (partial reset! response))
)
(-> {:personalizations [{:to [{:email "oly@digitaloctave.com"
:name "oly"}]}]
:from {:email "hello@theplantscape.com"
:name "test"}
:subject "Test sendgrid api"
:content [{:type "text/plain" :value "email body"}
{:type "text/html" :value "email body"}]
}
(m/send-email)
(h/attach-handler (partial reset! response))
(ajax-request)
(deref))
@response