ffsend_api/api/
request.rs1use reqwest::{blocking::Response, StatusCode};
2use thiserror::Error;
3
4use crate::config::{HTTP_STATUS_EXPIRED, HTTP_STATUS_UNAUTHORIZED};
5use crate::ext::status_code::StatusCodeExt;
6
7pub fn ensure_success(response: &Response) -> Result<(), ResponseError> {
9 let status = response.status();
11
12 if status.is_success() {
14 return Ok(());
15 }
16
17 if status == HTTP_STATUS_EXPIRED {
19 return Err(ResponseError::Expired);
20 }
21
22 if status == HTTP_STATUS_UNAUTHORIZED {
24 return Err(ResponseError::Unauthorized);
25 }
26
27 Err(ResponseError::OtherHttp(status, status.err_text()))
29}
30
31#[derive(Error, Debug)]
32pub enum ResponseError {
33 #[error("this file has expired or did never exist")]
35 Expired,
36
37 #[error("unauthorized, are the credentials correct?")]
40 Unauthorized,
41
42 #[error("bad HTTP response: {}", _1)]
44 OtherHttp(StatusCode, String),
45
46 #[error("server responded with undefined error")]
48 Undefined,
49}