parent
3cfbf0e5f2
commit
a18c4afdb7
@ -1,2 +1,17 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod routes;
|
||||
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;
|
||||
|
||||
@ -1,109 +1,108 @@
|
||||
use crate::api::types::{
|
||||
paginated_response::PaginatedResponse,
|
||||
transaction::{LatestTransaction, TransactionCompany, TransactionsAggregated},
|
||||
use crate::api::{
|
||||
types::{
|
||||
paginated_response::PaginatedResponse,
|
||||
transaction::{LatestTransaction, TransactionCompany, TransactionsAggregated},
|
||||
},
|
||||
FastInsidersApi,
|
||||
};
|
||||
|
||||
pub async fn get_transactions(
|
||||
company_slug: Option<String>,
|
||||
page: i64,
|
||||
size: i64,
|
||||
) -> Result<PaginatedResponse<TransactionCompany>, ()> {
|
||||
use crate::env::Config;
|
||||
impl FastInsidersApi {
|
||||
pub async fn get_transactions(
|
||||
&self,
|
||||
company_slug: Option<String>,
|
||||
page: i64,
|
||||
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
|
||||
let api_url = Config::new().api_url;
|
||||
let route = &format!(
|
||||
"{}transaction?{}&page={}&size={}",
|
||||
api_url,
|
||||
company_slug.map_or("".to_string(), |c| format!("company_slug={}", c)),
|
||||
page,
|
||||
size,
|
||||
);
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionCompany>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionCompany>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionCompany>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionCompany>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
pub async fn get_aggregated_transactions(
|
||||
hours: Option<String>,
|
||||
page: i64,
|
||||
size: i64,
|
||||
) -> Result<PaginatedResponse<TransactionsAggregated>, ()> {
|
||||
use crate::env::Config;
|
||||
|
||||
// TODO: Remove build-time environment variable
|
||||
let api_url = Config::new().api_url;
|
||||
let route = &format!(
|
||||
"{}transaction/aggregated?{}&page={}&size={}",
|
||||
api_url,
|
||||
hours.map_or("".to_string(), |c| format!("hours={}", c)),
|
||||
page,
|
||||
size,
|
||||
);
|
||||
pub async fn get_aggregated_transactions(
|
||||
&self,
|
||||
hours: Option<String>,
|
||||
page: i64,
|
||||
size: i64,
|
||||
) -> Result<PaginatedResponse<TransactionsAggregated>, ()> {
|
||||
let route = &format!(
|
||||
"{}/transaction/aggregated?{}&page={}&size={}",
|
||||
self.url,
|
||||
hours.map_or("".to_string(), |c| format!("hours={}", c)),
|
||||
page,
|
||||
size,
|
||||
);
|
||||
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
return Ok(res);
|
||||
}
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<TransactionsAggregated>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
pub async fn get_latest_transactions(
|
||||
_: Option<String>,
|
||||
page: i64,
|
||||
size: i64,
|
||||
) -> Result<PaginatedResponse<LatestTransaction>, ()> {
|
||||
use crate::env::Config;
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
// TODO: Remove build-time environment variable
|
||||
let api_url = Config::new().api_url;
|
||||
let route = &format!("{}transaction/latest?page={}&size={}", api_url, page, size,);
|
||||
pub async fn get_latest_transactions(
|
||||
&self,
|
||||
_: Option<String>,
|
||||
page: i64,
|
||||
size: i64,
|
||||
) -> Result<PaginatedResponse<LatestTransaction>, ()> {
|
||||
let route = &format!(
|
||||
"{}/transaction/latest?page={}&size={}",
|
||||
self.url, page, size,
|
||||
);
|
||||
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<LatestTransaction>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
#[cfg(client)]
|
||||
let res = reqwasm::http::Request::get(route)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<LatestTransaction>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<LatestTransaction>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
#[cfg(engine)]
|
||||
let res = reqwest::get(route)
|
||||
.await
|
||||
.map_err(|_| ())?
|
||||
.json::<PaginatedResponse<LatestTransaction>>()
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
return Ok(res);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,26 +1,49 @@
|
||||
use perseus::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::env::Config;
|
||||
use crate::api::FastInsidersApi;
|
||||
use perseus::state::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)]
|
||||
#[rx(alias = "AppStateRx")]
|
||||
pub struct AppState {
|
||||
pub dark_mode: bool,
|
||||
pub config: Config,
|
||||
pub api: FastInsidersApi,
|
||||
}
|
||||
|
||||
#[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;
|
||||
let config = Config::new();
|
||||
AppState {
|
||||
config,
|
||||
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