avanza_rs/position/
total_values.rs1use crate::client::Client;
2use serde::{Deserialize, Serialize};
3
4use crate::{account::Account, char_encode::char_encode, error::Error};
5
6#[derive(Debug, Serialize)]
7struct TotalValuesRequest(Vec<String>);
8
9#[derive(Debug, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct TotalValuesResponse {
12 #[serde(rename(serialize = "total_value"))]
13 pub aggegated: Aggegated,
14}
15
16#[derive(Debug, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct Aggegated {
19 pub total_value: TotalValue,
20}
21
22#[derive(Debug, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct TotalValue {
25 pub value: f64,
26}
27
28impl Client {
29 fn encode_account_ids(accounts: &Vec<Account>) -> Result<Vec<String>, Error> {
30 let encoded_account_ids: Result<Vec<_>, _> = accounts
31 .iter()
32 .map(|acc| {
33 return acc
34 .id
35 .parse::<u32>()
36 .map_err(|err| Error::AccountIdParseError(String::from("asd"), err))
37 .map(char_encode);
38 })
39 .collect();
40 return encoded_account_ids;
41 }
42
43 pub async fn get_total_values(
44 &self,
45 accounts: &Vec<Account>,
46 ) -> Result<TotalValuesResponse, Error> {
47 let encoded_account_ids = Self::encode_account_ids(accounts)?;
48
49 let request_body = TotalValuesRequest(encoded_account_ids);
50
51 let res = self
52 .http_client
53 .post(&self.config.urls.total_values)
54 .body_json(&request_body)?
55 .recv_json::<TotalValuesResponse>()
56 .await?;
57
58 return Ok(res);
59 }
60}