Skip to content

Commit

Permalink
Fetch new card count from Satori
Browse files Browse the repository at this point in the history
  • Loading branch information
thoiberg committed Jul 4, 2023
1 parent 3b4779f commit 279f5a7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
17 changes: 15 additions & 2 deletions backend/src/api/satori/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ use chrono::{DateTime, Utc};
pub struct SatoriData {
data_updated_at: DateTime<Utc>,
active_review_count: u32,
new_card_count: u32,
}

impl SatoriData {
pub fn new(current_count: SatoriCurrentCardsResponse) -> Self {
pub fn new(
current_cards: SatoriCurrentCardsResponse,
new_cards: SatoriNewCardsResponse,
) -> Self {
Self {
data_updated_at: Utc::now(),
active_review_count: current_count.result,
active_review_count: current_cards.result,
new_card_count: new_cards.result,
}
}
}
Expand All @@ -22,3 +27,11 @@ pub struct SatoriCurrentCardsResponse {
message: Option<String>,
exception: Option<String>,
}

#[derive(serde::Deserialize)]
pub struct SatoriNewCardsResponse {
result: u32,
success: bool,
message: Option<String>,
exception: Option<String>,
}
6 changes: 6 additions & 0 deletions backend/src/api/satori/fixtures/new_cards_with_no_cards.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"result": 0,
"success": true,
"message": null,
"exception": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"result": 20,
"success": true,
"message": null,
"exception": null
}
58 changes: 52 additions & 6 deletions backend/src/api/satori/request.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use std::env;

use axum::Json;
use reqwest::{Client, StatusCode};
use reqwest::{Client, ClientBuilder, StatusCode};

use crate::api::{internal_error, ErrorResponse};

use super::data::{SatoriCurrentCardsResponse, SatoriData};
use super::data::{SatoriCurrentCardsResponse, SatoriData, SatoriNewCardsResponse};

pub async fn satori_handler() -> Result<Json<SatoriData>, (StatusCode, Json<ErrorResponse>)> {
let current_cards = get_current_cards().await.map_err(internal_error)?;
let new_cards = get_new_cards().await.map_err(internal_error)?;

let satori_data = SatoriData::new(current_cards);
let satori_data = SatoriData::new(current_cards, new_cards);

Ok(Json(satori_data))
}

async fn get_current_cards() -> anyhow::Result<SatoriCurrentCardsResponse> {
let satori_cookie = env::var("SATORI_COOKIE")?;
let client = satori_client()?.build()?;

Client::new()
client
.get("https://www.satorireader.com/api/studylist/due/count")
.header("Cookie", format!("SessionToken={}", satori_cookie))
.send()
.await?
.text()
Expand All @@ -34,6 +34,36 @@ fn serialize_current_cards_response(body: &str) -> anyhow::Result<SatoriCurrentC
Ok(json_data)
}

async fn get_new_cards() -> anyhow::Result<SatoriNewCardsResponse> {
let client = satori_client()?;

client
.build()?
.get("https://www.satorireader.com/api/studylist/pending-auto-importable/count")
.send()
.await?
.text()
.await
.map(|body| serialize_new_cards_response(&body))?
}

fn serialize_new_cards_response(body: &str) -> anyhow::Result<SatoriNewCardsResponse> {
let json_data: SatoriNewCardsResponse = serde_json::from_str(body)?;

Ok(json_data)
}

fn satori_client() -> anyhow::Result<ClientBuilder> {
let satori_cookie = env::var("SATORI_COOKIE")?;

let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
"Cookie",
format!("SessionToken={}", satori_cookie).parse().unwrap(),
);
Ok(Client::builder().default_headers(headers))
}

#[cfg(test)]
mod test_super {
use super::*;
Expand All @@ -53,4 +83,20 @@ mod test_super {

assert!(serialize_result.is_ok());
}

#[test]
fn test_new_card_with_pending_cards() {
let json_string = include_str!("./fixtures/new_cards_with_pending_cards.json");
let serialized_result = serialize_new_cards_response(json_string);

assert!(serialized_result.is_ok());
}

#[test]
fn test_new_card_with_no_cards() {
let json_string = include_str!("./fixtures/new_cards_with_no_cards.json");
let serialized_result = serialize_new_cards_response(json_string);

assert!(serialized_result.is_ok());
}
}

0 comments on commit 279f5a7

Please sign in to comment.