Skip to content

Commit a357020

Browse files
authored
Feature/stats endpoint (#8)
* add blocking df-client * add PartialEq trait for DatafeedController * add stats endpoint
1 parent eb303be commit a357020

File tree

14 files changed

+763
-286
lines changed

14 files changed

+763
-286
lines changed

Cargo.lock

Lines changed: 491 additions & 180 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[workspace]
2+
resolver = "3"
23
members = [
34
"datafeed-cache-client",
45
"datafeed-cache-server",

datafeed-cache-client/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
[package]
22
name = "datafeed-cache-client"
33
edition = "2024"
4-
version = "1.3.2"
4+
version = "1.3.3"
55

66
[dependencies]
7-
reqwest = { version = "0.12.15", features = ["json"] }
7+
reqwest = { version = "0.13.1", features = ["json"] }
88
serde = { version = "1.0.219", features = ["derive"] }
99
dotenv = { version = "0.15.0" }
1010
datafeed-cache-shared = { path = "../datafeed-cache-shared" }
1111
env_logger = "0.11.7"
1212
log = "0.4.27"
1313

14+
[features]
15+
blocking = ["reqwest/blocking"]
16+
1417
[lib]
1518
name = "datafeed_cache_client"
1619
path = "src/lib.rs"

datafeed-cache-client/src/client.rs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,74 @@ use datafeed_cache_shared::datafeed::{
22
DatafeedAtis, DatafeedController, DatafeedMilitaryRating, DatafeedPilot, DatafeedPilotRating,
33
DatafeedServer,
44
};
5-
use datafeed_cache_shared::response::{
6-
DatafeedGeneralResponse, DatafeedListResponse, DatafeedResponse,
7-
};
8-
use log::info;
9-
use reqwest::Client;
5+
use datafeed_cache_shared::response::{DatafeedGeneralResponse, DatafeedGerStatsResponse, DatafeedListResponse, DatafeedResponse};
106
use serde::de::DeserializeOwned;
11-
12-
pub struct DatafeedClient {
13-
client: Client,
14-
base_url: String,
15-
}
16-
17-
const BASE_DEFAULT: &'static str = "https://df.vatsim-germany.org";
7+
use crate::DatafeedClient;
188

199
type Error = reqwest::Error;
2010

21-
#[allow(dead_code)]
2211
impl DatafeedClient {
23-
pub fn new() -> Self {
24-
let _ = env_logger::try_init();
25-
let _ = dotenv::dotenv();
26-
27-
let base_url: String = dotenv::var("BASE_URL").unwrap_or(BASE_DEFAULT.to_string());
28-
info!("Selected BASE_URL: {}", base_url);
2912

30-
DatafeedClient {
31-
client: Client::default(),
32-
base_url,
33-
}
34-
}
35-
36-
async fn make_request<T>(&self, path: &str) -> Result<T, Error>
13+
fn make_request<T>(&self, path: &str) -> Result<T, Error>
3714
where
3815
T: DeserializeOwned,
3916
{
4017
let req = self.client.get(self.base_url.to_owned() + path).build()?;
41-
let response = self.client.execute(req).await?;
42-
response.json::<T>().await
18+
let response = self.client.execute(req)?;
19+
response.json::<T>()
4320
}
4421

45-
pub async fn get(&self) -> Result<DatafeedResponse, Error> {
46-
self.make_request("/datafeed").await
22+
pub fn get(&self) -> Result<DatafeedResponse<'_>, Error> {
23+
self.make_request("/datafeed")
4724
}
4825

49-
pub async fn get_general(&self) -> Result<DatafeedGeneralResponse, Error> {
50-
self.make_request("/datafeed/general").await
26+
pub fn get_general(&self) -> Result<DatafeedGeneralResponse<'_>, Error> {
27+
self.make_request("/datafeed/general")
5128
}
5229

53-
pub async fn get_controllers(&self) -> Result<DatafeedListResponse<DatafeedController>, Error> {
54-
self.make_request("/datafeed/controllers").await
30+
pub fn get_controllers(&self) -> Result<DatafeedListResponse<'_, DatafeedController>, Error> {
31+
self.make_request("/datafeed/controllers")
5532
}
5633

57-
pub async fn get_pilots(&self) -> Result<DatafeedListResponse<DatafeedPilot>, Error> {
58-
self.make_request("/datafeed/pilots").await
34+
pub fn get_pilots(&self) -> Result<DatafeedListResponse<'_, DatafeedPilot>, Error> {
35+
self.make_request("/datafeed/pilots")
5936
}
6037

61-
pub async fn get_atis(&self) -> Result<DatafeedListResponse<DatafeedAtis>, Error> {
62-
self.make_request("/datafeed/atis").await
38+
pub fn get_atis(&self) -> Result<DatafeedListResponse<'_, DatafeedAtis>, Error> {
39+
self.make_request("/datafeed/atis")
6340
}
6441

65-
pub async fn get_servers(&self) -> Result<DatafeedListResponse<DatafeedServer>, Error> {
66-
self.make_request("/datafeed/servers").await
42+
pub fn get_servers(&self) -> Result<DatafeedListResponse<'_, DatafeedServer>, Error> {
43+
self.make_request("/datafeed/servers")
6744
}
6845

69-
pub async fn get_pilot_ratings(
46+
pub fn get_pilot_ratings(
7047
&self,
71-
) -> Result<DatafeedListResponse<DatafeedPilotRating>, Error> {
72-
self.make_request("/datafeed/pilot_ratings").await
48+
) -> Result<DatafeedListResponse<'_, DatafeedPilotRating>, Error> {
49+
self.make_request("/datafeed/pilot_ratings")
7350
}
7451

75-
pub async fn get_military_ratings(
52+
pub fn get_military_ratings(
7653
&self,
77-
) -> Result<DatafeedListResponse<DatafeedMilitaryRating>, Error> {
78-
self.make_request("/datafeed/military_ratings").await
54+
) -> Result<DatafeedListResponse<'_, DatafeedMilitaryRating>, Error> {
55+
self.make_request("/datafeed/military_ratings")
7956
}
8057

81-
pub async fn get_ger_controllers(
58+
pub fn get_ger_controllers(
8259
&self,
83-
) -> Result<DatafeedListResponse<DatafeedController>, Error> {
84-
self.make_request("/datafeed/controllers/ger").await
60+
) -> Result<DatafeedListResponse<'_, DatafeedController>, Error> {
61+
self.make_request("/datafeed/controllers/ger")
62+
}
63+
64+
pub fn get_ger_pilots(&self) -> Result<DatafeedListResponse<'_, DatafeedPilot>, Error> {
65+
self.make_request("/datafeed/pilots/ger")
8566
}
8667

87-
pub async fn get_ger_pilots(&self) -> Result<DatafeedListResponse<DatafeedPilot>, Error> {
88-
self.make_request("/datafeed/pilots/ger").await
68+
pub fn get_ger_atis(&self) -> Result<DatafeedListResponse<'_, DatafeedAtis>, Error> {
69+
self.make_request("/datafeed/atis/ger")
8970
}
9071

91-
pub async fn get_ger_atis(&self) -> Result<DatafeedListResponse<DatafeedAtis>, Error> {
92-
self.make_request("/datafeed/atis/ger").await
72+
pub fn get_ger_stats(&self) -> Result<DatafeedGerStatsResponse, Error> {
73+
self.make_request("/datafeed/stats")
9374
}
9475
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use datafeed_cache_shared::datafeed::{
2+
DatafeedAtis, DatafeedController, DatafeedMilitaryRating, DatafeedPilot, DatafeedPilotRating,
3+
DatafeedServer,
4+
};
5+
use datafeed_cache_shared::response::{DatafeedGeneralResponse, DatafeedGerStatsResponse, DatafeedListResponse, DatafeedResponse};
6+
use serde::de::DeserializeOwned;
7+
use crate::DatafeedClient;
8+
9+
type Error = reqwest::Error;
10+
11+
impl DatafeedClient {
12+
async fn make_request<T>(&self, path: &str) -> Result<T, Error>
13+
where
14+
T: DeserializeOwned,
15+
{
16+
let req = self.client.get(self.base_url.to_owned() + path).build()?;
17+
let response = self.client.execute(req).await?;
18+
response.json::<T>().await
19+
}
20+
21+
pub async fn get(&self) -> Result<DatafeedResponse<'_>, Error> {
22+
self.make_request("/datafeed").await
23+
}
24+
25+
pub async fn get_general(&self) -> Result<DatafeedGeneralResponse<'_>, Error> {
26+
self.make_request("/datafeed/general").await
27+
}
28+
29+
pub async fn get_controllers(&self) -> Result<DatafeedListResponse<'_, DatafeedController>, Error> {
30+
self.make_request("/datafeed/controllers").await
31+
}
32+
33+
pub async fn get_pilots(&self) -> Result<DatafeedListResponse<'_, DatafeedPilot>, Error> {
34+
self.make_request("/datafeed/pilots").await
35+
}
36+
37+
pub async fn get_atis(&self) -> Result<DatafeedListResponse<'_,DatafeedAtis>, Error> {
38+
self.make_request("/datafeed/atis").await
39+
}
40+
41+
pub async fn get_servers(&self) -> Result<DatafeedListResponse<'_,DatafeedServer>, Error> {
42+
self.make_request("/datafeed/servers").await
43+
}
44+
45+
pub async fn get_pilot_ratings(
46+
&self,
47+
) -> Result<DatafeedListResponse<'_,DatafeedPilotRating>, Error> {
48+
self.make_request("/datafeed/pilot_ratings").await
49+
}
50+
51+
pub async fn get_military_ratings(
52+
&self,
53+
) -> Result<DatafeedListResponse<'_,DatafeedMilitaryRating>, Error> {
54+
self.make_request("/datafeed/military_ratings").await
55+
}
56+
57+
pub async fn get_ger_controllers(
58+
&self,
59+
) -> Result<DatafeedListResponse<'_,DatafeedController>, Error> {
60+
self.make_request("/datafeed/controllers/ger").await
61+
}
62+
63+
pub async fn get_ger_pilots(&self) -> Result<DatafeedListResponse<'_,DatafeedPilot>, Error> {
64+
self.make_request("/datafeed/pilots/ger").await
65+
}
66+
67+
pub async fn get_ger_atis(&self) -> Result<DatafeedListResponse<'_,DatafeedAtis>, Error> {
68+
self.make_request("/datafeed/atis/ger").await
69+
}
70+
71+
pub async fn get_ger_stats(&self) -> Result<DatafeedGerStatsResponse, Error> {
72+
self.make_request("/datafeed/stats").await
73+
}
74+
}

datafeed-cache-client/src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
1+
use log::info;
2+
3+
#[cfg(feature = "blocking")]
14
pub mod client;
25

6+
#[cfg(feature = "blocking")]
7+
use reqwest::blocking::Client;
8+
9+
#[cfg(not(feature = "blocking"))]
10+
pub mod client_async;
11+
12+
#[cfg(not(feature = "blocking"))]
13+
use reqwest::Client;
14+
315
pub mod shared {
416
pub use datafeed_cache_shared::*;
517
}
618

7-
// TODO: Add Tests here which force the functions to work, even if the datafeed-cache has been
8-
// TODO: modified
19+
pub struct DatafeedClient {
20+
client: Client,
21+
base_url: String,
22+
}
23+
24+
const BASE_DEFAULT: &'static str = "https://df.vatsim-germany.org";
25+
26+
impl DatafeedClient {
27+
pub fn new() -> Self {
28+
let _ = env_logger::try_init();
29+
let _ = dotenv::dotenv();
30+
31+
let base_url: String = dotenv::var("BASE_URL").unwrap_or(BASE_DEFAULT.to_string());
32+
info!("Selected BASE_URL: {}", base_url);
33+
34+
DatafeedClient {
35+
client: Client::default(),
36+
base_url,
37+
}
38+
}
39+
}

datafeed-cache-server/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
[package]
22
name = "datafeed-cache-server"
33
edition = "2024"
4-
version = "1.3.2"
4+
version = "1.3.3"
55

66
[dependencies]
77
tokio = { version = "1.44.1", features = ["macros", "rt-multi-thread", "time"] }
88
actix-web = { version = "4.10.2" }
9-
reqwest = { version = "0.12.15", features = ["json"] }
9+
reqwest = { version = "0.13.1", features = ["json"] }
1010
serde = { version = "1.0.219", features = ["derive"] }
1111
rand = "0.9.0"
1212
env_logger = "0.11.7"
1313
log = "0.4.27"
14-
geo = { version = "0.31.0" }
14+
geo = { version = "0.32.0" }
1515
chrono = { version = "0.4.40", features = ["serde"] }
1616
datafeed-cache-shared = { path = "../datafeed-cache-shared" }
17+
once_cell = "1.21.3"
1718

1819
[[bin]]
1920
name = "datafeed_cache_server"

0 commit comments

Comments
 (0)