Skip to content

Commit

Permalink
refactor: move get_snap_name to ratings/mod.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 11, 2024
1 parent e304976 commit c4332a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 60 deletions.
62 changes: 2 additions & 60 deletions src/ratings/categories.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Updating snap categories from data in snapcraft.io
use crate::{
db::{set_categories_for_snap, snap_has_categories, Category},
ratings::Error,
ratings::{get_json, get_snap_name, Error},
Context,
};
use cached::proc_macro::cached;
use serde::{de::DeserializeOwned, Deserialize};
use serde::Deserialize;
use sqlx::PgConnection;
use std::sync::Arc;
use tokio::sync::Notify;
Expand Down Expand Up @@ -78,26 +77,6 @@ async fn update_categories_inner(
Ok(())
}

#[inline]
async fn get_json<T: DeserializeOwned>(
url: reqwest::Url,
query: &[(&str, &str)],
client: &reqwest::Client,
) -> Result<T, Error> {
let s = client
.get(url)
.header("User-Agent", "ratings-service")
.header("Snap-Device-Series", 16)
.query(query)
.send()
.await?
.error_for_status()?
.text()
.await?;

Ok(serde_json::from_str(&s)?)
}

/// Pull snap categories by for a given snapd_id from the snapcraft.io rest API
async fn get_snap_categories(
snap_id: &str,
Expand Down Expand Up @@ -141,43 +120,6 @@ async fn get_snap_categories(
}
}

#[cfg_attr(
not(feature = "skip_cache"),
cached(
key = "String",
convert = r##"{String::from(snap_id)}"##,
result = true
)
)]
async fn get_snap_name(
snap_id: &str,
base_url: &reqwest::Url,
client: &reqwest::Client,
) -> Result<String, Error> {
let assertions_url = base_url
.join(&format!("assertions/snap-declaration/16/{snap_id}"))
.map_err(|e| Error::InvalidUrl(e.to_string()))?;

let AssertionsResp {
headers: Headers { snap_name },
} = get_json(assertions_url, &[], client).await?;

return Ok(snap_name);

// serde structs
//
#[derive(Debug, Deserialize)]
struct AssertionsResp {
headers: Headers,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Headers {
snap_name: String,
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
59 changes: 59 additions & 0 deletions src/ratings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ mod categories;
mod charts;
mod rating;

use cached::proc_macro::cached;
pub use categories::update_categories;
pub use charts::{Chart, ChartData};
pub use rating::{calculate_band, Rating, RatingsBand};
use serde::{de::DeserializeOwned, Deserialize};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -30,3 +32,60 @@ pub enum Error {
#[error(transparent)]
Json(#[from] serde_json::Error),
}

#[inline]
async fn get_json<T: DeserializeOwned>(
url: reqwest::Url,
query: &[(&str, &str)],
client: &reqwest::Client,
) -> Result<T, Error> {
let s = client
.get(url)
.header("User-Agent", "ratings-service")
.header("Snap-Device-Series", 16)
.query(query)
.send()
.await?
.error_for_status()?
.text()
.await?;

Ok(serde_json::from_str(&s)?)
}

#[cfg_attr(
not(feature = "skip_cache"),
cached(
key = "String",
convert = r##"{String::from(snap_id)}"##,
result = true
)
)]
async fn get_snap_name(
snap_id: &str,
base_url: &reqwest::Url,
client: &reqwest::Client,
) -> Result<String, Error> {
let assertions_url = base_url
.join(&format!("assertions/snap-declaration/16/{snap_id}"))
.map_err(|e| Error::InvalidUrl(e.to_string()))?;

let AssertionsResp {
headers: Headers { snap_name },
} = get_json(assertions_url, &[], client).await?;

return Ok(snap_name);

// serde structs
//
#[derive(Debug, Deserialize)]
struct AssertionsResp {
headers: Headers,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Headers {
snap_name: String,
}
}

0 comments on commit c4332a2

Please sign in to comment.