Added register feature back with reqwest
This commit is contained in:
parent
64a5ebca2f
commit
e9d332487d
52
src/main.rs
52
src/main.rs
|
@ -79,7 +79,7 @@ async fn main() -> DynResult<()> {
|
|||
.build()
|
||||
.unwrap();
|
||||
let config: Config = get_config();
|
||||
let creds = get_mastodon_data(&config).await;
|
||||
let creds = get_mastodon_data(&client, &config).await;
|
||||
|
||||
match get_next_url(&config).await {
|
||||
Ok(image) => match image {
|
||||
|
@ -330,17 +330,59 @@ struct MastodonData {
|
|||
token: String,
|
||||
}
|
||||
|
||||
async fn get_mastodon_data(_config: &Config) -> MastodonData {
|
||||
async fn get_mastodon_data(client: &Client, config: &Config) -> MastodonData {
|
||||
match parse_mastodon_data(CREDENTIALS_FILENAME) {
|
||||
Ok(config) => config,
|
||||
Err(err) => {
|
||||
log::error!("Credentials file parsing unsuccesful: {}", err);
|
||||
log::info!("Please check the working directory to find a new Credentials file");
|
||||
register(client, config).await;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AppRegister {
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
}
|
||||
|
||||
async fn register(client: &Client, config: &Config) {
|
||||
let request = vec![
|
||||
("client_name", config.bot.name.as_str()),
|
||||
("redirect_uris", "urn:ietf:wg:oauth:2.0:oob"),
|
||||
("scopes", "write"),
|
||||
];
|
||||
|
||||
let app: AppRegister = client
|
||||
.post(format!("{}/api/v1/apps", config.bot.instance))
|
||||
.form(&request)
|
||||
.send()
|
||||
.await
|
||||
.expect("Error sending request to instance on app register")
|
||||
.json()
|
||||
.await
|
||||
.expect("Error parsing app register response");
|
||||
|
||||
println!("Please enter into the following url to authrise:");
|
||||
println!("{}/oauth/authorize?client_id={}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=write", config.bot.instance, app.client_id);
|
||||
let mut token = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut token)
|
||||
.expect("Error reading stdin");
|
||||
|
||||
let toml = toml::to_string(&MastodonData {
|
||||
base: config.bot.instance.clone(),
|
||||
client_id: app.client_id,
|
||||
client_secret: app.client_secret,
|
||||
redirect: "urn:ietf:wg:oauth:2.0:oob".to_string(),
|
||||
token,
|
||||
})
|
||||
.expect("Failed to create credentials file");
|
||||
|
||||
std::fs::write(CREDENTIALS_FILENAME, toml).expect("Failed to write credentials file");
|
||||
}
|
||||
|
||||
fn parse_mastodon_data(filename: &str) -> DynResult<MastodonData> {
|
||||
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it");
|
||||
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
|
||||
|
@ -478,7 +520,7 @@ mod tests {
|
|||
.build()
|
||||
.unwrap();
|
||||
let config = get_config();
|
||||
let creds = get_mastodon_data(&config).await;
|
||||
let creds = get_mastodon_data(&client, &config).await;
|
||||
let msg = "Test!".to_string();
|
||||
|
||||
let status = post(&client, &creds, &config, &msg, Visibility::direct)
|
||||
|
@ -499,7 +541,7 @@ mod tests {
|
|||
.build()
|
||||
.unwrap();
|
||||
let config = get_config();
|
||||
let creds = get_mastodon_data(&config).await;
|
||||
let creds = get_mastodon_data(&client, &config).await;
|
||||
|
||||
let status = post_image(
|
||||
&client,
|
||||
|
|
Loading…
Reference in New Issue