Skip to content

Commit

Permalink
Adding refresh cookies and/or crumb if they have expired
Browse files Browse the repository at this point in the history
  • Loading branch information
7jrxt42BxFZo4iAnN4CX committed Jan 18, 2025
1 parent a9d5959 commit bdd9ac7
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 29 deletions.
107 changes: 79 additions & 28 deletions src/async_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl YahooConnector {
if let None = &self.crumb {
self.crumb = Some(self.get_crumb().await?);
}

let cookie_provider = Arc::new(reqwest::cookie::Jar::default());
let url = reqwest::Url::parse(
&(format!(
Expand All @@ -135,28 +134,46 @@ impl YahooConnector {

cookie_provider.add_cookie_str(&self.cookie.clone().unwrap(), &url.clone().unwrap());

Ok(self
.create_client(Some(cookie_provider))
.await?
.get(url.unwrap())
.send()
.await?
.json()
.await?)
}
let mut result: Result<YQuoteSummary, YahooError> = Err(YahooError::NoResponse);

let max_retries = 1;
for i in 0..=max_retries {
result = Ok(self
.create_client(Some(cookie_provider.clone()))
.await?
.get(url.clone().unwrap())
.send()
.await?
.json()
.await?);

if let Ok(result) = &result {
if let Some(finance) = &result.finance {
if let Some(error) = &finance.error {
if let Some(description) = &error.description {
if description.contains("Invalid Crumb") {
self.crumb = Some(self.get_crumb().await?);
if i == max_retries {
return Err(YahooError::InvalidCrumb);
}
}
}
if let Some(code) = &error.code {
if code.contains("Unauthorized") {
println!("Unauthorized {:?}", i);
self.crumb = Some(self.get_crumb().await?);
if i == max_retries {
return Err(YahooError::Unauthorized);
}
}
}
}
}
}
}

async fn get_cookie(&mut self) -> Result<String, YahooError> {
Ok(self
.client
.get(Y_GET_COOKIE_URL)
.send()
.await?
.headers()
.get(Y_COOKIE_REQUEST_HEADER)
.ok_or(YahooError::NoCookies)?
.to_str()
.map_err(|_| YahooError::InvisibleAsciiInCookies)?
.to_string())
// Ok(result)
result
}

async fn get_crumb(&mut self) -> Result<String, YahooError> {
Expand All @@ -170,14 +187,47 @@ impl YahooConnector {
&reqwest::Url::parse(Y_GET_CRUMB_URL).unwrap(),
);

let mut result = Err(YahooError::NoResponse);

let max_retries = 1;
for i in 0..=max_retries {
result = Ok(self
.create_client(Some(cookie_provider.clone()))
.await?
.get(Y_GET_CRUMB_URL)
.send()
.await?
.text()
.await?);

if let Ok(result) = &result {
if result.contains("Invalid Cookie") {
self.cookie = Some(self.get_cookie().await?);
if i == max_retries {
return Err(YahooError::InvalidCookie);
}
}
}
}

// Ok(result)
result
}

async fn get_cookie(&mut self) -> Result<String, YahooError> {
println!("get_cookie()");

Ok(self
.create_client(Some(cookie_provider))
.await?
.get(Y_GET_CRUMB_URL)
.client
.get(Y_GET_COOKIE_URL)
.send()
.await?
.text()
.await?)
.headers()
.get(Y_COOKIE_REQUEST_HEADER)
.ok_or(YahooError::NoCookies)?
.to_str()
.map_err(|_| YahooError::InvisibleAsciiInCookies)?
.to_string())
}

async fn create_client(
Expand Down Expand Up @@ -412,7 +462,8 @@ mod tests {
let result = tokio_test::block_on(provider.get_ticker_info("AAPL"));

let quote_summary = result.unwrap().quote_summary;
assert!("Cupertino" == quote_summary.result[0].asset_profile.city); // Testing it retrieved info, hard coded but shouldn't change anytime soon
assert!("Cupertino" == quote_summary.unwrap().result[0].asset_profile.city);
// Testing it retrieved info, hard coded but shouldn't change anytime soon
}

#[tokio::test]
Expand Down
15 changes: 14 additions & 1 deletion src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,20 @@ pub struct CapitalGain {
#[derive(Deserialize, Debug)]
pub struct YQuoteSummary {
#[serde(rename = "quoteSummary")]
pub quote_summary: ExtendedQuoteSummary,
pub quote_summary: Option<ExtendedQuoteSummary>,
pub finance: Option<YFinance>,
}

#[derive(Deserialize, Debug)]
pub struct YFinance {
pub result: Option<serde_json::Value>,
pub error: Option<YErrorMessage>,
}

#[derive(Deserialize, Debug)]
pub struct YErrorMessage {
pub code: Option<String>,
pub description: Option<String>,
}

#[derive(Deserialize, Debug)]
Expand Down
8 changes: 8 additions & 0 deletions src/yahoo_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ pub enum YahooError {
NoCookies,
#[error("Invisible characters in cookies")]
InvisibleAsciiInCookies,
#[error("No response")]
NoResponse,
#[error("Invalid cookie")]
InvalidCookie,
#[error("Unauthorized")]
Unauthorized,
#[error("Invalid crumb")]
InvalidCrumb,
}

0 comments on commit bdd9ac7

Please sign in to comment.