2025-02-02 00:46:04 +01:00
|
|
|
use std::{env, result};
|
2025-02-02 20:25:22 +01:00
|
|
|
use std::array::TryFromSliceError;
|
2025-11-13 09:51:05 +01:00
|
|
|
use std::ops::Deref;
|
2025-05-28 22:39:38 +02:00
|
|
|
use json::{object, JsonValue};
|
2025-02-02 00:46:04 +01:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
use thiserror::Error;
|
2025-11-13 09:51:05 +01:00
|
|
|
use tokio::task::{spawn_blocking, JoinError};
|
2025-05-28 22:39:38 +02:00
|
|
|
use crate::grpc::account::Error::SomethingHappened;
|
|
|
|
|
static API_KEY: Lazy<String> = Lazy::new(||{
|
|
|
|
|
let key = env::var("ACCOUNT_GQL_API_KEY")
|
2025-06-13 12:36:28 +02:00
|
|
|
.expect("no graphql ip specified");
|
2025-02-02 00:46:04 +01:00
|
|
|
|
2025-05-28 22:39:38 +02:00
|
|
|
key
|
2025-02-02 00:46:04 +01:00
|
|
|
});
|
|
|
|
|
|
2025-05-28 22:39:38 +02:00
|
|
|
static CLIENT_URI: Lazy<String> = Lazy::new(||{
|
|
|
|
|
env::var("ACCOUNT_GQL_URL")
|
2025-02-02 00:46:04 +01:00
|
|
|
.ok()
|
|
|
|
|
.and_then(|s| s.parse().ok())
|
2025-06-13 12:36:28 +02:00
|
|
|
.expect("no graphql ip specified")
|
2025-02-02 00:46:04 +01:00
|
|
|
});
|
|
|
|
|
|
2025-05-28 22:39:38 +02:00
|
|
|
|
2025-02-02 00:46:04 +01:00
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
|
pub enum Error{
|
|
|
|
|
#[error(transparent)]
|
2025-11-13 09:51:05 +01:00
|
|
|
RequestError(#[from] ureq::Error),
|
2025-05-28 22:39:38 +02:00
|
|
|
#[error(transparent)]
|
|
|
|
|
Json(#[from] json::Error),
|
2025-11-13 09:51:05 +01:00
|
|
|
//#[error(transparent)]
|
|
|
|
|
//Status(#[from] tonic::Status),
|
2025-02-02 20:25:22 +01:00
|
|
|
#[error("invalid password size: {0}")]
|
2025-05-28 22:39:38 +02:00
|
|
|
PasswordConversion(#[from] TryFromSliceError),
|
|
|
|
|
#[error("something happened")]
|
2025-11-13 09:51:05 +01:00
|
|
|
SomethingHappened,
|
|
|
|
|
#[error("error joining blocking task: {0}")]
|
|
|
|
|
Join(#[from] JoinError)
|
2025-02-02 00:46:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
|
2025-11-13 09:51:05 +01:00
|
|
|
pub struct Client;//(reqwest::Client);
|
2025-05-28 22:39:38 +02:00
|
|
|
|
|
|
|
|
impl Client{
|
|
|
|
|
pub async fn new() -> Result<Self> {
|
2025-11-13 09:51:05 +01:00
|
|
|
//Ok(Self(reqwest::ClientBuilder::new().build()?))
|
|
|
|
|
Ok(Self)
|
2025-05-28 22:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn do_request(&self, request_data: JsonValue) -> Result<JsonValue>{
|
2025-11-13 10:06:58 +01:00
|
|
|
let request = ureq::post(CLIENT_URI.as_str())
|
2025-11-13 09:51:05 +01:00
|
|
|
.header("X-API-Key", API_KEY.deref())
|
|
|
|
|
.content_type("application/json");
|
|
|
|
|
let mut response = spawn_blocking(move || request.send(request_data.to_string())).await??;
|
|
|
|
|
|
|
|
|
|
let str_body = response.body_mut().read_to_string()?;
|
|
|
|
|
Ok(json::parse(&str_body)?)
|
|
|
|
|
/*
|
2025-05-28 22:39:38 +02:00
|
|
|
let mut request = reqwest::Request::new(Method::POST, Url::from_str(CLIENT_URI.as_str()).unwrap());
|
|
|
|
|
|
|
|
|
|
*(request.body_mut()) = Some(Body::from(request_data.to_string()));
|
|
|
|
|
request.headers_mut().insert("X-API-Key", HeaderValue::from_str(&API_KEY).unwrap());
|
|
|
|
|
request.headers_mut().insert("Content-Type", HeaderValue::from_str("application/json").unwrap());
|
|
|
|
|
|
|
|
|
|
let response = self.0.execute(request).await?;
|
|
|
|
|
|
|
|
|
|
Ok(json::parse(&response.text().await?)?)
|
2025-11-13 09:51:05 +01:00
|
|
|
|
|
|
|
|
*/
|
2025-05-28 22:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_nex_password(&mut self , pid: u32) -> Result<[u8; 16]>{
|
|
|
|
|
let req = self.do_request(object!{
|
|
|
|
|
"query": r"query($pid: Int!){
|
|
|
|
|
userByPid(pid: $pid){
|
|
|
|
|
nexPassword
|
|
|
|
|
}
|
|
|
|
|
}",
|
|
|
|
|
"variables": {
|
|
|
|
|
"pid": pid
|
|
|
|
|
}
|
|
|
|
|
}).await?;
|
|
|
|
|
|
|
|
|
|
let Some(val) = req.entries()
|
|
|
|
|
.find(|v| v.0 == "data")
|
|
|
|
|
.ok_or(SomethingHappened)?.1
|
|
|
|
|
.entries()
|
|
|
|
|
.find(|v| v.0 == "userByPid")
|
|
|
|
|
.ok_or(SomethingHappened)?.1
|
|
|
|
|
.entries()
|
|
|
|
|
.find(|v| v.0 == "nexPassword")
|
|
|
|
|
.ok_or(SomethingHappened)?.1
|
|
|
|
|
.as_str() else {
|
|
|
|
|
return Err(SomethingHappened);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(val.as_bytes().try_into().map_err(|_| SomethingHappened)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*pub async fn get_user_data(&mut self , pid: u32) -> Result<GetUserDataResponse>{
|
|
|
|
|
let req = Request::new(GetUserDataRequest{
|
|
|
|
|
pid
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let response = self.0.get_user_data(req).await?.into_inner();
|
|
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 00:46:04 +01:00
|
|
|
|
|
|
|
|
|
2025-05-28 22:39:38 +02:00
|
|
|
/*
|
|
|
|
|
|
2025-02-02 00:46:04 +01:00
|
|
|
pub struct Client(AccountClient<InterceptedService<Channel, InterceptorFunc>>);
|
|
|
|
|
|
|
|
|
|
impl Client{
|
|
|
|
|
pub async fn new() -> Result<Self>{
|
|
|
|
|
let channel = Channel::from_static(&*CLIENT_URI).connect().await?;
|
|
|
|
|
|
|
|
|
|
let func = Box::new(&|mut req: Request<()>|{
|
|
|
|
|
req.metadata_mut().insert("x-api-key", API_KEY.clone());
|
|
|
|
|
Ok(req)
|
|
|
|
|
}) as InterceptorFunc;
|
|
|
|
|
|
|
|
|
|
let client = AccountClient::with_interceptor(channel, func);
|
|
|
|
|
Ok(Self(client))
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 20:25:22 +01:00
|
|
|
pub async fn get_nex_password(&mut self , pid: u32) -> Result<[u8; 16]>{
|
2025-02-02 00:46:04 +01:00
|
|
|
let req = Request::new(GetNexPasswordRequest{
|
|
|
|
|
pid
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let response = self.0.get_nex_password(req).await?.into_inner();
|
|
|
|
|
|
2025-02-02 20:25:22 +01:00
|
|
|
Ok(response.password.as_bytes().try_into()?)
|
2025-02-02 00:46:04 +01:00
|
|
|
}
|
2025-02-05 16:02:32 +01:00
|
|
|
|
|
|
|
|
pub async fn get_user_data(&mut self , pid: u32) -> Result<GetUserDataResponse>{
|
|
|
|
|
let req = Request::new(GetUserDataRequest{
|
|
|
|
|
pid
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let response = self.0.get_user_data(req).await?.into_inner();
|
|
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
2025-02-02 00:46:04 +01:00
|
|
|
}
|
2025-11-05 21:57:57 +01:00
|
|
|
*/
|