Code Monkey home page Code Monkey logo

clj-sendcloud's Introduction

clj-sendcloud

A Clojure library designed to support Sendcloud API.

As Sendcloud APIv2 don't support HTTPS, this lib just support APIv1.

Usage

clj-sendcloud use clj-http as http client, make sure add clj-http dependencies. eg:

[clj-http "2.1.0"]

Add clj-sendcloud dependence:

Clojars Project

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    "[email protected]"
                  :template_invoke_name "new_order_confirm"
                  :substitution_vars {:to ["[email protected]"]
                                      :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                 "[email protected]"
                :template_invoke_name "new_order_confirm"
                :substitution_vars    {:to ["[email protected]"]
                                       :sub {:%user_name%   ["test_user"]
                                             :%total_price% [1123]}}})
   (s/sendt {:api_user "XXXXXX"
             :api_key  "XXXXXX"}
            "[email protected]"
            "new_order_confirm"
            {:mail         ["[email protected]"]
             :%user_name%  ["test_user"]
             :%total_price% [1123]}))
;=> true

License

Copyright © 2016 Michael Wong

Distributed under the Eclipse Public License .

clj-sendcloud's People

Contributors

defclass avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.