assinafy 0.1.2

Idiomatic async Rust SDK for the Assinafy electronic signature API (https://api.assinafy.com.br/v1).
Documentation
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Assignment (signature-request) endpoints.

use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::error::Result;
use crate::http::HttpClient;
use crate::models::{
    Assignment, AssignmentMethod, CostEstimate, NotificationMethod, ResendCostEstimate,
    ResendNotificationResult, SignDocumentItem, VerificationMethod, WhatsAppNotification,
};

fn reset_expiration_payload(new_expires_at: Option<&str>) -> serde_json::Value {
    serde_json::json!({ "expires_at": new_expires_at })
}

/// Per-signer override used when creating an assignment.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CreateAssignmentSigner {
    /// Signer identifier.
    #[serde(skip_serializing_if = "String::is_empty", default)]
    pub id: String,
    /// Optional 1-based sequential step.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step: Option<u32>,
    /// Override verification method for this signer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_method: Option<VerificationMethod>,
    /// Override notification methods for this signer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_methods: Option<Vec<NotificationMethod>>,
}

impl CreateAssignmentSigner {
    /// Convenience constructor with just the signer id.
    pub fn new<S: Into<String>>(signer_id: S) -> Self {
        Self {
            id: signer_id.into(),
            ..Default::default()
        }
    }

    /// Set or replace the signer id.
    pub fn id<S: Into<String>>(mut self, signer_id: S) -> Self {
        self.id = signer_id.into();
        self
    }

    /// Set the sequential signing step.
    pub fn step(mut self, step: u32) -> Self {
        self.step = Some(step);
        self
    }

    /// Set the verification method for this signer.
    pub fn verification_method(mut self, method: VerificationMethod) -> Self {
        self.verification_method = Some(method);
        self
    }

    /// Set notification methods for this signer.
    pub fn notification_methods(mut self, methods: Vec<NotificationMethod>) -> Self {
        self.notification_methods = Some(methods);
        self
    }
}

/// `POST /documents/{document_id}/assignments` request body.
///
/// New integrations should use `signers`. The `signer_ids` field remains
/// available for legacy deployments that still accept that documented alias.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateAssignmentBody {
    /// Delivery method.
    pub method: AssignmentMethod,
    /// Bulk list of signer identifiers (use when no overrides are needed).
    #[serde(
        rename = "signer_ids",
        alias = "signerIds",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub signer_ids: Vec<String>,
    /// Per-signer configurations (when overrides are needed).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signers: Option<Vec<CreateAssignmentSigner>>,
    /// Field placements for collect assignments.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub entries: Vec<AssignmentEntry>,
    /// Optional invitation message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// Legacy expiration timestamp field accepted by the API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expiration: Option<String>,
    /// Optional expiration timestamp (ISO-8601).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    /// Default verification method when no per-signer override is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verification_method: Option<VerificationMethod>,
    /// Default notification methods when no per-signer override is set.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notification_methods: Option<Vec<NotificationMethod>>,
    /// Optional list of copy-receivers (IDs of signers that only receive
    /// the final certificated copy).
    #[serde(
        rename = "copy_receivers",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub copy_receiver_ids: Vec<String>,
}

impl CreateAssignmentBody {
    /// Build a bulk request with one or more signer IDs.
    ///
    /// The IDs are serialized using the current `signers: [{ id }]` request
    /// shape. Use [`CreateAssignmentBody::legacy_signer_ids`] only when an
    /// older deployment specifically requires `signer_ids`.
    pub fn new<I, S>(method: AssignmentMethod, signer_ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let signers = signer_ids
            .into_iter()
            .map(CreateAssignmentSigner::new)
            .collect::<Vec<_>>();
        Self::from_signers(method, signers)
    }

    /// Build a request from per-signer configuration objects.
    pub fn from_signers<I>(method: AssignmentMethod, signers: I) -> Self
    where
        I: IntoIterator<Item = CreateAssignmentSigner>,
    {
        let signers = signers.into_iter().collect::<Vec<_>>();
        Self {
            method,
            signer_ids: Vec::new(),
            signers: (!signers.is_empty()).then_some(signers),
            entries: Vec::new(),
            message: None,
            expiration: None,
            expires_at: None,
            verification_method: None,
            notification_methods: None,
            copy_receiver_ids: Vec::new(),
        }
    }

    /// Set the optional invitation message.
    pub fn message<S: Into<String>>(mut self, message: S) -> Self {
        self.message = Some(message.into());
        self
    }

    /// Set the expiration timestamp.
    pub fn expires_at<S: Into<String>>(mut self, ts: S) -> Self {
        self.expires_at = Some(ts.into());
        self
    }

    /// Set the legacy `expiration` timestamp used by collect assignment docs.
    pub fn expiration<S: Into<String>>(mut self, ts: S) -> Self {
        self.expiration = Some(ts.into());
        self
    }

    /// Use per-signer overrides instead of a bulk list.
    pub fn with_signers(mut self, signers: Vec<CreateAssignmentSigner>) -> Self {
        self.signers = Some(signers);
        self.signer_ids.clear();
        self
    }

    /// Set per-signer overrides.
    pub fn signers<I>(mut self, signers: I) -> Self
    where
        I: IntoIterator<Item = CreateAssignmentSigner>,
    {
        let signers = signers.into_iter().collect::<Vec<_>>();
        self.signers = Some(signers);
        self.signer_ids.clear();
        self
    }

    /// Send signer IDs using the legacy `signer_ids` request field.
    pub fn legacy_signer_ids<I, S>(mut self, signer_ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.signer_ids = signer_ids.into_iter().map(Into::into).collect();
        self.signers = None;
        self
    }

    /// Set collect-assignment entries.
    pub fn entries(mut self, entries: Vec<AssignmentEntry>) -> Self {
        self.entries = entries;
        self
    }

    /// Default verification method.
    pub fn verification_method(mut self, method: VerificationMethod) -> Self {
        self.verification_method = Some(method);
        self
    }

    /// Default notification methods.
    pub fn notification_methods(mut self, methods: Vec<NotificationMethod>) -> Self {
        self.notification_methods = Some(methods);
        self
    }

    /// Add copy-only recipients.
    pub fn copy_receivers<I, S>(mut self, ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.copy_receiver_ids = ids.into_iter().map(Into::into).collect();
        self
    }
}

/// Entry used to place fields on a document page for collect assignments.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssignmentEntry {
    /// Document page identifier.
    pub page_id: String,
    /// Fields placed on this page.
    pub fields: Vec<AssignmentField>,
}

impl AssignmentEntry {
    /// Build an assignment entry.
    pub fn new<S: Into<String>>(page_id: S, fields: Vec<AssignmentField>) -> Self {
        Self {
            page_id: page_id.into(),
            fields,
        }
    }
}

/// Field placement used in a collect assignment entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssignmentField {
    /// Signer identifier.
    pub signer_id: String,
    /// Field definition identifier.
    pub field_id: String,
    /// Visual placement settings.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_settings: Option<serde_json::Value>,
}

impl AssignmentField {
    /// Build a field placement.
    pub fn new<S, F>(signer_id: S, field_id: F) -> Self
    where
        S: Into<String>,
        F: Into<String>,
    {
        Self {
            signer_id: signer_id.into(),
            field_id: field_id.into(),
            display_settings: None,
        }
    }

    /// Set visual placement settings.
    pub fn display_settings(mut self, settings: impl Into<serde_json::Value>) -> Self {
        self.display_settings = Some(settings.into());
        self
    }
}

/// Body for `POST /documents/{document_id}/assignments/estimate-cost`.
///
/// Accepts the same fields as a full create request; the server inspects them
/// without actually dispatching anything.
pub type EstimateAssignmentCostBody = CreateAssignmentBody;

/// Assignment endpoints.
#[derive(Debug)]
pub struct AssignmentsApi<'a> {
    http: &'a HttpClient,
}

impl<'a> AssignmentsApi<'a> {
    pub(crate) fn new(http: &'a HttpClient) -> Self {
        Self { http }
    }

    /// Request signatures from one or more signers.
    ///
    /// `POST /documents/{document_id}/assignments`.
    pub async fn create<S: AsRef<str>>(
        &self,
        document_id: S,
        body: &CreateAssignmentBody,
    ) -> Result<Assignment> {
        let path = format!("documents/{}/assignments", document_id.as_ref());
        let req = self.http.request(Method::POST, &path)?.json(body);
        self.http.send_data(req).await
    }

    /// Estimate the cost of an assignment without creating it.
    ///
    /// `POST /documents/{document_id}/assignments/estimate-cost`.
    pub async fn estimate_cost<S: AsRef<str>>(
        &self,
        document_id: S,
        body: &EstimateAssignmentCostBody,
    ) -> Result<CostEstimate> {
        let path = format!(
            "documents/{}/assignments/estimate-cost",
            document_id.as_ref()
        );
        let req = self.http.request(Method::POST, &path)?.json(body);
        self.http.send_envelope(req).await
    }

    /// Extend an assignment's expiration deadline.
    ///
    /// `PUT /documents/{document_id}/assignments/{assignmentId}/reset-expiration`.
    pub async fn reset_expiration<D: AsRef<str>, A: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
        new_expires_at: Option<&str>,
    ) -> Result<Assignment> {
        let path = format!(
            "documents/{}/assignments/{}/reset-expiration",
            document_id.as_ref(),
            assignment_id.as_ref()
        );
        let req = self
            .http
            .request(Method::PUT, &path)?
            .json(&reset_expiration_payload(new_expires_at));
        self.http.send_envelope(req).await
    }

    /// Re-send notifications to the assignment's outstanding signers.
    ///
    /// `PUT /documents/{document_id}/assignments/{assignmentId}/resend`.
    pub async fn resend<D: AsRef<str>, A: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
    ) -> Result<Assignment> {
        let path = format!(
            "documents/{}/assignments/{}/resend",
            document_id.as_ref(),
            assignment_id.as_ref()
        );
        let req = self.http.request(Method::PUT, &path)?;
        self.http.send_envelope(req).await
    }

    /// Re-send notifications to a specific signer.
    ///
    /// `PUT /documents/{document_id}/assignments/{assignment_id}/signers/{signer_id}/resend`.
    pub async fn resend_to_signer<D: AsRef<str>, A: AsRef<str>, S: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
        signer_id: S,
    ) -> Result<ResendNotificationResult> {
        let path = format!(
            "documents/{}/assignments/{}/signers/{}/resend",
            document_id.as_ref(),
            assignment_id.as_ref(),
            signer_id.as_ref()
        );
        let req = self.http.request(Method::PUT, &path)?;
        self.http.send_envelope(req).await
    }

    /// Estimate the cost of re-sending a notification to one signer.
    ///
    /// `POST /documents/{document_id}/assignments/{assignment_id}/signers/{signer_id}/estimate-resend-cost`.
    pub async fn estimate_resend_cost<D: AsRef<str>, A: AsRef<str>, S: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
        signer_id: S,
    ) -> Result<ResendCostEstimate> {
        let path = format!(
            "documents/{}/assignments/{}/signers/{}/estimate-resend-cost",
            document_id.as_ref(),
            assignment_id.as_ref(),
            signer_id.as_ref()
        );
        let req = self.http.request(Method::POST, &path)?;
        self.http.send_envelope(req).await
    }

    /// List WhatsApp notifications for an assignment.
    ///
    /// `GET /documents/{document_id}/assignments/{assignment_id}/whatsapp-notifications`.
    pub async fn whatsapp_notifications<D: AsRef<str>, A: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
    ) -> Result<Vec<WhatsAppNotification>> {
        let path = format!(
            "documents/{}/assignments/{}/whatsapp-notifications",
            document_id.as_ref(),
            assignment_id.as_ref()
        );
        let req = self.http.request(Method::GET, &path)?;
        self.http.send_envelope(req).await
    }

    /// Sign a document assignment on behalf of a signer access-code flow.
    ///
    /// `POST /documents/{document_id}/assignments/{assignment_id}`.
    pub async fn sign<D: AsRef<str>, A: AsRef<str>, I>(
        &self,
        document_id: D,
        assignment_id: A,
        items: I,
    ) -> Result<()>
    where
        I: IntoIterator<Item = SignDocumentItem>,
    {
        let path = format!(
            "documents/{}/assignments/{}",
            document_id.as_ref(),
            assignment_id.as_ref()
        );
        let items: Vec<SignDocumentItem> = items.into_iter().collect();
        let req = self.http.request(Method::POST, &path)?.json(&items);
        self.http.send_no_content(req).await
    }

    /// Reject a document assignment on behalf of a signer access-code flow.
    ///
    /// `PUT /documents/{document_id}/assignments/{assignment_id}/reject`.
    pub async fn reject<D: AsRef<str>, A: AsRef<str>, R: AsRef<str>>(
        &self,
        document_id: D,
        assignment_id: A,
        reason: R,
    ) -> Result<()> {
        let path = format!(
            "documents/{}/assignments/{}/reject",
            document_id.as_ref(),
            assignment_id.as_ref()
        );
        let req = self
            .http
            .request(Method::PUT, &path)?
            .json(&serde_json::json!({ "decline_reason": reason.as_ref() }));
        self.http.send_no_content(req).await
    }
}

#[cfg(test)]
mod tests {
    use super::reset_expiration_payload;

    #[test]
    fn reset_expiration_none_serializes_null() {
        assert_eq!(
            reset_expiration_payload(None),
            serde_json::json!({ "expires_at": null })
        );
    }

    #[test]
    fn reset_expiration_some_serializes_timestamp() {
        assert_eq!(
            reset_expiration_payload(Some("2026-06-01T12:00:00Z")),
            serde_json::json!({ "expires_at": "2026-06-01T12:00:00Z" })
        );
    }
}