Skip to content

Commit

Permalink
Add check for unauthorized - refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
sigaloid committed Jan 28, 2024
1 parent d0c5a1d commit 6c2579c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use hyper::client::HttpConnector;
use hyper::{body, body::Buf, client, header, Body, Client, Method, Request, Response, Uri};
use hyper_rustls::HttpsConnector;
use libflate::gzip;
use log::error;
use once_cell::sync::Lazy;
use percent_encoding::{percent_encode, CONTROLS};
use serde_json::Value;
Expand All @@ -13,7 +14,7 @@ use std::{io, result::Result};
use tokio::sync::RwLock;

use crate::dbg_msg;
use crate::oauth::{token_daemon, Oauth};
use crate::oauth::{force_refresh_token, token_daemon, Oauth};
use crate::server::RequestExt;
use crate::utils::format_url;

Expand Down Expand Up @@ -335,6 +336,8 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
}
}
Err(e) => {
error!("Got a bad response from reddit {e} - forcing a token refresh. Status code: {status}");
let _ = force_refresh_token().await;
if status.is_server_error() {
Err("Reddit is having issues, check if there's an outage".to_string())
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ pub async fn token_daemon() {
}
}
}

pub async fn force_refresh_token() {
OAUTH_CLIENT.write().await.refresh().await;
}

#[derive(Debug, Clone, Default)]
struct Device {
oauth_id: String,
Expand Down

0 comments on commit 6c2579c

Please sign in to comment.