jortt 0.1.0

Async Rust SDK for the Jortt API with typed modules, hybrid OAuth helpers, and raw operation escape hatch
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! High-level Jortt API client.

use std::sync::Arc;
use std::time::Duration;

use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT};
use reqwest::{Method, StatusCode, Url};
use serde::de::DeserializeOwned;
use serde_json::Value;
use tokio::time::sleep;

use crate::api::endpoints::ApiMethods;
use crate::api::operations::{
    BankAccountsOperation, CustomersOperation, EstimatesOperation, ExpensesOperation,
    FilesOperation, HttpMethod, InboxOperation, InvoicesOperation, LabelsOperation,
    LedgerAccountsOperation, LoonjournaalpostenOperation, OperationSpec, OrganizationsOperation,
    ProjectsOperation, ReportsOperation, TradenamesOperation, V2Operation,
};
use crate::api::{DomainApi, OperationRequest};
use crate::auth::AccessTokenSource;
use crate::error::{ErrorBuilder, ErrorEnvelope, JorttError};
use crate::models::common::DataEnvelope;
use crate::models::customers::{
    Customer, ListCustomersQuery, ListCustomersResponse, UpsertCustomerRequest,
};
use crate::models::invoices::{
    CreateInvoiceRequest, Invoice, InvoiceDownload, ListInvoicesQuery, ListInvoicesResponse,
};
use crate::models::ledger_accounts::{LedgerAccount, ListLedgerAccountsResponse};

const PATH_ENCODE_SET: &AsciiSet = &CONTROLS
    .add(b' ')
    .add(b'"')
    .add(b'#')
    .add(b'%')
    .add(b'/')
    .add(b'<')
    .add(b'>')
    .add(b'?')
    .add(b'`')
    .add(b'{')
    .add(b'}');

struct ClientInner {
    http: reqwest::Client,
    base_url: Url,
    token_source: Option<Arc<dyn AccessTokenSource>>,
    max_retries: u8,
}

/// Builder for [`JorttClient`].
#[derive(Clone)]
pub struct JorttClientBuilder {
    base_url: Url,
    timeout: Duration,
    user_agent: String,
    token_source: Option<Arc<dyn AccessTokenSource>>,
    max_retries: u8,
}

impl Default for JorttClientBuilder {
    fn default() -> Self {
        Self {
            base_url: Url::parse("https://api.jortt.nl/").expect("static URL must be valid"),
            timeout: Duration::from_secs(30),
            user_agent: format!("jortt-rs/{}", env!("CARGO_PKG_VERSION")),
            token_source: None,
            max_retries: 2,
        }
    }
}

impl JorttClientBuilder {
    /// Creates a default builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Overrides the API base URL.
    pub fn with_base_url(mut self, base_url: Url) -> Self {
        self.base_url = base_url;
        self
    }

    /// Sets request timeout.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets user-agent header value.
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// Sets retry count for transport and retryable HTTP statuses.
    pub fn with_max_retries(mut self, max_retries: u8) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Sets bearer token source.
    pub fn with_token_source(mut self, token_source: Arc<dyn AccessTokenSource>) -> Self {
        self.token_source = Some(token_source);
        self
    }

    /// Builds the SDK client.
    pub fn build(self) -> Result<JorttClient, JorttError> {
        let http = reqwest::Client::builder()
            .timeout(self.timeout)
            .build()
            .map_err(JorttError::Transport)?;

        Ok(JorttClient {
            inner: Arc::new(ClientInner {
                http,
                base_url: self.base_url,
                token_source: self.token_source,
                max_retries: self.max_retries,
            }),
            user_agent: self.user_agent,
        })
    }
}

/// Main Jortt API client.
#[derive(Clone)]
pub struct JorttClient {
    inner: Arc<ClientInner>,
    user_agent: String,
}

impl JorttClient {
    /// Returns a new builder.
    pub fn builder() -> JorttClientBuilder {
        JorttClientBuilder::new()
    }

    /// Returns the configured base URL.
    pub fn base_url(&self) -> &Url {
        &self.inner.base_url
    }

    /// Returns grouped generated methods for every OpenAPI operation.
    pub fn methods(&self) -> ApiMethods {
        ApiMethods::new(self.clone())
    }

    /// Typed operation access for `customers` tag.
    pub fn customers(&self) -> DomainApi<CustomersOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `invoices` tag.
    pub fn invoices(&self) -> DomainApi<InvoicesOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `ledger_accounts` tag.
    pub fn ledger_accounts(&self) -> DomainApi<LedgerAccountsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `projects` tag.
    pub fn projects(&self) -> DomainApi<ProjectsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `reports` tag.
    pub fn reports(&self) -> DomainApi<ReportsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `expenses` tag.
    pub fn expenses(&self) -> DomainApi<ExpensesOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `customers` tag.
    pub fn estimates(&self) -> DomainApi<EstimatesOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `files` tag.
    pub fn files(&self) -> DomainApi<FilesOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `inbox` tag.
    pub fn inbox(&self) -> DomainApi<InboxOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `labels` tag.
    pub fn labels(&self) -> DomainApi<LabelsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `loonjournaalposten` tag.
    pub fn loonjournaalposten(&self) -> DomainApi<LoonjournaalpostenOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `organizations` tag.
    pub fn organizations(&self) -> DomainApi<OrganizationsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `tradenames` tag.
    pub fn tradenames(&self) -> DomainApi<TradenamesOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `bank-accounts` tag.
    pub fn bank_accounts(&self) -> DomainApi<BankAccountsOperation> {
        DomainApi::new(self.clone())
    }

    /// Typed operation access for `v2` tag.
    pub fn v2(&self) -> DomainApi<V2Operation> {
        DomainApi::new(self.clone())
    }

    /// Creates a customer using the unversioned endpoint.
    pub async fn create_customer(
        &self,
        request: &UpsertCustomerRequest,
    ) -> Result<Customer, JorttError> {
        let req = OperationRequest::new().with_json_body(request)?;
        let value = self
            .customers()
            .execute(CustomersOperation::PostCustomers, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Updates a customer using the unversioned endpoint.
    pub async fn update_customer(
        &self,
        customer_id: &str,
        request: &UpsertCustomerRequest,
    ) -> Result<Customer, JorttError> {
        let req = OperationRequest::new()
            .with_path_param("customer_id", customer_id)
            .with_json_body(request)?;

        let value = self
            .customers()
            .execute(CustomersOperation::PutCustomersByCustomerId, req)
            .await?;

        parse_data_envelope(value)
    }

    /// Fetches a customer by ID.
    pub async fn get_customer(&self, customer_id: &str) -> Result<Customer, JorttError> {
        let req = OperationRequest::new().with_path_param("customer_id", customer_id);
        let value = self
            .customers()
            .execute(CustomersOperation::GetCustomersByCustomerId, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Lists customers, optionally filtered with query parameters.
    pub async fn list_customers(
        &self,
        query: &ListCustomersQuery,
    ) -> Result<ListCustomersResponse, JorttError> {
        let mut req = OperationRequest::new();
        if let Some(value) = &query.query {
            req = req.with_query_param("query", value);
        }
        if let Some(value) = query.page {
            req = req.with_query_param("page", value);
        }
        let value = self
            .customers()
            .execute(CustomersOperation::GetCustomers, req)
            .await?;
        parse_json(value)
    }

    /// Sends a direct debit mandate request to the customer.
    pub async fn send_customer_direct_debit_mandate(
        &self,
        customer_id: &str,
    ) -> Result<Customer, JorttError> {
        let req = OperationRequest::new().with_path_param("customer_id", customer_id);
        let value = self
            .customers()
            .execute(
                CustomersOperation::PostCustomersByCustomerIdDirectDebitMandate,
                req,
            )
            .await?;
        parse_data_envelope(value)
    }

    /// Deletes a customer and returns the deleted resource snapshot.
    pub async fn delete_customer(&self, customer_id: &str) -> Result<Customer, JorttError> {
        let req = OperationRequest::new().with_path_param("customer_id", customer_id);
        let value = self
            .customers()
            .execute(CustomersOperation::DeleteCustomersByCustomerId, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Creates an invoice using the unversioned endpoint.
    pub async fn create_invoice(
        &self,
        request: &CreateInvoiceRequest,
    ) -> Result<Invoice, JorttError> {
        let req = OperationRequest::new().with_json_body(request)?;
        let value = self
            .invoices()
            .execute(InvoicesOperation::PostInvoices, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Lists invoices with optional search/status filters.
    pub async fn list_invoices(
        &self,
        query: &ListInvoicesQuery,
    ) -> Result<ListInvoicesResponse, JorttError> {
        let mut req = OperationRequest::new();
        if let Some(value) = &query.query {
            req = req.with_query_param("query", value);
        }
        if let Some(value) = &query.invoice_status {
            req = req.with_query_param("invoice_status", value);
        }
        if let Some(value) = query.page {
            req = req.with_query_param("page", value);
        }
        let value = self
            .invoices()
            .execute(InvoicesOperation::GetInvoices, req)
            .await?;
        parse_json(value)
    }

    /// Fetches a single invoice by ID.
    pub async fn get_invoice(&self, invoice_id: &str) -> Result<Invoice, JorttError> {
        let req = OperationRequest::new().with_path_param("id", invoice_id);
        let value = self
            .invoices()
            .execute(InvoicesOperation::GetInvoicesById, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Requests a temporary invoice PDF download URL.
    pub async fn download_invoice_pdf_url(
        &self,
        invoice_id: &str,
    ) -> Result<InvoiceDownload, JorttError> {
        let req = OperationRequest::new().with_path_param("id", invoice_id);
        let value = self
            .invoices()
            .execute(InvoicesOperation::GetInvoicesByIdDownload, req)
            .await?;
        parse_data_envelope(value)
    }

    /// Lists invoice ledger accounts.
    pub async fn list_invoice_ledger_accounts(&self) -> Result<Vec<LedgerAccount>, JorttError> {
        let value = self
            .ledger_accounts()
            .execute(
                LedgerAccountsOperation::GetLedgerAccountsInvoices,
                OperationRequest::new(),
            )
            .await?;
        let parsed: ListLedgerAccountsResponse = parse_json(value)?;
        Ok(parsed.data)
    }

    pub(crate) async fn execute_spec(
        &self,
        spec: OperationSpec,
        request: OperationRequest,
    ) -> Result<Value, JorttError> {
        self.execute(
            spec.method,
            spec.path,
            request.path_params,
            request.query_params,
            request.body,
            request.accept,
        )
        .await
    }

    pub(crate) async fn execute(
        &self,
        method: HttpMethod,
        path_template: &str,
        path_params: Vec<crate::api::PathParam>,
        query_params: Vec<crate::api::QueryParam>,
        body: Option<Value>,
        accept: Option<String>,
    ) -> Result<Value, JorttError> {
        let path = render_path(path_template, &path_params)?;
        let url = self
            .inner
            .base_url
            .join(path.trim_start_matches('/'))
            .map_err(|err| ErrorBuilder::config(err.to_string()).build())?;

        let method = to_reqwest_method(method);

        for attempt in 0..=self.inner.max_retries {
            let mut req = self
                .inner
                .http
                .request(method.clone(), url.clone())
                .header(USER_AGENT, &self.user_agent);

            if let Some(accept) = &accept {
                req = req.header(ACCEPT, accept);
            }

            if !query_params.is_empty() {
                let pairs = query_params
                    .iter()
                    .map(|param| (param.name.as_str(), param.value.as_str()))
                    .collect::<Vec<_>>();
                req = req.query(&pairs);
            }

            if let Some(token_source) = &self.inner.token_source {
                let token = token_source.access_token().await?;
                req = req.header(AUTHORIZATION, format!("Bearer {token}"));
            }

            if let Some(body) = &body {
                req = req.json(body);
            }

            match req.send().await {
                Ok(response) => {
                    let status = response.status();

                    if should_retry(status) && attempt < self.inner.max_retries {
                        sleep(backoff_delay(attempt)).await;
                        continue;
                    }

                    if status.is_success() {
                        if status == StatusCode::NO_CONTENT {
                            return Ok(Value::Null);
                        }

                        let bytes = response.bytes().await?;
                        if bytes.is_empty() {
                            return Ok(Value::Null);
                        }
                        return Ok(serde_json::from_slice(&bytes)?);
                    }

                    let raw_body = response.text().await.unwrap_or_default();
                    if let Ok(envelope) = serde_json::from_str::<ErrorEnvelope>(&raw_body) {
                        return Err(ErrorBuilder::api(status, envelope.error).build());
                    }

                    return Err(ErrorBuilder::http(status).body(raw_body).build());
                }
                Err(err) => {
                    if attempt < self.inner.max_retries {
                        sleep(backoff_delay(attempt)).await;
                        continue;
                    }
                    return Err(JorttError::Transport(err));
                }
            }
        }

        Err(ErrorBuilder::config("retry loop exhausted unexpectedly").build())
    }
}

fn to_reqwest_method(method: HttpMethod) -> Method {
    match method {
        HttpMethod::Get => Method::GET,
        HttpMethod::Post => Method::POST,
        HttpMethod::Put => Method::PUT,
        HttpMethod::Delete => Method::DELETE,
        HttpMethod::Patch => Method::PATCH,
        HttpMethod::Options => Method::OPTIONS,
        HttpMethod::Head => Method::HEAD,
    }
}

fn render_path(
    path_template: &str,
    path_params: &[crate::api::PathParam],
) -> Result<String, JorttError> {
    let mut rendered = path_template.to_string();

    for param in path_params {
        let encoded = utf8_percent_encode(&param.value, PATH_ENCODE_SET).to_string();
        rendered = rendered.replace(&format!("{{{}}}", param.name), &encoded);
    }

    if let Some(start) = rendered.find('{') {
        let tail = &rendered[start + 1..];
        let end = tail.find('}').unwrap_or(tail.len());
        let name = tail[..end].to_string();
        return Err(ErrorBuilder::missing_path_param(name, path_template).build());
    }

    Ok(rendered)
}

fn should_retry(status: StatusCode) -> bool {
    status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error()
}

fn backoff_delay(attempt: u8) -> Duration {
    let multiplier = 1u64 << attempt;
    Duration::from_millis(100 * multiplier)
}

fn parse_json<T: DeserializeOwned>(value: Value) -> Result<T, JorttError> {
    serde_json::from_value(value).map_err(JorttError::Deserialize)
}

fn parse_data_envelope<T: DeserializeOwned>(value: Value) -> Result<T, JorttError> {
    let envelope: DataEnvelope<T> = parse_json(value)?;
    Ok(envelope.data)
}