1
0

test refresh

This commit is contained in:
syui 2024-02-17 12:56:11 +09:00
parent 807f7814c7
commit 51c41897e2
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
22 changed files with 97 additions and 35 deletions

View File

@ -18,6 +18,7 @@ pub fn data_file(s: &str) -> String {
match &*s {
"toml" => f + &"token.toml",
"json" => f + &"token.json",
"refresh" => f + &"refresh.toml",
_ => f + &"." + &s,
}
}
@ -59,6 +60,17 @@ impl Data {
}
}
impl Refresh {
pub fn new() -> Result<Self, ConfigError> {
let d = data_file("refresh");
let s = Config::builder()
.add_source(File::with_name(&d))
.add_source(config::Environment::with_prefix("APP"))
.build()?;
s.try_deserialize()
}
}
#[derive(Debug, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct Token {
@ -78,6 +90,13 @@ pub struct Data {
pub refresh: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct Refresh {
pub access: String,
pub refresh: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BaseUrl {
pub profile_get: String,
@ -190,6 +209,20 @@ pub fn data_toml(s: &str) -> String {
}
}
pub fn data_refresh(s: &str) -> String {
let s = String::from(s);
let refresh = Refresh::new().unwrap();
let refresh = Refresh {
access: refresh.access,
refresh: refresh.refresh,
};
match &*s {
"access" => refresh.access,
"refresh" => refresh.refresh,
_ => s,
}
}
pub fn data_scpt(s: &str) -> String {
let s = String::from(s);
let file = "/.config/ai/scpt/".to_owned() + &s + &".zsh";
@ -475,6 +508,21 @@ pub fn w_cfg(h: &str, res: &str) {
ff.write_all(&toml.as_bytes()).unwrap();
}
pub fn w_refresh(res: &str) {
let f = data_file(&"json");
let ff = data_file(&"refresh");
let mut f = fs::File::create(f.clone()).unwrap();
let mut ff = fs::File::create(ff.clone()).unwrap();
f.write_all(&res.as_bytes()).unwrap();
let json: Token = serde_json::from_str(&res).unwrap();
let refreshs = Refresh {
access: json.accessJwt.to_string(),
refresh: json.refreshJwt.to_string(),
};
let toml = toml::to_string(&refreshs).unwrap();
ff.write_all(&toml.as_bytes()).unwrap();
}
pub fn w_cid(cid: String, file: String, t: bool) -> bool {
let f = file;
let mut file = match OpenOptions::new()
@ -558,3 +606,4 @@ pub struct OpenData {
pub struct Choices {
text: String,
}

View File

@ -3,7 +3,7 @@ extern crate reqwest;
use crate::url;
pub async fn get_request(user: String) -> String {
//let token = data_toml(&"access");
//let token = data_refresh(&"access");
let url = url(&"describe");
let client = reqwest::Client::new();

View File

@ -1,5 +1,6 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
@ -7,7 +8,7 @@ use serde_json::json;
//use crate::data::Follow;
pub async fn post_request(u: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");
@ -43,7 +44,7 @@ pub async fn post_request(u: String) -> String {
}
pub async fn delete_request(u: String, rkey: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,10 +1,10 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
//use serde_json::json;
pub async fn get_request(actor: String, cursor: Option<String>) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"followers");
let cursor = cursor.unwrap();

View File

@ -1,10 +1,10 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
//use serde_json::json;
pub async fn get_request(actor: String, cursor: Option<String>) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"follows");
let cursor = cursor.unwrap();
//let cursor = "1682386039125::bafyreihwgwozmvqxcxrhbr65agcaa4v357p27ccrhzkjf3mz5xiozjvzfa".to_string();

View File

@ -1,12 +1,13 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use serde_json::json;
use iso8601_timestamp::Timestamp;
pub async fn post_request(text: String, link: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,5 +1,6 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
@ -11,7 +12,7 @@ pub async fn post_request(
uri: String,
itype: String,
) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,12 +1,13 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use serde_json::json;
use iso8601_timestamp::Timestamp;
pub async fn post_request(text: String, link: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,11 +1,12 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
pub async fn post_request(cid: String, uri: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -6,8 +6,10 @@ use crate::bot::c_bot;
use crate::data::c_follow_all;
use crate::data::c_openai_key;
use crate::data::data_toml;
use crate::data::data_refresh;
use crate::data::url;
use crate::data::w_cfg;
use crate::data::w_refresh;
use data::ProfileIdentityResolve;
@ -280,12 +282,14 @@ fn token(c: &Context) {
if let Ok(p) = c.string_flag("password") {
if let Ok(s) = c.string_flag("server") {
let res = token::post_request(m.to_string(), p.to_string(), s.to_string()).await;
w_cfg(&s, &res)
w_cfg(&s, &res);
w_refresh(&res);
} else {
let res =
token::post_request(m.to_string(), p.to_string(), "bsky.social".to_string())
.await;
w_cfg(&"bsky.social", &res)
w_cfg(&"bsky.social", &res);
w_refresh(&res);
}
}
};
@ -294,15 +298,12 @@ fn token(c: &Context) {
}
fn refresh(_c: &Context) {
let server = data_toml(&"host");
let h = async {
let session = session::get_request().await;
if session == "err" {
let res = refresh::post_request().await;
println!("{}", res);
w_cfg(&server, &res)
} else {
println!("no refresh");
w_refresh(&res)
}
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
@ -497,7 +498,7 @@ fn reply(c: &Context) {
#[tokio::main]
async fn c_img_upload(c: &Context) -> reqwest::Result<()> {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let atoken = "Authorization: Bearer ".to_owned() + &token;
let con = "Content-Type: image/png";
let url = url(&"upload_blob");

View File

@ -1,11 +1,12 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
pub async fn post_request(text: String, at: String, udid: String, s: i32, e: i32) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,10 +1,10 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
//use serde_json::json;
pub async fn get_request(limit: i32) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"notify_list");
let client = reqwest::Client::new();

View File

@ -1,10 +1,10 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use serde_json::json;
pub async fn post_request(time: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"notify_update");
let post = Some(json!({

View File

@ -1,11 +1,12 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
pub async fn post_request(text: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,11 +1,12 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
pub async fn post_request(text: String, link: String, s: i32, e: i32) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,9 +1,9 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
pub async fn get_request(user: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"profile_get") + &"?handle=" + &user;
let client = reqwest::Client::new();

View File

@ -1,5 +1,6 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
@ -11,7 +12,7 @@ pub async fn post_request(
cid_root: String,
uri_root: String,
) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,5 +1,6 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
@ -14,7 +15,7 @@ pub async fn post_request(
cid_root: String,
uri_root: String,
) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,5 +1,6 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
@ -13,7 +14,7 @@ pub async fn post_request(
title: String,
description: String,
) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,11 +1,12 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
use iso8601_timestamp::Timestamp;
use serde_json::json;
pub async fn post_request(cid: String, uri: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let did = data_toml(&"did");
let handle = data_toml(&"handle");

View File

@ -1,9 +1,9 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
pub async fn get_request() -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"session_get");
let client = reqwest::Client::new();

View File

@ -1,9 +1,9 @@
extern crate reqwest;
use crate::data_toml;
use crate::data_refresh;
use crate::url;
pub async fn get_request(actor: String) -> String {
let token = data_toml(&"access");
let token = data_refresh(&"access");
let url = url(&"record_list");
let actor = actor.to_string();