diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-12-14 12:05:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-14 12:05:59 +0100 |
commit | 19d52b9a55a6d5a67f27cbcc6cbe9c6c15865d63 (patch) | |
tree | 8d0675b2dc2fb54c224102a71add31440de75cef /cli/tools/registry/api.rs | |
parent | f2c56dc3d82fbce872c1fc8d014604ad804a1cb7 (diff) |
refactor: split registry into multiple modules (#21572)
Co-authored-by: David Sherret <dsherret@gmail.com>
Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'cli/tools/registry/api.rs')
-rw-r--r-- | cli/tools/registry/api.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/cli/tools/registry/api.rs b/cli/tools/registry/api.rs new file mode 100644 index 000000000..b174ea367 --- /dev/null +++ b/cli/tools/registry/api.rs @@ -0,0 +1,110 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use deno_core::serde_json; +use deno_runtime::deno_fetch::reqwest; +use serde::de::DeserializeOwned; + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CreateAuthorizationResponse { + pub verification_url: String, + pub code: String, + pub exchange_token: String, + pub poll_interval: u64, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ExchangeAuthorizationResponse { + pub token: String, + pub user: User, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct User { + pub name: String, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct OidcTokenResponse { + pub value: String, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PublishingTaskError { + pub code: String, + pub message: String, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PublishingTask { + pub id: String, + pub status: String, + pub error: Option<PublishingTaskError>, +} + +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ApiError { + pub code: String, + pub message: String, + #[serde(skip)] + pub x_deno_ray: Option<String>, +} + +impl std::fmt::Display for ApiError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} ({})", self.message, self.code)?; + if let Some(x_deno_ray) = &self.x_deno_ray { + write!(f, "[x-deno-ray: {}]", x_deno_ray)?; + } + Ok(()) + } +} + +impl std::fmt::Debug for ApiError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self, f) + } +} + +impl std::error::Error for ApiError {} + +pub async fn parse_response<T: DeserializeOwned>( + response: reqwest::Response, +) -> Result<T, ApiError> { + let status = response.status(); + let x_deno_ray = response + .headers() + .get("x-deno-ray") + .and_then(|value| value.to_str().ok()) + .map(|s| s.to_string()); + let text = response.text().await.unwrap(); + + if !status.is_success() { + match serde_json::from_str::<ApiError>(&text) { + Ok(mut err) => { + err.x_deno_ray = x_deno_ray; + return Err(err); + } + Err(_) => { + let err = ApiError { + code: "unknown".to_string(), + message: format!("{}: {}", status, text), + x_deno_ray, + }; + return Err(err); + } + } + } + + serde_json::from_str(&text).map_err(|err| ApiError { + code: "unknown".to_string(), + message: format!("Failed to parse response: {}, response: '{}'", err, text), + x_deno_ray, + }) +} |