harvest_api/request/
create_invoice_payment.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct CreateInvoicePaymentRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub invoice_id: String,
10 pub amount: Option<f64>,
11 pub paid_at: Option<String>,
12 pub paid_date: Option<String>,
13 pub notes: Option<String>,
14}
15impl<'a> CreateInvoicePaymentRequest<'a> {
16 pub async fn send(self) -> anyhow::Result<InvoicePayment> {
17 let mut r = self
18 .client
19 .client
20 .post(
21 &format!("/invoices/{invoice_id}/payments", invoice_id = self.invoice_id),
22 );
23 if let Some(ref unwrapped) = self.amount {
24 r = r.push_json(json!({ "amount" : unwrapped }));
25 }
26 if let Some(ref unwrapped) = self.paid_at {
27 r = r.push_json(json!({ "paid_at" : unwrapped }));
28 }
29 if let Some(ref unwrapped) = self.paid_date {
30 r = r.push_json(json!({ "paid_date" : unwrapped }));
31 }
32 if let Some(ref unwrapped) = self.notes {
33 r = r.push_json(json!({ "notes" : unwrapped }));
34 }
35 r = self.client.authenticate(r);
36 let res = r.send().await.unwrap().error_for_status();
37 match res {
38 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
39 Err(res) => {
40 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
41 Err(anyhow::anyhow!("{:?}", text))
42 }
43 }
44 }
45 pub fn amount(mut self, amount: f64) -> Self {
46 self.amount = Some(amount);
47 self
48 }
49 pub fn paid_at(mut self, paid_at: &str) -> Self {
50 self.paid_at = Some(paid_at.to_owned());
51 self
52 }
53 pub fn paid_date(mut self, paid_date: &str) -> Self {
54 self.paid_date = Some(paid_date.to_owned());
55 self
56 }
57 pub fn notes(mut self, notes: &str) -> Self {
58 self.notes = Some(notes.to_owned());
59 self
60 }
61}