Skip to content

Commit 80b559a

Browse files
authored
Merge pull request #14 from habibi-dev/develop
💱 feat(api): Add currency exchange & crypto price endpoints
2 parents 06b009b + 0a957ea commit 80b559a

28 files changed

Lines changed: 340 additions & 135 deletions

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust_rest_api"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
edition = "2024"
55
license = "MIT"
66
authors = ["Habibi-Dev"]

src/app.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core::config::Config;
2-
use crate::jobs::{cache_flush::CacheFlushJob, coin_sync::CoinSyncJob};
2+
use crate::jobs::cache_flush::CacheFlushJob;
3+
use crate::jobs::tgju_sync::TgjuSyncJob;
34
use crate::server;
45
use crate::services::{cache::StatsCache, routes::Routes};
56
use crate::state::{self, APP_STATE, AppState};
@@ -48,9 +49,9 @@ async fn setup_database() -> Result<sea_orm::DatabaseConnection> {
4849

4950
fn start_background_jobs(cache: &Arc<StatsCache>) -> Result<()> {
5051
let flush_job = CacheFlushJob::new(Arc::clone(cache));
51-
let coin_job = CoinSyncJob::new();
52+
let tgju_job = TgjuSyncJob::new();
5253

53-
crate::services::jobs::FlushJob::start(vec![flush_job.into_task(), coin_job.into_task()], None);
54+
crate::services::jobs::FlushJob::start(vec![flush_job.into_task(), tgju_job.into_task()], None);
5455

5556
Ok(())
5657
}

src/jobs/coin_sync.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/jobs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod cache_flush;
2-
pub mod coin_sync;
2+
pub mod tgju_sync;
33

44
use std::{future::Future, pin::Pin, sync::Arc};
55

src/jobs/tgju_sync.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::jobs::JobTask;
2+
use crate::request::request_handler;
3+
use crate::state::APP_STATE;
4+
use std::sync::Arc;
5+
6+
pub struct TgjuSyncJob;
7+
8+
impl TgjuSyncJob {
9+
pub fn new() -> Self {
10+
Self
11+
}
12+
13+
pub fn into_task(self) -> JobTask {
14+
Arc::new(|| {
15+
Box::pin(async {
16+
if let Err(e) = tgju_sync().await {
17+
eprintln!("Gold sync job failed: {}", e);
18+
}
19+
})
20+
})
21+
}
22+
}
23+
24+
async fn tgju_sync() -> Result<(), Box<dyn std::error::Error>> {
25+
let service = request_handler::RequestHandler::new();
26+
let tg = service.tgju_typed().await?;
27+
28+
let state = APP_STATE.get().ok_or("Application state not available")?;
29+
30+
state
31+
.financial_rates_tx
32+
.send(tg)
33+
.map_err(|_| "Failed to send golds to channel")?;
34+
35+
Ok(())
36+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod entities;
44
mod jobs;
55
mod middleware;
66
mod repository;
7+
mod request;
78
mod server;
89
mod services;
910
mod state;

src/request/data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/request/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod data;
2+
pub mod request_handler;
3+
pub mod tgju_data;
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
use crate::services::coin_ir::data::{ApiResponse, CoinData};
1+
use crate::request::tgju_data::{ApiResponse, TgjuData};
22
use reqwest::Client;
33
use uuid::Uuid;
44

5-
pub struct RequestCoinIr {
5+
pub struct RequestHandler {
66
client: Client,
77
}
88

9-
impl RequestCoinIr {
9+
impl RequestHandler {
1010
pub fn new() -> Self {
1111
let client = Client::builder()
1212
.use_rustls_tls()
1313
.timeout(std::time::Duration::from_secs(10))
1414
.build()
15-
.expect("reqwest client build failed");
15+
.expect("reqwest client [RequestGold] build failed");
1616
Self { client }
1717
}
1818

1919
pub async fn start(&self) -> Result<ApiResponse, reqwest::Error> {
2020
let uuid = Uuid::new_v4();
21-
let url = format!("https://call1.tgju.org/ajax.json?rev={}", uuid);
21+
let url = format!("https://call3.tgju.org/ajax.json?rev={}", uuid);
2222
let resp = self
2323
.client
2424
.get(url)
@@ -31,8 +31,8 @@ impl RequestCoinIr {
3131
Ok(api)
3232
}
3333

34-
pub async fn coins_typed(&self) -> Result<CoinData, reqwest::Error> {
34+
pub async fn tgju_typed(&self) -> Result<TgjuData, reqwest::Error> {
3535
let api = self.start().await?;
36-
Ok(CoinData::from(&api.current))
36+
Ok(TgjuData::from(&api.current))
3737
}
3838
}

0 commit comments

Comments
 (0)