wspr_cdk/services/
data.rs

1#![allow(non_snake_case)]
2use anyhow::{Context, Error};
3use serde::{Deserialize, Serialize};
4
5#[allow(unused)]
6use crate::{services::prelude::*, state::prelude::WsprSpot};
7
8#[derive(Debug, Deserialize, Serialize)]
9pub struct DataService {
10    query: Option<String>,
11}
12
13impl DataService {
14    // [GET] all "general" records from the [rx] table.
15    pub async fn GET_SPOT_DATA(
16        query: &String,
17        limit: String,
18        result_format: Option<String>,
19    ) -> Result<Vec<WsprSpot>, Error> {
20        let client = reqwest::Client::new();
21        let session = session_manager::SessionManager::new();
22        let BASE_URL = session.BASE_URL.trim();
23
24        let query = query_manager::QueryManager::new(query);
25        let QUERY_STRING = query.QUERY;
26
27        let FORMAT_OPTIONS = match result_format {
28            Some(format) => format,
29            None => String::from(""),
30        };
31
32        let REQUEST = format!(
33            "{}?query={} {} FORMAT {}",
34            BASE_URL, QUERY_STRING, limit, FORMAT_OPTIONS
35        );
36
37        let response = client
38            .get(REQUEST)
39            .send()
40            .await
41            .context("Error sending request!")?;
42
43        //////////////////////////////////////
44        ///// Verify [RESPONSE] status. //////
45        //////////////////////////////////////
46        if response.status().is_success() {
47            let wspr_data = response.text().await.context("Error parsing response!")?;
48
49            let parsed_data: serde_json::Value = match serde_json::from_str(&wspr_data) {
50                Ok(data) => data,
51                Err(e) => {
52                    eprintln!("Error parsing JSON: {:?}", e);
53                    return Err(anyhow::anyhow!("Error parsing JSON: {:?}", e));
54                }
55            };
56
57            //////////////////////////////////////////////////////
58            ///// Retrieve <data> block from <parsed_data>  /////
59            ////////////////////////////////////////////////////
60            let data = &parsed_data["data"];
61
62            ///////////////////////////////
63            ///// Parse <spot> data. //////
64            ///////////////////////////////
65            let spots: Vec<WsprSpot> = match serde_json::from_value(data.clone()) {
66                Ok(spots) => spots,
67                Err(e) => {
68                    eprintln!("Error deserializing WsprSpot data: {:?}", e);
69                    return Err(anyhow::anyhow!(
70                        "Error deserializing WsprSpot data: {:?}",
71                        e
72                    ));
73                }
74            };
75
76            /*****************************
77               [DEBUG] logs.
78
79                dbg!("{}", parsed_data);
80                dbg!("{}", data);
81                dbg!("{}", wspr_data);
82                dbg!("{}", spots);
83
84            *********************************/
85
86            Ok(spots)
87        } else {
88            Err(anyhow::anyhow!(
89                "Request failed with status: {}",
90                response.status()
91            ))
92        }
93    }
94
95    // [GET] record by [Id] from the [rx] table.
96    pub fn GET_SPOT_DATA_BY_ID() {
97        todo!()
98    }
99}