forked from minecrust/hopper
Compare commits
6 Commits
74de0d15b3
...
132d82680b
Author | SHA1 | Date |
---|---|---|
Emma Tebibyte | 132d82680b | |
Emma Tebibyte | 5ae5aa668d | |
Emma Tebibyte | 3c87609720 | |
[ ] | 552d75cbff | |
[ ] | 9064888265 | |
[ ] | f7ba63b492 |
16
src/api.rs
16
src/api.rs
|
@ -1,6 +1,6 @@
|
|||
use console::style;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, fmt};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SearchResponse {
|
||||
|
@ -140,3 +140,17 @@ pub struct ModVersionFile {
|
|||
pub primary: bool,
|
||||
pub size: isize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Error {
|
||||
pub error: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}: {}", self.error, self.description)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::api::{ModInfo, ModResult, ModVersion, ModVersionFile, SearchResponse};
|
||||
use crate::api::{ModInfo, ModResult, ModVersion, ModVersionFile, SearchResponse, Error as APIError};
|
||||
use crate::config::{Args, Config, PackageType, SearchArgs};
|
||||
use futures_util::StreamExt;
|
||||
use log::*;
|
||||
|
@ -14,7 +14,10 @@ impl HopperClient {
|
|||
pub fn new(config: Config) -> Self {
|
||||
Self {
|
||||
config: config,
|
||||
client: reqwest::Client::new(),
|
||||
client: reqwest::ClientBuilder::new()
|
||||
.user_agent(format!("tebibytemedia/hopper/{} (tebibyte.media)", env!("CARGO_PKG_VERSION")))
|
||||
.build()
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,18 +54,20 @@ impl HopperClient {
|
|||
.to_string();
|
||||
facets.push(package_type_facet);
|
||||
}
|
||||
params.push(("facets", format!("[{}]", facets.join(","))));
|
||||
|
||||
if !facets.is_empty() {
|
||||
params.push(("facets", format!("[{}]", facets.join(","))));
|
||||
}
|
||||
|
||||
let url = reqwest::Url::parse_with_params(url.as_str(), ¶ms)?;
|
||||
info!("GET {}", url);
|
||||
let response = self
|
||||
.client
|
||||
.get(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<SearchResponse>()
|
||||
.await?;
|
||||
Ok(response)
|
||||
let response = self.client.get(url).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(response.json::<SearchResponse>().await?)
|
||||
} else {
|
||||
Err(response.json::<APIError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch_mod_info(&self, mod_result: &ModResult) -> anyhow::Result<ModInfo> {
|
||||
|
@ -78,8 +83,12 @@ impl HopperClient {
|
|||
);
|
||||
info!("GET {}", url);
|
||||
let response = self.client.get(url).send().await?;
|
||||
let response = response.json::<ModInfo>().await?;
|
||||
Ok(response)
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(response.json::<ModInfo>().await?)
|
||||
} else {
|
||||
Err(response.json::<APIError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch_mod_version(&self, version_id: &String) -> anyhow::Result<ModVersion> {
|
||||
|
@ -91,8 +100,12 @@ impl HopperClient {
|
|||
);
|
||||
info!("GET {}", url);
|
||||
let response = self.client.get(url).send().await?;
|
||||
let response = response.json::<ModVersion>().await?;
|
||||
Ok(response)
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(response.json::<ModVersion>().await?)
|
||||
} else {
|
||||
Err(response.json::<APIError>().await?.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_version_file(
|
||||
|
@ -119,6 +132,11 @@ impl HopperClient {
|
|||
let url = &file.url;
|
||||
info!("GET {}", url);
|
||||
let response = self.client.get(url).send().await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(response.json::<APIError>().await?.into())
|
||||
}
|
||||
|
||||
let total_size = response.content_length().unwrap();
|
||||
|
||||
// TODO better colors and styling!
|
||||
|
|
Loading…
Reference in New Issue