-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.clj
173 lines (157 loc) · 6.88 KB
/
api.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/
(ns coop.magnet.esignatures.docusign.api
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.spec.alpha :as s]
[coop.magnet.esignatures.core :as core]
[coop.magnet.esignatures.docusign.oauth :as oauth]
[coop.magnet.esignatures.util :as util])
(:import [java.io ByteArrayOutputStream]
[java.io BufferedInputStream]
[java.util Base64]))
(defn input-stream->base64 [^BufferedInputStream input-stream]
(with-open [is input-stream
out (ByteArrayOutputStream.)]
(io/copy is out)
(.encodeToString (Base64/getEncoder) (.toByteArray out))))
(defn- document->api-document
[{:keys [name file-extension stream]}]
{:name name
:fileExtension file-extension
:documentBase64 (input-stream->base64 stream)})
(defn- add-ids [id-key entities]
(map-indexed
(fn [idx entity]
(assoc entity id-key (inc idx)))
entities))
(defn- documents->api-documents [documents]
(->> documents
(map document->api-document)
(add-ids :documentId)))
(defn- signer->api-signer [signer]
(-> signer
(assoc :recipientId (:id signer))
(set/rename-keys {:id :clientUserId})))
(defn- signers->api-signers [signers]
(map signer->api-signer signers))
(defn- envelope->api-envelope
[{:keys [documents signers]}]
{:documents (documents->api-documents documents)
:recipients {:signers (signers->api-signers signers)}
:emailSubject "Please sign this document set"
:status "sent"})
(s/fdef create-envelope
:args ::core/create-envelope-args
:ret ::core/create-envelope-ret)
(defn create-envelope
[{:keys [base-url account-id retry-config] :as adapter} envelope opts]
{:pre [(s/valid? ::core/envelope envelope)
(s/valid? ::core/create-envelope-opts opts)]}
(if-let [access-token (oauth/get-access-token adapter)]
(let [api-envelope (envelope->api-envelope envelope)
{:keys [status body]}
(util/do-request {:method :post
:url (format "%s/restapi/v2.1/accounts/%s/envelopes" base-url account-id)
:headers {"Authorization" (str "Bearer " access-token)}
:body (merge api-envelope opts)}
retry-config)]
(if (and (= status :ok) (:envelopeId body))
{:success? true :id (:envelopeId body)}
{:success? false
:reason :request-failed
:error-details {:body body}}))
{:success? false
:reason :could-not-get-access-token}))
(s/fdef delete-envelope
:args ::core/delete-envelope-args
:ret ::core/delete-envelope-ret)
(defn delete-envelope
[{:keys [base-url account-id retry-config] :as adapter} envelope-id opts]
{:pre [(s/valid? ::core/envelope-id envelope-id)
(s/valid? ::core/delete-envelope-opts opts)]}
(if-let [access-token (oauth/get-access-token adapter)]
(let [{:keys [status body]}
(util/do-request {:method :put
:url (format "%s/restapi/v2.1/accounts/%s/envelopes/%s"
base-url account-id envelope-id)
:headers {"Authorization" (str "Bearer " access-token)}
:body (merge
{:status "voided"
:voidedReason "API delete"}
opts)}
retry-config)]
(if (= status :ok)
{:success? true}
{:success? false
:reason :request-failed
:error-details {:status status
:body body}}))
{:success? false
:reason :could-not-get-access-token}))
(s/fdef get-signing-url
:args ::core/get-envelope-signing-url-args
:ret ::core/get-envelope-signing-url-ret)
(defn get-envelope-signing-url
[{:keys [retry-config base-url account-id] :as adapter} envelope-id {:keys [id email name] :as signer} return-url opts]
{:pre [(s/valid? ::core/envelope-id envelope-id)
(s/valid? ::core/signer signer)
(s/valid? ::core/return-url return-url)
(s/valid? ::core/get-envelope-signing-url-opts opts)]}
(if-let [access-token (oauth/get-access-token adapter)]
(let [view {:returnUrl return-url
:authenticationMethod "none"
:email email
:userName name
:clientUserId id}
{:keys [status body]}
(util/do-request {:method :post
:url (format "%s/restapi/v2.1/accounts/%s/envelopes/%s/views/recipient"
base-url account-id envelope-id)
:headers {"Authorization" (str "Bearer " access-token)}
:body (merge view opts)}
retry-config)]
(if (and (= status :ok) (:url body))
{:success? true :url (:url body)}
{:success? false
:reason :request-failed
:error-details {:body body}}))
{:success? false
:reason :could-not-get-access-token}))
(defn get-envelope-documents
[{:keys [retry-config base-url account-id] :as adapter} envelope-id opts]
(if-let [access-token (oauth/get-access-token adapter)]
(let [mode (get opts :documentId "combined")
{:keys [status body]}
(util/do-request {:method :get
:url (format "%s/restapi/v2.1/accounts/%s/envelopes/%s/documents/%s"
base-url account-id envelope-id mode)
:headers {"Authorization" (str "Bearer " access-token)}}
retry-config)]
(if (and (= status :ok) body)
{:success? true
:documents body}
{:success? false
:reason :request-failed
:error-details {:body body}}))
{:success? false
:reason :could-not-get-access-token}))
(defrecord DocuSign [auth-config base-url account-id retry-config]
core/ESignature
(create-envelope [this envelope]
(create-envelope this envelope {}))
(create-envelope [this envelope opts]
(create-envelope this envelope opts))
(delete-envelope [this envelope-id]
(delete-envelope this envelope-id {}))
(delete-envelope [this envelope-id opts]
(delete-envelope this envelope-id opts))
(get-envelope-signing-url [this envelope-id signer return-url]
(get-envelope-signing-url this envelope-id signer return-url {}))
(get-envelope-signing-url [this envelope-id signer return-url opts]
(get-envelope-signing-url this envelope-id signer return-url opts))
(get-envelope-documents [this envelope-id]
(get-envelope-documents this envelope-id {}))
(get-envelope-documents [this envelope-id opts]
(get-envelope-documents this envelope-id opts)))