Share reqwest::Client in gallery example

This commit is contained in:
Héctor Ramón Jiménez 2025-10-26 04:22:11 +01:00
parent 1c055e26c0
commit 8b12b313a9
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 6 additions and 8 deletions

View file

@ -4,7 +4,9 @@ use tokio::task;
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::sync::Arc; use std::sync::{Arc, LazyLock};
static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(reqwest::Client::new);
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Image { pub struct Image {
@ -17,14 +19,12 @@ impl Image {
pub const LIMIT: usize = 96; pub const LIMIT: usize = 96;
pub async fn list() -> Result<Vec<Self>, Error> { pub async fn list() -> Result<Vec<Self>, Error> {
let client = reqwest::Client::new();
#[derive(Deserialize)] #[derive(Deserialize)]
struct Response { struct Response {
items: Vec<Image>, items: Vec<Image>,
} }
let response: Response = client let response: Response = CLIENT
.get("https://civitai.com/api/v1/images") .get("https://civitai.com/api/v1/images")
.query(&[ .query(&[
("sort", "Most Reactions"), ("sort", "Most Reactions"),
@ -66,8 +66,6 @@ impl Image {
pub fn download(self, size: Size) -> impl Straw<Bytes, Blurhash, Error> { pub fn download(self, size: Size) -> impl Straw<Bytes, Blurhash, Error> {
sipper(async move |mut sender| { sipper(async move |mut sender| {
let client = reqwest::Client::new();
if let Size::Thumbnail { width, height } = size { if let Size::Thumbnail { width, height } = size {
let image = self.clone(); let image = self.clone();
@ -78,7 +76,7 @@ impl Image {
})); }));
} }
let bytes = client let bytes = CLIENT
.get(match size { .get(match size {
Size::Original => self.url, Size::Original => self.url,
Size::Thumbnail { width, .. } => self Size::Thumbnail { width, .. } => self

View file

@ -588,4 +588,4 @@ fn rounded(theme: &Theme) -> container::Style {
container::dark(theme).border(border::rounded(BORDER_RADIUS)) container::dark(theme).border(border::rounded(BORDER_RADIUS))
} }
const BORDER_RADIUS: u32 = 20; const BORDER_RADIUS: u32 = 10;