A Clojure library designed to support Sendcloud API.
As Sendcloud APIv2 don't support HTTPS, this lib just support APIv1.
clj-sendcloud use clj-http as http client, make sure add clj-http dependencies. eg:
[clj-http "2.1.0"]
Add clj-sendcloud dependence:
Examples:
(require '[clj-sendcloud.core :as s])
;; tlist
(s/tlist {:api_user "XXXXXXXX" :api_key "XXXXXXXX"} {})
; =>
;["https://sendcloud.sohu.com/webapi/template.get.json" {:api_user "XXXXXXXX", :api_key "XXXXXXXX"}]
;; wrap-request
(->> (tlist {:api_user "XXXXXXXX" :api_key "XXXXXXXX"} {})
s/wrap-request)
; =>
;["https://sendcloud.sohu.com/webapi/template.get.json"
; {:body "api_user=XXXXXXXX&api_key=XXXXXXXX",
; :content-type "application/x-www-form-urlencoded",
; :as :json,
; :coerce :always}]
;; clj-http request
(require '[clj-http.client :as c])
(->> (s/tlist {:api_user "XXXXX" :api_key "XXXXX"} {})
s/wrap-request
(apply c/post))
; =>
;{:status 200,
; :headers {"Server" "nginx",
; "Date" "Wed, 15 Jun 2016 08:08:31 GMT",
; "Content-Type" "application/json;charset=UTF-8",
; "Transfer-Encoding" "chunked",
; "Connection" "close"},
; :body ... ,
; :request-time 246,
; :trace-redirects ["https://sendcloud.sohu.com/webapi/template.get.json"],
; :orig-content-encoding "gzip",
; :cookies ... }
;; template send. Note that: if `:to` key which in `:substitution_vars`
;; have more than one element,the `:api_user` type should be `batch` , and
;; the template type should be `batch` as well.
(->> (s/send-tpl {:api_user "XXXXXX"
:api_key "XXXXXX"}
{:from "XXX@XXX.cn"
:template_invoke_name "new_order_confirm"
:substitution_vars {:to ["somebody-email-address@qq.com"]
:sub {:%user_name% ["test_user"]
:%total_price% [1123]}}})
(s/wrap-request)
(apply c/post))
; =>
; {:status 200,
; :headers
; {"Server" "nginx",
; "Date" "Mon, 20 Jun 2016 09:09:02 GMT",
; "Content-Type" "application/json;charset=UTF-8",
; "Transfer-Encoding" "chunked",
; "Connection" "close"},
; :body {:message "success"},
; :request-time 704,
; :trace-redirects
; ["https://sendcloud.sohu.com/webapi/mail.send_template.json"],
; :orig-content-encoding "gzip",
; :cookies
; {"sceroute_c1ba81468573a7be0bc71aebe26c46c3"
; {:discard true,
; :path "/",
; :secure false,
; :value "aac123b39b42f8992f6f7b6fc96d79f4",
; :version 0}}}
;; about sendt
(= (s/send-tpl {:api_user "XXXXXX"
:api_key "XXXXXX"}
{:from "XXX@XXX.cn"
:template_invoke_name "new_order_confirm"
:substitution_vars {:to ["somebody-email-address@qq.com"]
:sub {:%user_name% ["test_user"]
:%total_price% [1123]}}})
(s/sendt {:api_user "XXXXXX"
:api_key "XXXXXX"}
"XXX@XXX.cn"
"new_order_confirm"
{:mail ["somebody-email-address@qq.com"]
:%user_name% ["test_user"]
:%total_price% [1123]}))
;=> true
Copyright © 2016 Michael Wong
Distributed under the Eclipse Public License .