1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use reqwest::header::HeaderMap;
use reqwest::{Client, Url};
use crate::errors::ClientCreationError;

pub mod users;
pub mod models;
pub mod errors;
pub mod requests;
pub mod groups;
pub mod actions;

/// A client for interacting with a LuckPerms instance.
pub struct LuckClient {
    base_url: Url,
    client: Client,
}

impl LuckClient {
    /// Create a new LuckClient.
    /// ```rust
    /// use luckperms_rs::LuckClient;
    ///
    /// fn main() {
    ///     let client = LuckClient::try_new("http://localhost:8080".to_string(), "YOUR API KEY".to_string()).unwrap();
    /// }
    pub fn try_new(base_url: String, api_key: String) -> Result<Self, ClientCreationError> {
        let url = Url::parse(&base_url)?;

        let mut headers = HeaderMap::new();
        headers.insert("X-API-KEY", api_key.parse().unwrap());

        let client = reqwest::ClientBuilder::new()
            .default_headers(headers).build()?;

        Ok(LuckClient {
            base_url: url,
            client
        })
    }
}