parent
3cfbf0e5f2
commit
a18c4afdb7
@ -1,2 +1,17 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
|
pub struct FastInsidersApi {
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FastInsidersApi {
|
||||||
|
pub fn new(url: &str) -> Self {
|
||||||
|
FastInsidersApi {
|
||||||
|
url: url.to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
|
pub mod company;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
|
|||||||
@ -1,109 +1,108 @@
|
|||||||
use crate::api::types::{
|
use crate::api::{
|
||||||
paginated_response::PaginatedResponse,
|
types::{
|
||||||
transaction::{LatestTransaction, TransactionCompany, TransactionsAggregated},
|
paginated_response::PaginatedResponse,
|
||||||
|
transaction::{LatestTransaction, TransactionCompany, TransactionsAggregated},
|
||||||
|
},
|
||||||
|
FastInsidersApi,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn get_transactions(
|
impl FastInsidersApi {
|
||||||
company_slug: Option<String>,
|
pub async fn get_transactions(
|
||||||
page: i64,
|
&self,
|
||||||
size: i64,
|
company_slug: Option<String>,
|
||||||
) -> Result<PaginatedResponse<TransactionCompany>, ()> {
|
page: i64,
|
||||||
use crate::env::Config;
|
size: i64,
|
||||||
|
) -> Result<PaginatedResponse<TransactionCompany>, ()> {
|
||||||
|
let route = &format!(
|
||||||
|
"{}/transaction?{}&page={}&size={}",
|
||||||
|
self.url,
|
||||||
|
company_slug.map_or("".to_string(), |c| format!("company_slug={}", c)),
|
||||||
|
page,
|
||||||
|
size,
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: Remove build-time environment variable
|
#[cfg(client)]
|
||||||
let api_url = Config::new().api_url;
|
let res = reqwasm::http::Request::get(route)
|
||||||
let route = &format!(
|
.send()
|
||||||
"{}transaction?{}&page={}&size={}",
|
.await
|
||||||
api_url,
|
.map_err(|_| ())?
|
||||||
company_slug.map_or("".to_string(), |c| format!("company_slug={}", c)),
|
.json::<PaginatedResponse<TransactionCompany>>()
|
||||||
page,
|
.await
|
||||||
size,
|
.map_err(|_| ())?;
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(client)]
|
#[cfg(engine)]
|
||||||
let res = reqwasm::http::Request::get(route)
|
let res = reqwest::get(route)
|
||||||
.send()
|
.await
|
||||||
.await
|
.map_err(|_| ())?
|
||||||
.map_err(|_| ())?
|
.json::<PaginatedResponse<TransactionCompany>>()
|
||||||
.json::<PaginatedResponse<TransactionCompany>>()
|
.await
|
||||||
.await
|
.map_err(|_| ())?;
|
||||||
.map_err(|_| ())?;
|
|
||||||
|
|
||||||
#[cfg(engine)]
|
return Ok(res);
|
||||||
let res = reqwest::get(route)
|
}
|
||||||
.await
|
|
||||||
.map_err(|_| ())?
|
|
||||||
.json::<PaginatedResponse<TransactionCompany>>()
|
|
||||||
.await
|
|
||||||
.map_err(|_| ())?;
|
|
||||||
|
|
||||||
return Ok(res);
|
pub async fn get_aggregated_transactions(
|
||||||
}
|
&self,
|
||||||
|
hours: Option<String>,
|
||||||
pub async fn get_aggregated_transactions(
|
page: i64,
|
||||||
hours: Option<String>,
|
size: i64,
|
||||||
page: i64,
|
) -> Result<PaginatedResponse<TransactionsAggregated>, ()> {
|
||||||
size: i64,
|
let route = &format!(
|
||||||
) -> Result<PaginatedResponse<TransactionsAggregated>, ()> {
|
"{}/transaction/aggregated?{}&page={}&size={}",
|
||||||
use crate::env::Config;
|
self.url,
|
||||||
|
hours.map_or("".to_string(), |c| format!("hours={}", c)),
|
||||||
// TODO: Remove build-time environment variable
|
page,
|
||||||
let api_url = Config::new().api_url;
|
size,
|
||||||
let route = &format!(
|
);
|
||||||
"{}transaction/aggregated?{}&page={}&size={}",
|
|
||||||
api_url,
|
|
||||||
hours.map_or("".to_string(), |c| format!("hours={}", c)),
|
|
||||||
page,
|
|
||||||
size,
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
let res = reqwasm::http::Request::get(route)
|
let res = reqwasm::http::Request::get(route)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?
|
.map_err(|_| ())?
|
||||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
|
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
let res = reqwest::get(route)
|
let res = reqwest::get(route)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?
|
.map_err(|_| ())?
|
||||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
|
|
||||||
return Ok(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_latest_transactions(
|
return Ok(res);
|
||||||
_: Option<String>,
|
}
|
||||||
page: i64,
|
|
||||||
size: i64,
|
|
||||||
) -> Result<PaginatedResponse<LatestTransaction>, ()> {
|
|
||||||
use crate::env::Config;
|
|
||||||
|
|
||||||
// TODO: Remove build-time environment variable
|
pub async fn get_latest_transactions(
|
||||||
let api_url = Config::new().api_url;
|
&self,
|
||||||
let route = &format!("{}transaction/latest?page={}&size={}", api_url, page, size,);
|
_: Option<String>,
|
||||||
|
page: i64,
|
||||||
|
size: i64,
|
||||||
|
) -> Result<PaginatedResponse<LatestTransaction>, ()> {
|
||||||
|
let route = &format!(
|
||||||
|
"{}/transaction/latest?page={}&size={}",
|
||||||
|
self.url, page, size,
|
||||||
|
);
|
||||||
|
|
||||||
#[cfg(client)]
|
#[cfg(client)]
|
||||||
let res = reqwasm::http::Request::get(route)
|
let res = reqwasm::http::Request::get(route)
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?
|
.map_err(|_| ())?
|
||||||
.json::<PaginatedResponse<LatestTransaction>>()
|
.json::<PaginatedResponse<LatestTransaction>>()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
|
|
||||||
#[cfg(engine)]
|
#[cfg(engine)]
|
||||||
let res = reqwest::get(route)
|
let res = reqwest::get(route)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?
|
.map_err(|_| ())?
|
||||||
.json::<PaginatedResponse<LatestTransaction>>()
|
.json::<PaginatedResponse<LatestTransaction>>()
|
||||||
.await
|
.await
|
||||||
.map_err(|_| ())?;
|
.map_err(|_| ())?;
|
||||||
|
|
||||||
return Ok(res);
|
return Ok(res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,26 +1,49 @@
|
|||||||
use perseus::prelude::*;
|
use perseus::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::env::Config;
|
use crate::api::FastInsidersApi;
|
||||||
use perseus::state::GlobalStateCreator;
|
use perseus::state::GlobalStateCreator;
|
||||||
|
|
||||||
pub fn get_global_state_creator() -> GlobalStateCreator {
|
pub fn get_global_state_creator() -> GlobalStateCreator {
|
||||||
GlobalStateCreator::new().build_state_fn(get_build_state)
|
GlobalStateCreator::new()
|
||||||
|
.build_state_fn(get_build_state)
|
||||||
|
.request_state_fn(get_request_state)
|
||||||
|
.amalgamate_states_fn(amalgamate_states)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, ReactiveState)]
|
#[derive(Serialize, Deserialize, ReactiveState)]
|
||||||
#[rx(alias = "AppStateRx")]
|
#[rx(alias = "AppStateRx")]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub dark_mode: bool,
|
pub dark_mode: bool,
|
||||||
pub config: Config,
|
pub api: FastInsidersApi,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[engine_only_fn]
|
#[engine_only_fn]
|
||||||
pub async fn get_build_state(_locale: String) -> AppState {
|
pub async fn get_build_state() -> AppState {
|
||||||
|
AppState {
|
||||||
|
dark_mode: true,
|
||||||
|
api: FastInsidersApi::new(""), // It's unfortunately not possible to have a different type
|
||||||
|
// for the build state and the request state, I would rather
|
||||||
|
// have left this out
|
||||||
|
// This will also only ever work as long as we don't need the api
|
||||||
|
// while building the app, so no SSR!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
async fn get_request_state(_req: Request) -> AppState {
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
let config = Config::new();
|
let config = Config::new();
|
||||||
AppState {
|
AppState {
|
||||||
config,
|
|
||||||
dark_mode: true,
|
dark_mode: true,
|
||||||
|
api: FastInsidersApi::new(&config.api_url),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[engine_only_fn]
|
||||||
|
async fn amalgamate_states(build_state: AppState, request_state: AppState) -> AppState {
|
||||||
|
AppState {
|
||||||
|
dark_mode: build_state.dark_mode,
|
||||||
|
api: request_state.api,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue