-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
175 lines (153 loc) · 5.37 KB
/
lib.rs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::clients::group_client::GroupClient;
use crate::clients::player_client::PlayerClient;
use reqwest::header::{HeaderMap, HeaderValue};
/// Individual clients for each endpoint
pub mod clients;
/// Responses for each endpoint
pub mod models;
const BASE_URL: &str = "https://api.wiseoldman.net/v2";
static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
"/",
"GitHub:https://github.com/fatfingers23/wom_rs"
);
///Wom endpoints
pub(crate) enum ApiEndpoint {
Player,
Group,
}
impl ApiEndpoint {
pub fn as_str(&self) -> &str {
match self {
ApiEndpoint::Player => "/players",
ApiEndpoint::Group => "/groups",
}
}
}
/// Wise Old Man Client
pub struct WomClient {
pub player_client: PlayerClient,
pub group_client: GroupClient,
}
/// Used for endpoints that take pagination
pub struct Pagination {
pub limit: Option<i32>,
pub offset: Option<i32>,
}
/// Query parameters for a api call
pub(crate) type QueryParams = Vec<QueryParam>;
/// Query parameter tuple
pub(crate) type QueryParam = (String, String);
impl Pagination {
pub fn to_query(&self) -> QueryParams {
let limit = ("limit".to_string(), self.limit.unwrap_or(20).to_string());
let offset = ("offset".to_string(), self.offset.unwrap_or(0).to_string());
vec![limit, offset]
}
}
impl WomClient {
fn new_reqwest_client(api_key: Option<String>) -> reqwest::Client {
let client = reqwest::Client::builder().user_agent(APP_USER_AGENT);
match api_key {
Some(key) => {
let mut headers = HeaderMap::new();
headers.insert("x-api-key", HeaderValue::from_str(&*key).unwrap());
client.default_headers(headers)
}
None => client,
}
.build()
.unwrap()
}
fn new_wom_client(client: reqwest::Client, base_url: String) -> Self {
let _ = env_logger::try_init();
let sub_client_base_url = base_url.clone();
Self {
player_client: PlayerClient::new(client.clone(), &*sub_client_base_url),
group_client: GroupClient::new(client, &*sub_client_base_url),
}
}
///Creates a new `WOMClient`
pub fn new() -> Self {
let client = WomClient::new_reqwest_client(None);
Self::new_wom_client(client, BASE_URL.to_string())
}
///Creates a new `WOMClient` with an API key
pub fn new_with_key(api_key: String) -> Self {
let client = WomClient::new_reqwest_client(Some(api_key));
Self::new_wom_client(client, BASE_URL.to_string())
}
///Creates a new `WOMClient` with a custom base URL, and optionally an API key.
/// Used for testing and self hosted/League WOM instances
pub fn new_with_base_url(base_url: String, api_key: Option<String>) -> Self {
let client = WomClient::new_reqwest_client(api_key);
Self::new_wom_client(client, base_url)
}
}
pub(crate) mod helpers {
use crate::models::error::ErrorResponse;
use anyhow::anyhow;
use log::debug;
use reqwest::{Error, Response, StatusCode};
use serde::de::DeserializeOwned;
pub async fn handle_response<ResponseType: DeserializeOwned>(
response: Result<Response, Error>,
) -> Result<ResponseType, anyhow::Error> {
match response {
Ok(result) => match result.status() {
StatusCode::OK => {
let body = result.json::<ResponseType>().await;
match body {
Ok(body) => Ok(body),
Err(err) => Err(anyhow!(err)),
}
}
StatusCode::NOT_FOUND => {
let error_body = result.json::<ErrorResponse>().await;
match error_body {
Ok(body) => Err(anyhow!(body.message)),
Err(err) => Err(anyhow!(err)),
}
}
StatusCode::BAD_REQUEST => {
debug!("{:?}", result);
let error_body = result.json::<ErrorResponse>().await;
match error_body {
Ok(body) => Err(anyhow!(body.message)),
Err(err) => Err(anyhow!(err)),
}
}
_ => {
debug!("{:?}", result);
let error_body = result.json::<ErrorResponse>().await;
match error_body {
Ok(body) => Err(anyhow!(body.message)),
Err(err) => Err(anyhow!(err)),
}
}
},
Err(err) => Err(anyhow!(err)),
}
}
pub fn _handle_empty_response(response: Result<Response, Error>) -> Result<(), Error> {
match response {
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}
pub fn query_params_to_string(query_params: &Vec<(String, String)>) -> String {
let mut query_string = String::new();
for (index, (key, value)) in query_params.iter().enumerate() {
if index == 0 {
query_string.push_str(&format!("?{}={}", key, value));
} else {
query_string.push_str(&format!("&{}={}", key, value));
}
}
query_string
}
}
#[cfg(test)]
mod tests {}