Saving changes
This commit is contained in:
@@ -3,6 +3,7 @@ pub const SECONDS_TO_SLEEP: u64 = 5;
|
|||||||
|
|
||||||
pub const APP_NAME: &str = "catapult";
|
pub const APP_NAME: &str = "catapult";
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub api_url: String,
|
pub api_url: String,
|
||||||
pub auth_url: String,
|
pub auth_url: String,
|
||||||
|
|||||||
+19
-1
@@ -14,8 +14,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let auth = catapult::service::auth::Auth {
|
||||||
|
app: app.clone()
|
||||||
|
};
|
||||||
|
let token = match auth.get_token().await {
|
||||||
|
Ok(token) => token,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error getting token");
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
std::process::exit(-1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut svc = catapult::service::Service {
|
||||||
|
app: app.clone(),
|
||||||
|
token
|
||||||
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
catapult::service::do_the_work(&app).await;
|
// catapult::service::do_the_work(&app).await;
|
||||||
|
svc.do_the_work().await;
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(
|
tokio::time::sleep(tokio::time::Duration::from_secs(
|
||||||
catapult::app::SECONDS_TO_SLEEP,
|
catapult::app::SECONDS_TO_SLEEP,
|
||||||
))
|
))
|
||||||
|
|||||||
+193
-21
@@ -1,22 +1,194 @@
|
|||||||
pub async fn do_the_work(_app: &crate::app::App) {
|
pub struct Service {
|
||||||
println!("Do some work");
|
pub app: crate::app::App,
|
||||||
todo!(
|
pub token: textsender_models::token::Login
|
||||||
r#"
|
|
||||||
Need to finish this
|
|
||||||
|
|
||||||
This function should fetch a token, check if the token is valid, and obtain a refresh token if
|
|
||||||
it has expired.
|
|
||||||
|
|
||||||
Then do the real work.
|
|
||||||
|
|
||||||
1. Check the queue
|
|
||||||
2. If there is an item, get the data,
|
|
||||||
3. Evaluate if it is schedulable
|
|
||||||
4. Get the events and iterate each one
|
|
||||||
5. starting with the message
|
|
||||||
6. Get the contact
|
|
||||||
7. Send the message
|
|
||||||
8. Record the message event response
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Service {
|
||||||
|
pub async fn do_the_work(&mut self) {
|
||||||
|
println!("Do some work");
|
||||||
|
todo!(
|
||||||
|
r#"
|
||||||
|
Need to finish this
|
||||||
|
|
||||||
|
This function should fetch a token, check if the token is valid, and obtain a refresh token if
|
||||||
|
it has expired.
|
||||||
|
|
||||||
|
Then do the real work.
|
||||||
|
|
||||||
|
1. Check the queue
|
||||||
|
2. If there is an item, get the data,
|
||||||
|
3. Evaluate if it is schedulable
|
||||||
|
4. Get the events and iterate each one
|
||||||
|
5. starting with the message
|
||||||
|
6. Get the contact
|
||||||
|
7. Send the message
|
||||||
|
8. Record the message event response
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod auth {
|
||||||
|
pub struct Auth {
|
||||||
|
pub app: crate::app::App
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
|
struct ServiceRequest {
|
||||||
|
pub username: String,
|
||||||
|
pub passphrase: String
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
impl Auth {
|
||||||
|
pub async fn get_token(&self) -> Result<textsender_models::token::Login, std::io::Error> {
|
||||||
|
/*
|
||||||
|
let svcRequest = ServiceRequest {
|
||||||
|
username: self.app.service_username,
|
||||||
|
passphrase: self.app.service_passphrase,
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let endpoint = String::from("api/v1/service/login");
|
||||||
|
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
||||||
|
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"username": self.app.auth_url,
|
||||||
|
"passphrase": self.app.api_url
|
||||||
|
});
|
||||||
|
|
||||||
|
match client.post(api_url).json(&payload).send().await {
|
||||||
|
Ok(response) => match response.status() {
|
||||||
|
reqwest::StatusCode::OK => {
|
||||||
|
println!("Response: {response:?}");
|
||||||
|
match response.json().await {
|
||||||
|
Ok(val) => {
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(std::io::Error::other(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
Err(std::io::Error::other("Error getting token"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
Err(std::io::Error::other(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let endpoint = String::from("api/v2/service/login");
|
||||||
|
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||||
|
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"passphrase": icarus_envy::environment::get_service_passphrase().await.value,
|
||||||
|
});
|
||||||
|
|
||||||
|
match client.post(api_url).json(&payload).send().await {
|
||||||
|
Ok(response) => match response
|
||||||
|
.json::<crate::api::service_token::response::Response>()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.data.is_empty() {
|
||||||
|
Err(std::io::Error::other(String::from("No token returned")))
|
||||||
|
} else {
|
||||||
|
Ok(resp.data[0].clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
type tokenResponse struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data []*token.Login `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Auth) GetToken() (*token.Login, error) {
|
||||||
|
serv := service{
|
||||||
|
Username: a.Application.ServiceUsername,
|
||||||
|
Passphrase: a.Application.ServicePassphrase,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(serv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resp, err := http.Post(
|
||||||
|
fmt.Sprintf("%s/api/v1/service/login", a.Application.AuthUrl),
|
||||||
|
"application/json",
|
||||||
|
bytes.NewBuffer(jsonData),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var r tokenResponse
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Data[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type refreshTokenRequest struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Auth) GetRefreshToken(tok *token.Login) (*token.Login, error) {
|
||||||
|
req := refreshTokenRequest{AccessToken: tok.AccessToken}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(fmt.Sprintf("%s/api/v1/token/refresh", a.Application.AuthUrl), "application/json", bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var r tokenResponse
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
if len(r.Data) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return r.Data[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Auth) TokenExpired(tok *token.Login) bool {
|
||||||
|
now := time.Now()
|
||||||
|
expiredTime := time.Unix(tok.ExpiresIn, 0)
|
||||||
|
|
||||||
|
if now.After(expiredTime) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user