Skip to content

Commit

Permalink
Add coin history request
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinnstah committed Apr 6, 2024
1 parent f3fe789 commit f519403
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
48 changes: 35 additions & 13 deletions crates/crypto-service-uniffi/src/api_client/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::api_client::network_antenna::ExternalClient;
use crate::api_client::network_antenna::FFINetworkingRequest;
use crate::api_client::network_antenna::FFINetworkingResponse;
use crate::coin_watch_service::models::CoinHistoryRequest;
use crate::coin_watch_service::models::CoinMeta;
use crate::coin_watch_service::models::CoinMetaRequest;
use crate::coin_watch_service::models::ListOfCoinsRequest;
Expand Down Expand Up @@ -47,7 +48,7 @@ impl Gateway {
self.network_antenna.get_api_keys().coin_watch,
);
let request = ListOfCoinsRequest::new(limit);

self.post::<_, Vec<CoinMeta>, Vec<CoinMeta>, _, _, _>(
"/coins/list",
request,
Expand All @@ -56,7 +57,7 @@ impl Gateway {
)
.await
}

pub async fn get_coin_meta_info(
&self,
code: String,
Expand All @@ -65,7 +66,7 @@ impl Gateway {
self.network_antenna.get_api_keys().coin_watch,
);
let request = CoinMetaRequest::new(code);

self.post::<_, CoinMeta, CoinMeta, _, _, _>(
"/coins/single",
request,
Expand All @@ -74,6 +75,28 @@ impl Gateway {
)
.await
}

pub async fn get_coin_history_info(
&self,
code: String,
start: u64,
end: u64,
meta: bool,
) -> Result<CoinMeta, FFIBridgeError> {
let external_client = CoinWatchExternalClient::new(
self.network_antenna.get_api_keys().coin_watch,
);
let request =
CoinHistoryRequest::new(code, start, end, meta);

self.post::<_, CoinMeta, CoinMeta, _, _, _>(
"/coins/single/history",
request,
res_id,
external_client,
)
.await
}
}

impl Gateway {
Expand All @@ -97,7 +120,6 @@ impl Gateway {

let json: Json<U> = axum::Json::from_bytes(&body)
.expect("Failed to deserialize");
println!("{:#?}", json.0);

serde_json::from_slice::<U>(&body).map_err(|_| {
RustSideError::UnableJSONDeserializeHTTPResponseBodyIntoTypeName {
Expand All @@ -123,14 +145,14 @@ impl Gateway {
{
// JSON serialize request into body bytes
let body = to_vec(&request).unwrap();

// Append relative path to base url
let url = format!(
"{}{}",
client.get_base_url(),
path.to_owned()
);

// Create Network request object, which will be translated by
// Swift side into a `[Swift]URLRequest`
let request = FFINetworkingRequest {
Expand All @@ -140,16 +162,16 @@ impl Gateway {
headers: client.get_headers(),
};
println!("{:#?}", &request);

// Let Swift side make network request and await response
// let response = self.networking_dispatcher.dispatch(request).await?;
let response = self
.network_antenna
.make_request(request)
.await?;
// Read out HTTP body from response and JSON parse it into U
let model =
self.model_from_response(response).map_err(
.network_antenna
.make_request(request)
.await?;
// Read out HTTP body from response and JSON parse it into U
let model =
self.model_from_response(response).map_err(
|error| FFIBridgeError::FromRust { error },
)?;
// return model
Expand Down
19 changes: 18 additions & 1 deletion crates/crypto-service-uniffi/src/coin_watch_service/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ pub struct CoinHistoryRequest {
meta: bool,
}

impl CoinHistoryRequest {
pub fn new(
code: String,
start: u64,
end: u64,
meta: bool,
) -> Self {
Self {
currency: "USD".into(),
code,
start,
end,
meta,
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Enum)]
#[serde(rename_all = "lowercase")]
pub enum Sort {
Expand All @@ -46,7 +63,6 @@ impl ListOfCoinsRequest {
}
}


#[derive(Debug, Serialize, Deserialize, Record)]
pub struct Coin {
pub code: Option<String>,
Expand Down Expand Up @@ -131,6 +147,7 @@ pub struct CoinMeta {
pub png64: Option<String>,
pub webp64: Option<String>,
#[serde(rename = "allTimeHighUSD")]
pub history: Option<History>,
pub all_time_high_usd: Option<f64>,
pub code: Option<String>,
pub rate: Option<f64>,
Expand Down

0 comments on commit f519403

Please sign in to comment.