|
1 | 1 | /* SPDX-License-Identifier: GPL-3.0-or-later */ |
2 | 2 | /* Copyright © 2026 Eduard Smet */ |
3 | 3 |
|
4 | | -use std::{ |
5 | | - str::FromStr, |
6 | | - sync::{Arc, LazyLock}, |
7 | | -}; |
| 4 | +use std::str::FromStr; |
8 | 5 |
|
| 6 | +use anyhow::{Context, Error, Result}; |
9 | 7 | use reqwest::StatusCode; |
10 | | -use tracing::{debug, error}; |
| 8 | +use tracing::debug; |
11 | 9 | use url::{ParseError, Url}; |
12 | 10 |
|
13 | 11 | use crate::http::HttpClient; |
14 | 12 |
|
15 | | -static DEFAULT_REGISTRY_URL: LazyLock<Url> = LazyLock::new(|| { |
16 | | - Url::parse("https://raw.githubusercontent.com/celarye/discord-bot-plugins/refs/heads/master/") |
17 | | - .unwrap() |
18 | | -}); |
19 | | - |
20 | 13 | impl HttpClient { |
21 | | - pub async fn get_file_from_registry( |
22 | | - &self, |
23 | | - registry: &Arc<Option<String>>, |
24 | | - path: &str, |
25 | | - ) -> Result<Vec<u8>, ()> { |
26 | | - let url = match Self::parse_url(registry, path) { |
27 | | - Ok(url) => url, |
28 | | - Err(err) => { |
29 | | - error!( |
30 | | - "An error occurred while trying to construct a valid URL from the provided registry and path: {err}" |
31 | | - ); |
32 | | - return Err(()); |
33 | | - } |
34 | | - }; |
| 14 | + pub async fn get_file_from_registry(&self, registry: &str, path: &str) -> Result<Vec<u8>> { |
| 15 | + let url = Self::parse_url(registry, path).context("An error occurred while trying to construct a valid URL from the provided registry and path")?; |
35 | 16 |
|
36 | 17 | debug!("Requested registry file: {url}"); |
37 | 18 |
|
38 | | - match self.client.get(url).send().await { |
39 | | - Ok(raw_response) => { |
40 | | - if raw_response.status() != StatusCode::OK { |
41 | | - error!( |
42 | | - "The response was undesired, status code: {}", |
43 | | - raw_response.status(), |
44 | | - ); |
45 | | - return Err(()); |
46 | | - } |
| 19 | + let response = self.client.get(url).send().await?; |
47 | 20 |
|
48 | | - match raw_response.bytes().await { |
49 | | - Ok(response) => Ok(response.to_vec()), |
50 | | - Err(err) => { |
51 | | - error!( |
52 | | - "Something went wrong while getting the raw bytes from the response, error: {err}" |
53 | | - ); |
54 | | - Err(()) |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - Err(err) => { |
59 | | - error!("Something went wrong while making the request, error: {err}"); |
60 | | - Err(()) |
61 | | - } |
| 21 | + if response.status() != StatusCode::OK { |
| 22 | + return Err(Error::msg(format!( |
| 23 | + "The response was undesired, status code: {}", |
| 24 | + response.status() |
| 25 | + ))); |
62 | 26 | } |
63 | | - } |
64 | 27 |
|
65 | | - fn parse_url(registry: &Arc<Option<String>>, path: &str) -> Result<Url, ParseError> { |
66 | | - if let Some(registry) = registry.as_deref() { |
67 | | - return Url::from_str(registry)?.join(path); |
68 | | - } |
| 28 | + Ok(response |
| 29 | + .bytes() |
| 30 | + .await |
| 31 | + .context("Something went wrong while getting the raw bytes from the response")? |
| 32 | + .to_vec()) |
| 33 | + } |
69 | 34 |
|
70 | | - DEFAULT_REGISTRY_URL.join(path) |
| 35 | + fn parse_url(registry: &str, path: &str) -> Result<Url, ParseError> { |
| 36 | + Url::from_str(&format!("https://{registry}/"))?.join(path) |
71 | 37 | } |
72 | 38 | } |
0 commit comments