1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
use crate::error::Error;
use crate::result::Result;
pub async fn get(url: impl Into<String>) -> Result<String> {
let url = url.into();
let resp = reqwest::get(&url).await?;
let status = resp.status();
let text = resp.text().await?;
if status.is_success() {
Ok(text)
} else {
Err(Error::Custom(format!("{}: {}", status, text)))
}
}
pub async fn get_json<T: serde::de::DeserializeOwned>(url: impl Into<String>) -> Result<T> {
let url = url.into();
let resp = reqwest::get(&url).await?;
let status = resp.status();
let text = resp.text().await?;
if status.is_success() {
Ok(serde_json::from_str(&text)?)
} else {
Err(Error::Custom(format!("{}: {}", status, text)))
}
}
pub struct Request {
pub url: String,
pub user_agent: Option<String>,
}
impl Request {
pub fn new(url: impl Into<String>) -> Self {
Self {
url: url.into(),
user_agent: None,
}
}
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = Some(user_agent.into());
self
}
pub async fn get(self) -> Result<String> {
let mut req = reqwest::Client::new().get(&self.url);
if let Some(user_agent) = self.user_agent {
req = req.header("User-Agent", user_agent);
}
let resp = req.send().await?;
let status = resp.status();
let text = resp.text().await?;
if status.is_success() {
Ok(text)
} else {
Err(Error::Custom(format!("{}: {}", status, text)))
}
}
pub async fn get_json<T: serde::de::DeserializeOwned>(self) -> Result<T> {
let mut req = reqwest::Client::new().get(&self.url);
if let Some(user_agent) = self.user_agent {
req = req.header("User-Agent", user_agent);
}
let resp = req.send().await?;
let status = resp.status();
let text = resp.text().await?;
if status.is_success() {
Ok(serde_json::from_str(&text)?)
} else {
Err(Error::Custom(format!("{}: {}", status, text)))
}
}
}
// pub async fn get_json_with_user_agent<T: serde::de::DeserializeOwned>(url: impl Into<String>, user_agent: impl Into<String>) -> Result<T> {
// let url = url.into();
// let resp = reqwest::Client::builder()
// .user_agent(user_agent.into())
// .build()?
// .get(url)
// .send()
// .await?;
// let status = resp.status();
// let text = resp.text().await?;
// if status.is_success() {
// Ok(serde_json::from_str(&text)?)
// } else {
// Err(Error::Custom(format!("{}: {}", status, text)))
// }
// }
// pub async fn post(url : impl Into<String>, body : impl Into<String>) -> Result<String> {
// let url = url.into();
// let body = body.into();
// let resp = reqwest::Client::new().post(&url).body(body).send().await?;
// let status = resp.status();
// let text = resp.text().await?;
// if status.is_success() {
// Ok(text)
// } else {
// Err(Error::Custom(format!("{}: {}", status, text)))
// }
// }
// pub async fn post_json(url : impl Into<String>, body : impl Into<String>) -> Result<String> {
// let url = url.into();
// let body = body.into();
// let resp = reqwest::Client::new().post(&url).body(body).header("Content-Type", "application/json").send().await?;
// let status = resp.status();
// let text = resp.text().await?;
// if status.is_success() {
// Ok(text)
// } else {
// Err(Error::Custom(format!("{}: {}", status, text)))
// }
// }
// pub async fn post_json_with_auth(url : impl Into<String>, body : impl Into<String>, auth : impl Into<String>) -> Result<String> {
// let url = url.into();
// let body = body.into();
// let auth = auth.into();
// let resp = reqwest::Client::new().post(&url).body(body).header("Content-Type", "application/json").header("Authorization", auth).send().await?;
// let status = resp.status();
// let text = resp.text().await?;
// if status.is_success() {
// Ok(text)
// } else {
// Err(Error::Custom(format!("{}: {}", status, text)))
// }
// }