Skip to content

Commit

Permalink
refactor(ureq): update to ureq v3 (#247)
Browse files Browse the repository at this point in the history
* refactor(ureq): update to ureq v3

* build: update Cargo.lock for ureq=3

* fix(ureq): handle json correctly
  • Loading branch information
EdJoPaTo authored Feb 1, 2025
1 parent 9563291 commit cc8bf15
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 54 deletions.
49 changes: 25 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ features = ["fs"]
optional = true

[dependencies.ureq]
version = "2"
version = "3.0.0"
default-features = false
features = ["rustls"]
optional = true

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ fn main() {
}

fn custom_client() -> Api {
let request_agent = frankenstein::ureq::builder()
.timeout(Duration::from_secs(100))
let config = frankenstein::ureq::Agent::config_builder()
.http_status_as_error(false)
.timeout_global(Some(Duration::from_secs(100)))
.build();
let request_agent = frankenstein::ureq::Agent::new_with_config(config);
let api_url = format!("{BASE_API_URL}{TOKEN}");

Api::builder()
Expand Down
54 changes: 31 additions & 23 deletions src/client_ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ pub struct Api {
#[builder(into)]
pub api_url: String,

#[builder(default = ureq::builder().timeout(Duration::from_secs(500)).build())]
#[builder(default = default_agent())]
pub request_agent: ureq::Agent,
}

fn default_agent() -> ureq::Agent {
ureq::Agent::new_with_config(
ureq::config::Config::builder()
.http_status_as_error(false)
.timeout_global(Some(Duration::from_secs(500)))
.build(),
)
}

impl Api {
/// Create a new `Api`. You can use [`Api::new_url`] or [`Api::builder`] for more options.
pub fn new(api_key: &str) -> Self {
Expand All @@ -31,21 +40,18 @@ impl Api {
}

fn decode_response<Output>(
response: Result<ureq::Response, ureq::Error>,
response: ureq::http::response::Response<ureq::Body>,
) -> Result<Output, Error>
where
Output: serde::de::DeserializeOwned,
{
match response {
Ok(response) => {
let message = response.into_string().map_err(Error::DecodeUreqBody)?;
crate::json::decode(&message)
}
Err(ureq::Error::Status(_code, response)) => {
let message = response.into_string().map_err(Error::DecodeUreqBody)?;
Err(Error::Api(crate::json::decode(&message)?))
}
Err(ureq::Error::Transport(error)) => Err(Error::HttpUreq(error)),
let success = response.status().is_success();
let body = response.into_body().read_to_string()?;
if success {
crate::json::decode(&body)
} else {
let api_error = crate::json::decode(&body)?;
Err(Error::Api(api_error))
}
}
}
Expand All @@ -59,15 +65,17 @@ impl TelegramApi for Api {
Output: serde::de::DeserializeOwned,
{
let url = format!("{}/{method}", self.api_url);
let prepared_request = self
.request_agent
.post(&url)
.set("Content-Type", "application/json");
let request = self.request_agent.post(&url);
let response = match params {
None => prepared_request.call(),
None => request.send_empty()?,
Some(data) => {
let json = crate::json::encode(&data)?;
prepared_request.send_string(&json)
request
.header(
ureq::http::header::CONTENT_TYPE,
ureq::http::HeaderValue::from_static("application/json; charset=utf-8"),
)
.send(&json)?
}
};
Self::decode_response(response)
Expand Down Expand Up @@ -115,15 +123,15 @@ impl TelegramApi for Api {
}

let url = format!("{}/{method}", self.api_url);
let form_data = form.prepare().unwrap();
let mut form_data = form.prepare().unwrap();
let response = self
.request_agent
.post(&url)
.set(
"Content-Type",
&format!("multipart/form-data; boundary={}", form_data.boundary()),
.header(
ureq::http::header::CONTENT_TYPE,
format!("multipart/form-data; boundary={}", form_data.boundary()),
)
.send(form_data);
.send(ureq::SendBody::from_reader(&mut form_data))?;
Self::decode_response(response)
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ pub enum Error {

#[cfg(feature = "client-ureq")]
#[error("HTTP error: {0}")]
HttpUreq(#[source] ureq::Transport),
#[cfg(feature = "client-ureq")]
#[error("Decode Body Error: {0}")]
DecodeUreqBody(#[source] std::io::Error),
HttpUreq(#[from] ureq::Error),
}

impl Error {
Expand Down

0 comments on commit cc8bf15

Please sign in to comment.