|
| 1 | +use async_trait::async_trait; |
| 2 | +use http::StatusCode; |
| 3 | +use mockall::automock; |
| 4 | +use url::Url; |
| 5 | + |
| 6 | +#[derive(Debug, thiserror::Error)] |
| 7 | +pub enum DocsRsError { |
| 8 | + /// The rebuild couldn't be triggered. |
| 9 | + /// The reason is passed in the given error message. |
| 10 | + #[error("Bad request: {0}")] |
| 11 | + BadRequest(String), |
| 12 | + /// The request was rate limited by the server. |
| 13 | + /// This is the NGINX level rate limit for requests coming from a single IP. |
| 14 | + /// This is _not_ the rate limit that docs.rs might apply for rebuilds of the same crate |
| 15 | + /// (AKA: "rebuild too often"). |
| 16 | + #[error("rate limited")] |
| 17 | + RateLimited, |
| 18 | + #[error(transparent)] |
| 19 | + Permission(anyhow::Error), |
| 20 | + /// crate or version not found on docs.rs. |
| 21 | + /// This can be temporary directly after a release until the docs.rs registry watcher |
| 22 | + /// queued the build for the release. |
| 23 | + #[error("crate or version not found on docs.rs")] |
| 24 | + NotFound, |
| 25 | + #[error(transparent)] |
| 26 | + Other(anyhow::Error), |
| 27 | +} |
| 28 | + |
| 29 | +#[automock] |
| 30 | +#[async_trait] |
| 31 | +pub trait DocsRsClient: Send + Sync { |
| 32 | + async fn rebuild_docs(&self, name: &str, version: &str) -> Result<(), DocsRsError>; |
| 33 | +} |
| 34 | + |
| 35 | +pub(crate) struct RealDocsRsClient { |
| 36 | + client: reqwest::Client, |
| 37 | + base_url: Url, |
| 38 | + api_token: String, |
| 39 | +} |
| 40 | + |
| 41 | +impl RealDocsRsClient { |
| 42 | + pub fn new(base_url: impl Into<Url>, api_token: impl Into<String>) -> Self { |
| 43 | + Self { |
| 44 | + client: reqwest::Client::new(), |
| 45 | + base_url: base_url.into(), |
| 46 | + api_token: api_token.into(), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[async_trait] |
| 52 | +impl DocsRsClient for RealDocsRsClient { |
| 53 | + async fn rebuild_docs(&self, name: &str, version: &str) -> Result<(), DocsRsError> { |
| 54 | + let target_url = self |
| 55 | + .base_url |
| 56 | + .join(&format!("/crate/{name}/{version}/rebuild")) |
| 57 | + .map_err(|err| DocsRsError::Other(err.into()))?; |
| 58 | + |
| 59 | + let response = self |
| 60 | + .client |
| 61 | + .post(target_url) |
| 62 | + .bearer_auth(&self.api_token) |
| 63 | + .send() |
| 64 | + .await |
| 65 | + .map_err(|err| DocsRsError::Other(err.into()))?; |
| 66 | + |
| 67 | + match response.status() { |
| 68 | + StatusCode::CREATED => Ok(()), |
| 69 | + StatusCode::NOT_FOUND => Err(DocsRsError::NotFound), |
| 70 | + StatusCode::TOO_MANY_REQUESTS => Err(DocsRsError::RateLimited), |
| 71 | + StatusCode::BAD_REQUEST => { |
| 72 | + #[derive(Deserialize)] |
| 73 | + struct BadRequestResponse { |
| 74 | + message: String, |
| 75 | + } |
| 76 | + |
| 77 | + let error_response: BadRequestResponse = response |
| 78 | + .json() |
| 79 | + .await |
| 80 | + .map_err(|err| DocsRsError::Other(err.into()))?; |
| 81 | + |
| 82 | + Err(DocsRsError::BadRequest(error_response.message)) |
| 83 | + } |
| 84 | + _ => Err(DocsRsError::Other(anyhow::anyhow!( |
| 85 | + "Unexpected response from docs.rs: {}\n{}", |
| 86 | + response.status(), |
| 87 | + response.text().await.unwrap_or_default() |
| 88 | + ))), |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// Builds an [DocsRsClient] implementation based on the [config::Server] |
| 94 | +pub fn docs_rs_client(config: &crate::config::Server) -> Box<dyn DocsRsClient + Send + Sync> { |
| 95 | + if let Some(api_token) = &config.docs_rs_api_token { |
| 96 | + Box::new(RealDocsRsClient::new(config.docs_rs_url.clone(), api_token)) |
| 97 | + } else { |
| 98 | + Box::new(MockDocsRsClient::new()) |
| 99 | + } |
| 100 | +} |
0 commit comments