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
//! Requestor part of the Payment API
use chrono::{DateTime, TimeZone};
use std::fmt::Display;
use std::sync::Arc;

use crate::{web::default_on_timeout, web::WebClient, web::WebInterface, Result};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use ya_client_model::payment::*;

#[derive(Default, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct RequestorApiConfig {
    // All timeouts are given in seconds.
    // None is interpreted by server as default timeout (60 seconds).
    pub accept_debit_note_timeout: Option<f64>,
    pub reject_debit_note_timeout: Option<f64>,
    pub accept_invoice_timeout: Option<f64>,
    pub reject_invoice_timeout: Option<f64>,
}

impl RequestorApiConfig {
    pub fn from_env() -> envy::Result<Self> {
        envy::from_env()
    }
}

#[derive(Clone)]
pub struct PaymentRequestorApi {
    client: WebClient,
    config: Arc<RequestorApiConfig>,
}

impl WebInterface for PaymentRequestorApi {
    const API_URL_ENV_VAR: &'static str = crate::payment::PAYMENT_URL_ENV_VAR;
    const API_SUFFIX: &'static str = ya_client_model::payment::PAYMENT_API_PATH;

    fn from_client(client: WebClient) -> Self {
        let config = RequestorApiConfig::default();
        let config = Arc::new(config);
        Self { client, config }
    }
}

impl PaymentRequestorApi {
    pub fn new(client: &WebClient, config: RequestorApiConfig) -> Self {
        let config = config.into();
        let client = client.clone();
        Self { client, config }
    }

    pub async fn get_debit_notes(&self) -> Result<Vec<DebitNote>> {
        self.client.get("requestor/debitNotes").send().json().await
    }

    pub async fn get_debit_note(&self, debit_note_id: &str) -> Result<DebitNote> {
        let url = url_format!("requestor/debitNotes/{debit_note_id}", debit_note_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_payments_for_debit_note(&self, debit_note_id: &str) -> Result<Vec<Payment>> {
        let url = url_format!(
            "requestor/debitNotes/{debit_note_id}/payments",
            debit_note_id
        );
        self.client.get(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn accept_debit_note(
        &self,
        debit_note_id: &str,
        acceptance: &Acceptance,
    ) -> Result<()> {
        let timeout = self.config.accept_debit_note_timeout;
        let url = url_format!(
            "requestor/debitNotes/{debit_note_id}/accept",
            debit_note_id,
            #[query] timeout
        );
        self.client.post(&url).send_json(acceptance).json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn reject_debit_note(
        &self,
        debit_note_id: &str,
        rejection: &Rejection,
    ) -> Result<()> {
        let timeout = self.config.reject_debit_note_timeout;
        let url = url_format!(
            "requestor/debitNotes/{debit_note_id}/reject",
            debit_note_id,
            #[query] timeout
        );
        self.client.post(&url).send_json(rejection).json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_debit_note_events<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<DebitNoteEvent>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "requestor/debitNoteEvents",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await.or_else(default_on_timeout)
    }

    pub async fn get_invoices(&self) -> Result<Vec<Invoice>> {
        self.client.get("requestor/invoices").send().json().await
    }

    pub async fn get_invoice(&self, invoice_id: &str) -> Result<Invoice> {
        let url = url_format!("requestor/invoices/{invoice_id}", invoice_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_payments_for_invoice(&self, invoice_id: &str) -> Result<Vec<Payment>> {
        let url = url_format!("requestor/invoices/{invoice_id}/payments", invoice_id);
        self.client.get(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn accept_invoice(
        &self,
        invoice_id: &str,
        acceptance: &Acceptance,
    ) -> Result<()> {
        let timeout = self.config.accept_invoice_timeout;
        let url = url_format!(
            "requestor/invoices/{invoice_id}/accept",
            invoice_id,
            #[query] timeout
        );
        self.client.post(&url).send_json(acceptance).json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn reject_invoice(&self, invoice_id: &str, rejection: &Rejection) -> Result<()> {
        let timeout = self.config.reject_invoice_timeout;
        let url = url_format!(
            "requestor/invoices/{invoice_id}/reject",
            invoice_id,
            #[query] timeout
        );
        self.client.post(&url).send_json(rejection).json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_invoice_events<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<InvoiceEvent>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "requestor/invoiceEvents",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await.or_else(default_on_timeout)
    }

    pub async fn create_allocation(&self, allocation: &NewAllocation) -> Result<Allocation> {
        self.client
            .post("requestor/allocations")
            .send_json(allocation)
            .json()
            .await
    }

    pub async fn get_allocations(&self) -> Result<Vec<Allocation>> {
        self.client.get("requestor/allocations").send().json().await
    }

    pub async fn get_allocation(&self, allocation_id: &str) -> Result<Allocation> {
        let url = url_format!("requestor/allocations/{allocation_id}", allocation_id);
        self.client.get(&url).send().json().await
    }

    pub async fn amend_allocation(&self, allocation: &Allocation) -> Result<Allocation> {
        let allocation_id = &allocation.allocation_id;
        let url = url_format!("requestor/allocations/{allocation_id}", allocation_id);
        self.client.put(&url).send_json(allocation).json().await
    }

    pub async fn release_allocation(&self, allocation_id: &str) -> Result<()> {
        let url = url_format!("requestor/allocations/{allocation_id}", allocation_id);
        self.client.delete(&url).send().json().await
    }

    #[allow(non_snake_case)]
    #[rustfmt::skip]
    pub async fn get_payments<Tz>(
        &self,
        later_than: Option<&DateTime<Tz>>,
        timeout: Option<Duration>,
    ) -> Result<Vec<Payment>>
    where
        Tz: TimeZone,
        Tz::Offset: Display,
    {
        let laterThan = later_than.map(|dt| dt.to_rfc3339());
        let timeout = timeout.map(|d| d.as_secs_f64());
        let url = url_format!(
            "requestor/payments",
            #[query] laterThan,
            #[query] timeout
        );
        self.client.get(&url).send().json().await.or_else(default_on_timeout)
    }

    pub async fn get_payment(&self, payment_id: &str) -> Result<Payment> {
        let url = url_format!("requestor/payments/{payment_id}", payment_id);
        self.client.get(&url).send().json().await
    }

    pub async fn get_accounts(&self) -> Result<Vec<Account>> {
        self.client.get("requestor/accounts").send().json().await
    }
}