kodik_api/client.rs
1use reqwest::{
2 Client as ReqwestClient, ClientBuilder as ReqwestClientBuilder, Proxy, RequestBuilder,
3};
4
5#[derive(Debug)]
6pub struct ClientBuilder {
7 api_key: Option<String>,
8 api_url: String,
9 reqwest_client_builder: ReqwestClientBuilder,
10}
11
12impl ClientBuilder {
13 /// Constructs a new `ClientBuilder`
14 pub fn new() -> ClientBuilder {
15 ClientBuilder {
16 api_key: None,
17 api_url: "https://kodikapi.com".to_owned(),
18 reqwest_client_builder: ReqwestClientBuilder::new(),
19 }
20 }
21
22 /// API key (token) for Kodik API
23 ///
24 /// ```
25 /// use kodik_api::ClientBuilder;
26 ///
27 /// ClientBuilder::new()
28 /// .api_key("q8p5vnf9crt7xfyzke4iwc6r5rvsurv7");
29 /// ```
30 pub fn api_key(mut self, api_key: impl Into<String>) -> ClientBuilder {
31 self.api_key = Some(api_key.into());
32 self
33 }
34
35 /// Base URL for Kodik API
36 ///
37 /// Default: `https://kodikapi.com`
38 ///
39 /// ```
40 /// use kodik_api::ClientBuilder;
41 ///
42 /// ClientBuilder::new()
43 /// .api_url("https://koooooooooooooodik.com/api");
44 /// ```
45 pub fn api_url(mut self, api_url: impl Into<String>) -> ClientBuilder {
46 self.api_url = api_url.into();
47 self
48 }
49
50 /// ```
51 /// use kodik_api::ClientBuilder;
52 ///
53 /// ClientBuilder::new()
54 /// .proxy(reqwest::Proxy::http("https://my.prox").unwrap());
55 /// ```
56 pub fn proxy(mut self, proxy: Proxy) -> ClientBuilder {
57 self.reqwest_client_builder = self.reqwest_client_builder.proxy(proxy);
58 self
59 }
60
61 /// ```
62 /// use kodik_api::ClientBuilder;
63 ///
64 /// ClientBuilder::new()
65 /// .custom_reqwest_builder(reqwest::ClientBuilder::new());
66 /// ```
67 pub fn custom_reqwest_builder(mut self, builder: ReqwestClientBuilder) -> ClientBuilder {
68 self.reqwest_client_builder = builder;
69 self
70 }
71
72 // TODO: Add handle errors
73 /// # Panic
74 /// If api_key is not set and if it was not possible to build http client
75 ///
76 /// ```
77 /// use kodik_api::ClientBuilder;
78 ///
79 /// ClientBuilder::new().api_key("q8p5vnf9crt7xfyzke4iwc6r5rvsurv7").build();
80 /// ```
81 pub fn build(self) -> Client {
82 Client {
83 api_key: self.api_key.expect("api key is required"),
84 api_url: self.api_url,
85 http_client: self
86 .reqwest_client_builder
87 .build()
88 .expect("failed to build reqwest client"),
89 }
90 }
91}
92
93impl Default for ClientBuilder {
94 fn default() -> Self {
95 Self::new()
96 }
97}
98
99/// The top-level struct of the SDK, representing a client
100#[derive(Debug, Clone)]
101pub struct Client {
102 api_key: String,
103 api_url: String,
104 http_client: ReqwestClient,
105}
106
107impl Client {
108 /// Create a client
109 ///
110 /// # Example
111 ///
112 /// ```
113 /// # use kodik_api::Client;
114 ///
115 /// let api_key = std::env::var("KODIK_API_KEY").expect("KODIK_API_KEY is not set");
116 ///
117 /// let client = Client::new(api_key);
118 /// ```
119 pub fn new(api_key: impl Into<String>) -> Client {
120 ClientBuilder::new().api_key(api_key).build()
121 }
122
123 pub(crate) fn init_post_request(&self, path_or_url: &str) -> RequestBuilder {
124 if !path_or_url.starts_with("http") {
125 self.http_client
126 .post(self.api_url.clone() + path_or_url)
127 .query(&[("token", &self.api_key)])
128 } else {
129 self.http_client.post(path_or_url.to_owned())
130 }
131 }
132}