rplaid 0.4.0

An async client library for Plaid APIs.
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
use http_client::h1::H1Client;
use http_client::Error as HttpError;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::model::*;
use crate::HttpClient;

const SANDBOX_DOMAIN: &str = "https://sandbox.plaid.com";
const DEVELOPMENT_DOMAIN: &str = "https://development.plaid.com";
const PRODUCTION_DOMAIN: &str = "https://production.plaid.com";

/// Error returned by client requests.
#[derive(Error, Debug)]
pub enum ClientError {
    /// Wraps errors from the underlying HTTP client.
    #[error("http request failed: {0}")]
    Http(HttpError),
    /// Error either serializing request types or deserializing response types
    /// from requests.
    #[error(transparent)]
    Parse(#[from] serde_json::Error),
    /// Wraps errors from Plaid's API responses. If an error is parsed then
    /// Plaid successfully returned a response but returned with errors.
    #[error(transparent)]
    App(#[from] ErrorResponse),
}

/// Credentials required to make authenticated calls to the Plaid API.
#[derive(Debug, Default)]
pub struct Credentials {
    /// Plaid API client id token.
    pub client_id: String,
    /// Plaid API secret for the configured environment (e.g. sandbox, dev, prod).
    pub secret: String,
}

impl From<HttpError> for ClientError {
    fn from(error: HttpError) -> Self {
        Self::Http(error)
    }
}

/// Environment controls the domain for the client, matches Plaid's sandbox,
/// development, and production environments.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Environment {
    /// Used to configure the client to request against a the domain in the string.
    /// Should be a fully qualified domain with protocol and scheme, for example
    /// http://localhost:3000.
    Custom(String),
    /// Plaid sandbox environment.
    Sandbox,
    /// Plaid development environment.
    Development,
    /// Plaid production environment.
    Production,
}

impl std::default::Default for Environment {
    fn default() -> Self {
        Environment::Sandbox
    }
}

impl std::string::ToString for Environment {
    fn to_string(&self) -> String {
        match self {
            Environment::Sandbox => SANDBOX_DOMAIN.into(),
            Environment::Development => DEVELOPMENT_DOMAIN.into(),
            Environment::Production => PRODUCTION_DOMAIN.into(),
            Environment::Custom(s) => s.into(),
        }
    }
}

/// Plaid API client type.
pub struct Plaid<T: HttpClient> {
    http: T,
    credentials: Credentials,
    env: Environment,
}

/// Builder helps construct Plaid client types with sensible defaults.
pub struct Builder {
    http: Option<Box<dyn HttpClient>>,
    credentials: Option<Credentials>,
    env: Option<Environment>,
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

impl Builder {
    /// Constructs a new Plaid client builder.
    ///
    /// ```
    /// use rplaid::client::Builder;
    ///
    /// let client = Builder::new().build();
    /// ```
    pub fn new() -> Self {
        Self {
            http: None,
            credentials: None,
            env: None,
        }
    }

    /// Override the default HTTP client.
    pub fn with_http_client(mut self, client: impl HttpClient) -> Self {
        self.http = Some(Box::new(client));
        self
    }

    /// Set Plaid API credentials for authenticating Plaid API calls.
    pub fn with_credentials(mut self, creds: Credentials) -> Self {
        self.credentials = Some(creds);
        self
    }

    /// Set API request environment.
    pub fn with_env(mut self, env: Environment) -> Self {
        self.env = Some(env);
        self
    }

    /// Consume a builder returning a Plaid client instance.
    pub fn build(self) -> Plaid<Box<dyn HttpClient>> {
        let http = self.http.unwrap_or_else(|| Box::new(H1Client::new()));
        Plaid {
            http,
            credentials: self.credentials.unwrap_or_default(),
            env: self.env.unwrap_or_default(),
        }
    }
}

impl<T: HttpClient> Plaid<T> {
    /// Creates a new Plaid instance from a set of credentials and an HttpClient.
    ///
    /// ```
    /// use rplaid::client::{Plaid, Credentials, Environment};
    /// use http_client::h1::H1Client;
    ///
    /// let client = Plaid::new(
    ///     H1Client::new(),
    ///     Credentials{client_id: "".into(), secret: "".into()},
    ///     Environment::Sandbox);
    /// ```
    pub fn new(http: T, credentials: Credentials, env: Environment) -> Self {
        Self {
            http,
            credentials,
            env,
        }
    }

    async fn request<E: crate::model::Endpoint>(
        &self,
        endpoint: &E,
    ) -> Result<E::Response, ClientError>
    where
        for<'de> <E as crate::model::Endpoint>::Response: serde::Deserialize<'de>,
    {
        let mut post = endpoint.request(&self.env.to_string());
        post.insert_header("Content-Type", "application/json");
        post.insert_header("PLAID-CLIENT-ID", &self.credentials.client_id);
        post.insert_header("PLAID-SECRET", &self.credentials.secret);
        let mut res = self.http.send(post).await?;

        match res.status() {
            http_client::http_types::StatusCode::Ok => Ok(res.body_json::<E::Response>().await?),
            _ => Err(ClientError::from(res.body_json::<ErrorResponse>().await?)),
        }
    }

    /// Returns details for institutions that match the query parameters up to a
    /// maximum of ten institutions per query.
    ///
    /// https://plaid.com/docs/api/institutions/#institutionssearch
    pub async fn search_institutions<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &InstitutionsSearchRequest<'_, P>,
    ) -> Result<Vec<Institution>, ClientError> {
        Ok(self.request(req).await?.institutions)
    }

    /// Returns details on an institution currently supported by plaid.
    ///
    /// https://plaid.com/docs/api/institutions/#institutionsget_by_id
    pub async fn get_institution_by_id<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &InstitutionGetRequest<'_, P>,
    ) -> Result<Institution, ClientError> {
        Ok(self.request(req).await?.institution)
    }

    /// Returns details on all financial institutions currently supported by
    /// Plaid. Plaid supports thousands of institutions and results are
    /// paginated. Institutions with no overlap to the client's enabled products
    /// are filtered from results.
    ///
    /// https://plaid.com/docs/api/institutions/#institutionsget
    pub async fn get_institutions<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &InstitutionsGetRequest<'_, P>,
    ) -> Result<Vec<Institution>, ClientError> {
        Ok(self.request(req).await?.institutions)
    }

    /// Creates a valid `public_token` for an institution ID, initial products,
    /// and test credentials. The created public token maps to a new Sandbox
    /// item.
    ///
    /// https://plaid.com/docs/api/sandbox/#sandboxpublic_tokencreate
    pub async fn create_public_token<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: CreatePublicTokenRequest<'_, P>,
    ) -> Result<String, ClientError> {
        Ok(self.request(&req).await?.public_token)
    }

    /// Forces an item into an `ITEM_LOGIN_REQUIRED` state in order to simulate
    /// an Item whose login is no longer valid.
    ///
    /// https://plaid.com/docs/api/sandbox/#sandboxitemreset_login
    pub async fn reset_login<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
    ) -> Result<(), ClientError> {
        let res = self.request(&ResetLoginRequest { access_token }).await?;
        match res.reset_login {
            true => Ok(()),
            false => Err(ClientError::App(ErrorResponse {
                error_message: Some("failed to reset login".into()),
                ..ErrorResponse::default()
            })),
        }
    }

    /// Exchange a Link `public_token` for an API `access_token`. Public tokens
    /// are ephemeral and expires after 30 minutes.
    ///
    /// https://plaid.com/docs/api/tokens/#itempublic_tokenexchange
    pub async fn exchange_public_token<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        public_token: P,
    ) -> Result<ExchangePublicTokenResponse, ClientError> {
        self
            .request(&ExchangePublicTokenRequest { public_token })
            .await
    }

    /// Creates a `link_token` that is required as a parameter when initializing
    /// a Link.
    ///
    /// https://plaid.com/docs/api/tokens/#linktokencreate
    pub async fn create_link_token<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &CreateLinkTokenRequest<'_, P>,
    ) -> Result<CreateLinkTokenResponse, ClientError> {
        self.request(req).await
    }

    /// Retrieves information for any linked item, only active accounts are
    /// returned. Responses may be cached, if up-to-date information is required
    /// use `balances` instead.
    ///
    /// https://plaid.com/docs/api/accounts/#accountsget
    pub async fn accounts<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
    ) -> Result<Vec<Account>, ClientError> {
        Ok(self
            .request(&GetAccountsRequest {
                access_token,
                options: None,
            })
            .await?
            .accounts)
    }

    /// Returns information about the status of an Item.
    ///
    /// https://plaid.com/docs/api/items/#itemget
    pub async fn item<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
    ) -> Result<Item, ClientError> {
        Ok(self.request(&GetItemRequest { access_token }).await?.item)
    }

    /// Removes an Item. Once removed, the `access_token` associated with the
    /// Item is no longer valid and cannot be used to access any data that was
    /// associated with the Item.
    ///
    /// https://plaid.com/docs/api/items/#itemremove
    pub async fn item_del<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
    ) -> Result<(), ClientError> {
        self.request(&RemoveItemRequest { access_token }).await?;

        Ok(())
    }

    /// Updates the webhook URL associated with an Item. Updates trigger a
    /// `WEBHOOK_UPDATE_ACKNOWLEDGED` event to the new webhook URL.
    ///
    /// https://plaid.com/docs/api/items/#itemwebhookupdate
    pub async fn item_webhook_update<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
        webhook: P,
    ) -> Result<Item, ClientError> {
        Ok(self
            .request(&UpdateItemWebhookRequest {
                access_token,
                webhook,
            })
            .await?
            .item)
    }

    /// Verify real-time account balances. This endpoint can be used as long as
    /// Link has been initialized with any other product.
    ///
    /// https://plaid.com/docs/api/products/#balance
    pub async fn balances<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        access_token: P,
    ) -> Result<Vec<Account>, ClientError> {
        Ok(self
            .request(&AccountBalancesGetRequest {
                access_token,
                options: None,
            })
            .await?
            .accounts)
    }

    /// Returns the bank account and bank identification numbers associated with
    /// an Item's checking and savings accounts, along with high-level account
    /// data and balances when available.
    ///
    /// https://plaid.com/docs/api/products/#auth
    pub async fn auth<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &GetAuthRequest<'_, P>,
    ) -> Result<GetAuthResponse, ClientError> {
        self.request(req).await
    }

    /// Verify the name, address, phone number, and email address of a user
    /// against bank account information on file.
    ///
    /// https://plaid.com/docs/api/products/#identity
    pub async fn identity<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &GetIdentityRequest<'_, P>,
    ) -> Result<GetIdentityResponse, ClientError> {
        self.request(req).await
    }

    /// Triggers a Transactions `DEFAULT_UPDATE` webhook for a given Sandbox
    /// Item. If the Item does not support Transactions, a
    /// `SANDBOX_PRODUCT_NOT_ENABLED` error will result.
    ///
    /// https://plaid.com/docs/api/sandbox/#sandboxitemfire_webhook
    pub async fn fire_webhook<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &FireWebhookRequest<P>,
    ) -> Result<FireWebhookResponse, ClientError> {
        self.request(req).await
    }

    /// Changes the verification status of an Item in the sandbox in order to
    /// simulate the Automated Micro-deposit flow.
    ///
    /// https://plaid.com/docs/api/sandbox/#sandboxitemset_verification_status
    pub async fn set_verification_status<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &SetVerificationStatusRequest<P>,
    ) -> Result<SetVerificationStatusResponse, ClientError> {
        self.request(req).await
    }

    /// Searches Plaid's database for known employers to use with Deposit
    /// Switch.
    ///
    /// https://plaid.com/docs/api/employers/
    pub async fn search_employers<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &SearchEmployerRequest<'_, P>,
    ) -> Result<SearchEmployerResponse, ClientError> {
        self.request(req).await
    }

    /// Provides a JSON Web Key (JWK) that can be used to verify a JWT.
    ///
    /// https://plaid.com/docs/api/webhooks/webhook-verification/#webhook_verification_keyget
    pub async fn create_webhook_verification_key<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &GetWebhookVerificationKeyRequest<P>,
    ) -> Result<GetWebhookVerificationKeyResponse, ClientError> {
        self.request(req).await
    }

    /// Gets information about a `link_token`, can be useful for debugging.
    ///
    /// https://plaid.com/docs/api/tokens/#linktokenget
    pub async fn link_token<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &GetLinkTokenRequest<P>,
    ) -> Result<GetLinkTokenResponse, ClientError> {
        self.request(req).await
    }

    /// Rotate the `access_token` associated with an Item. Call returns a new
    /// `access_token` and immediately invalidates the previous token.
    ///
    /// https://plaid.com/docs/api/tokens/#itemaccess_tokeninvalidate
    pub async fn invalidate_access_token<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &InvalidateAccessTokenRequest<P>,
    ) -> Result<InvalidateAccessTokenResponse, ClientError> {
        self.request(req).await
    }

    /// Get detailed information on categories returned by Plaid. This endpoint
    /// does not require authentication.
    ///
    /// https://plaid.com/docs/api/products/#categoriesget
    pub async fn categories(
        &self,
        req: &GetCategoriesRequest,
    ) -> Result<GetCategoriesResponse, ClientError> {
        self.request(req).await
    }

    /// Initiates on-demand extraction to fetch the newest transactions for an
    /// Item.
    ///
    /// https://plaid.com/docs/api/products/#transactionsrefresh
    pub async fn refresh_transactions<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &RefreshTransactionsRequest<P>,
    ) -> Result<(), ClientError> {
        self.request(req).await?;
        Ok(())
    }

    /// Returns user-authorized transaction data for credit, depository, and
    /// some loan-type accounts. For transaction history from investment
    /// accounts, use investment endpoints instead (production only). Results
    /// are paginated based on request options and defaults to 100 entities per
    /// page.
    ///
    /// https://plaid.com/docs/api/products/#transactionsget
    pub async fn transactions<P: AsRef<str> + http_types::convert::Serialize>(
        &self,
        req: &GetTransactionsRequest<P>,
    ) -> Result<GetTransactionsResponse, ClientError> {
        self.request(req).await
    }

    /// Returns a Stream of transactions that can be used to iterative fetch
    /// pages from the transaction endpoint. Each call will return the number of
    /// items configured in the original request.
    ///
    /// ```ignore
    /// use futures_util::pin_mut;
    /// use futures_util::StreamExt;
    ///
    /// ...
    ///
    ///   let req = GetTransactionsRequest {
    ///       access_token: res.access_token.as_str(),
    ///       start_date: "2019-09-01",
    ///       end_date: "2021-09-05",
    ///       options: Some(GetTransactionsOptions {
    ///           // Number of items to return per page.
    ///           count: Some(10),
    ///           // Number of items from the start_date to offset results by.
    ///           offset: Some(5),
    ///           account_ids: None,
    ///           include_original_description: None,
    ///       }),
    ///   };
    ///   let iter = client.transactions_iter(req);
    ///   pin_mut!(iter);
    ///
    ///   while let Some(txn) = iter.next().await {
    ///     println!("{:?}", txn);
    ///   }
    /// ```
    #[cfg(feature = "streams")]
    pub fn transactions_iter<'a, P: AsRef<str> + http_types::convert::Serialize + Clone + 'a>(
        &'a self,
        req: GetTransactionsRequest<P>,
    ) -> impl futures_core::stream::Stream<Item = Result<Vec<Transaction>, ClientError>> + 'a {
        async_stream::try_stream! {
            let mut yielded = 0;
            let mut total_xacts = None;
            let mut request = req.clone();
            let count = req.options.as_ref().unwrap().count.unwrap_or(100);
            let mut offset = req.options.as_ref().unwrap().offset.unwrap_or(0);

            while total_xacts.is_none() || total_xacts.unwrap() > yielded {
                if let Some(ref mut opts) = &mut request.options {
                    opts.count = Some(count);
                    opts.offset = Some(offset);
                } else {
                    request.options = Some(GetTransactionsOptions{
                        count: Some(count),
                        offset: Some(offset),
                        account_ids: None,
                        include_original_description: None,
                    });
                }

                let res = self.transactions(&request).await?;
                if total_xacts.is_none() {
                    total_xacts = Some(res.total_transactions - offset);
                }
                yielded += res.transactions.len();
                offset += yielded;

                yield res.transactions;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures_util::pin_mut;
    use futures_util::StreamExt;

    const INSTITUTION_ID: &str = "ins_129571";

    fn credentials() -> Credentials {
        Credentials {
            client_id: std::env::var("PLAID_CLIENT_ID")
                .expect("Variable PLAID_CLIENT_ID must be defined."),
            secret: std::env::var("PLAID_SECRET").expect("Variable PLAID_SECRET must be defined."),
        }
    }

    #[tokio::test]
    async fn unauthorized_calls_return_parsable_error() {
        let client = Builder::new().with_credentials(credentials()).build();
        let res = client
            // Accounts is an authenticated call and requires a valid access token.
            .accounts("")
            .await;

        match res.unwrap_err() {
            ClientError::App(e) => {
                assert_eq!(e.error_type.unwrap(), ErrorType::InvalidRequest);
            }
            _ => panic!("unexpected error"),
        }
    }

    #[tokio::test]
    async fn can_get_multiple_institutions() {
        let client = Builder::new().with_credentials(credentials()).build();
        let res = client
            .get_institutions(&InstitutionsGetRequest {
                count: 10,
                offset: 0,
                country_codes: &["US"],
                options: None,
            })
            .await
            .unwrap();

        insta::assert_json_snapshot!(res);
    }

    #[tokio::test]
    async fn can_fetch_single_institution() {
        let client = Builder::new().with_credentials(credentials()).build();
        let res = client
            .get_institution_by_id(&InstitutionGetRequest {
                institution_id: INSTITUTION_ID,
                country_codes: &[],
                options: None,
            })
            .await
            .unwrap();

        insta::assert_json_snapshot!(res);
    }

    #[tokio::test]
    async fn can_search_institutions() {
        let client = Builder::new().with_credentials(credentials()).build();
        let res = client
            .search_institutions(&InstitutionsSearchRequest {
                query: "Banque Populaire",
                country_codes: &[],
                products: None,
                options: None,
            })
            .await
            .unwrap();

        insta::assert_json_snapshot!(res);
    }

    #[tokio::test]
    async fn can_create_sandbox_pub_token() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance"],
                options: None,
            })
            .await
            .unwrap();

        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());
        // Should succeed.
        client.reset_login(res.access_token).await.unwrap();
    }

    #[tokio::test]
    async fn can_fetch_accounts_with_token() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance"],
                options: None,
            })
            .await
            .unwrap();

        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());
        let accounts = client.accounts(res.access_token).await.unwrap();

        insta::assert_json_snapshot!(accounts, {
            "[].account_id" => "[account_id]"
        });
    }

    #[tokio::test]
    async fn can_modify_items() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance"],
                options: None,
            })
            .await
            .unwrap();

        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());
        let item = client.item(&res.access_token).await.unwrap();

        insta::assert_json_snapshot!(item, {
            ".item_id" => "[item_id]"
        });

        // Should succeed.
        client.item_del(res.access_token).await.unwrap();
    }

    #[tokio::test]
    async fn can_create_link_token() {
        let client = Builder::new().with_credentials(credentials()).build();
        let create_res = client
            .create_link_token(&CreateLinkTokenRequest {
                client_name: "test_client",
                user: LinkUser::new("test-user"),
                language: "en",
                country_codes: &["US"],
                products: &["transactions"],
                ..CreateLinkTokenRequest::default()
            })
            .await
            .unwrap();

        assert!(!create_res.link_token.is_empty());
        // Check that we can read back the token we created.
        let res = client
            .link_token(&GetLinkTokenRequest {
                link_token: &create_res.link_token,
            })
            .await
            .unwrap();
        assert_eq!(create_res.link_token, res.link_token);
    }

    #[tokio::test]
    async fn can_read_transactions() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();

        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());
        // Calling refresh before requesting transactions prevents
        // `PRODUCT_NOT_READY` errors.
        client
            .refresh_transactions(&RefreshTransactionsRequest {
                access_token: &res.access_token,
            })
            .await
            .unwrap();
        let res = client
            .transactions(&GetTransactionsRequest {
                access_token: res.access_token.as_str(),
                start_date: "2021-09-01",
                end_date: "2021-09-05",
                options: None,
            })
            .await
            .unwrap();

        insta::assert_json_snapshot!(res.transactions, {
            "[].transaction_id" => "[transaction_id]",
            "[].account_id" => "[account_id]",
        });
    }

    #[tokio::test]
    async fn can_drain_transaction_stream() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();

        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());
        // Calling refresh before requesting transactions prevents
        // `PRODUCT_NOT_READY` errors.
        client
            .refresh_transactions(&RefreshTransactionsRequest {
                access_token: &res.access_token,
            })
            .await
            .unwrap();

        let req = GetTransactionsRequest {
            access_token: res.access_token.as_str(),
            start_date: "2021-09-01",
            end_date: "2021-09-05",
            options: Some(GetTransactionsOptions {
                count: Some(10),
                offset: Some(2),
                account_ids: None,
                include_original_description: None,
            }),
        };
        let iter = client.transactions_iter(req);
        pin_mut!(iter);

        let xacts = iter
            .fold(vec![], |mut acc, x| async move {
                acc.append(&mut x.unwrap());
                acc
            })
            .await;
        assert_eq!(xacts.len(), 4);
    }

    #[tokio::test]
    async fn can_read_categories() {
        let client = Builder::new().with_credentials(credentials()).build();
        let res = client.categories(&GetCategoriesRequest {}).await.unwrap();
        insta::assert_json_snapshot!(res.categories);
    }

    #[tokio::test]
    async fn can_refresh_transactions() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();
        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());

        client
            .refresh_transactions(&RefreshTransactionsRequest {
                access_token: res.access_token,
            })
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn can_read_auth() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();
        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());

        let res = client
            .auth(&GetAuthRequest {
                access_token: res.access_token,
                options: None,
            })
            .await
            .unwrap();
        insta::assert_json_snapshot!(res, {
            ".accounts[].account_id" => "[account_id]",
            ".numbers.ach[].account_id" => "[ach_account_id]",
            ".request_id" => "[request_id]",
            ".item.item_id" => "[item_id]",
        });
    }

    #[tokio::test]
    async fn can_read_identity() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();
        let res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!res.access_token.is_empty());

        let res = client
            .identity(&GetIdentityRequest {
                access_token: res.access_token,
                options: None,
            })
            .await
            .unwrap();
        insta::assert_json_snapshot!(res, {
            ".accounts[].account_id" => "[account_id]",
            ".item.item_id" => "[item_id]",
            ".request_id" => "[request_id]",
        });
    }

    #[tokio::test]
    async fn can_invalidate_access_token() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: None,
            })
            .await
            .unwrap();
        let create_res = client.exchange_public_token(public_token).await.unwrap();
        assert!(!create_res.access_token.is_empty());

        let res = client
            .invalidate_access_token(&InvalidateAccessTokenRequest {
                access_token: &create_res.access_token,
            })
            .await
            .unwrap();
        // A new access token should be returned.
        assert_ne!(res.new_access_token, create_res.access_token);
    }

    #[tokio::test]
    async fn can_fire_webhook() {
        let client = Builder::new().with_credentials(credentials()).build();
        let public_token = client
            .create_public_token(CreatePublicTokenRequest {
                institution_id: INSTITUTION_ID,
                initial_products: &["assets", "auth", "balance", "transactions"],
                options: Some(CreatePublicTokenOptions {
                    webhook: Some("localhost:3000"),
                    override_username: None,
                    override_password: None,
                    transactions: None,
                }),
            })
            .await
            .unwrap();
        let res = client.exchange_public_token(public_token).await.unwrap();
        let res = client
            .fire_webhook(&FireWebhookRequest {
                access_token: res.access_token.as_str(),
                webhook_code: WebhookCode::DefaultUpdate,
            })
            .await
            .unwrap();

        assert!(res.webhook_fired);
    }
}