-
Notifications
You must be signed in to change notification settings - Fork 0
/
axum.rs
63 lines (49 loc) · 1.92 KB
/
axum.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
use axum::{
extract::Query,
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Json, Router,
};
use serde::Deserialize;
#[derive(Deserialize)]
struct GetByIdParams {
id: i32,
}
static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/character", get(get_esi_character))
.route("/corporation", get(get_esi_corporation));
let listener = tokio::net::TcpListener::bind("127.0.0.1:8000")
.await
.unwrap();
println!("Test character API at http://localhost:8000/character?id=2114794365");
println!("Test corporation API at http://localhost:8000/corporation?id=98785281");
axum::serve(listener, app).await.unwrap();
}
async fn get_esi_character(params: Query<GetByIdParams>) -> Response {
let mut esi_client: eve_esi::Client = eve_esi::Client::new(USER_AGENT);
esi_client.esi_url = "https://esi.evetech.net/latest".to_string();
let character_id: i32 = params.0.id;
match esi_client.get_character(character_id).await {
Ok(character) => (StatusCode::OK, Json(character)).into_response(),
Err(error) => {
let status_code: StatusCode =
StatusCode::from_u16(error.status().unwrap().into()).unwrap();
(status_code, Json(error.to_string())).into_response()
}
}
}
async fn get_esi_corporation(params: Query<GetByIdParams>) -> Response {
let esi_client: eve_esi::Client = eve_esi::Client::new(USER_AGENT);
let corporation_id: i32 = params.0.id;
match esi_client.get_corporation(corporation_id).await {
Ok(corporation) => (StatusCode::OK, Json(corporation)).into_response(),
Err(error) => {
let status_code = StatusCode::from_u16(error.status().unwrap().into()).unwrap();
(status_code, Json(error.to_string())).into_response()
}
}
}