osrs_wiki_prices/
lib.rs

1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3
4pub mod endpoints;
5pub mod types;
6#[cfg(test)]
7pub mod testing;
8
9const BASE_URL: &str = "prices.runescape.wiki/api/v1";
10
11pub struct Client {
12    http_client: reqwest::Client,
13    base_url: String,
14}
15
16#[derive(Debug, thiserror::Error)]
17pub enum ClientNewError {
18    #[error(transparent)]
19    ReqwestError(#[from] reqwest::Error),
20}
21
22impl Client {
23    pub fn try_new(user_agent: Cow<str>, api_endpoint: ApiEndpoint) -> Result<Self, ClientNewError> {
24        let http_client = reqwest::Client::builder()
25            .user_agent(user_agent.as_ref())
26            .build()?;
27        let base_url = format!("https://{}/{}", BASE_URL, api_endpoint);
28        Ok(Self { http_client, base_url })
29    }
30}
31
32#[derive(Debug, Clone, Copy)]
33#[derive(PartialEq)]
34pub enum ApiEndpoint {
35    OldSchoolRuneScape,
36    DeadmanArmageddon,
37}
38
39impl Display for ApiEndpoint {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        match self {
42            ApiEndpoint::OldSchoolRuneScape => write!(f, "osrs"),
43            ApiEndpoint::DeadmanArmageddon => write!(f, "dmm"),
44        }
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use std::borrow::Cow;
52
53    #[test]
54    fn test_client_new() {
55        let user_agent = Cow::Borrowed("test_user_agent");
56        let api_endpoint = ApiEndpoint::OldSchoolRuneScape;
57        let client = Client::try_new(user_agent, api_endpoint);
58        assert!(client.is_ok());
59        let client = client.unwrap();
60        assert_eq!(client.base_url, format!("https://{}/{}", BASE_URL, api_endpoint));
61    }
62}