Watch
0
0
Fork
You've already forked hyperhive
0

add shared forge-client crate; hive-forge and hive-forge-notify use it

This commit is contained in:
atlas 2026-11-15 19:49:02 +01:00 • committed by mara
commit e59e41ec90

View file

@ -9,26 +9,20 @@
use std::path::PathBuf;
use anyhow::{Context, Result, anyhow, bail};
use forgejo_api::{Auth, ForgejoError};
use forge_client::{StaticToken, TimeoutPolicy};
use forgejo_api::ForgejoError;
use reqwest::blocking::{Client as HttpClient, Response};
use reqwest::header::{
ACCEPT, AUTHORIZATION, CONTENT_DISPOSITION, CONTENT_TYPE, HeaderMap, HeaderValue,
};
use reqwest::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Bound on reaching the forge.
const HTTP_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
/// Whole-request budget for a small JSON call (an `/api/v1` GET, or the
/// run-view log streamer's snapshot POST).
const JSON_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);
/// Whole-request budget for a GET that downloads a file body — an
/// attachment, an Actions artifact zip, or a persisted job log. Sized
/// for a large-but-live transfer rather than a JSON call:
/// `reqwest::blocking` has no separate read/idle timeout, so this bounds
/// the whole download rather than cutting it off partway through like
/// [`JSON_TIMEOUT`] would.
pub(crate) const DOWNLOAD_TIMEOUT: std::time::Duration = std::time::Duration::from_mins(10);
/// The CLI's timeout policy: connect bound, JSON-call budget (an `/api/v1`
/// GET, or the run-view log streamer's snapshot POST) and download budget
/// (an attachment, an Actions artifact zip, or a persisted job log; it bounds
/// the whole transfer because `reqwest::blocking` has no idle timeout).
const TIMEOUTS: TimeoutPolicy = TimeoutPolicy::CLI;
const JSON_TIMEOUT: std::time::Duration = TIMEOUTS.json;
pub(crate) const DOWNLOAD_TIMEOUT: std::time::Duration = TIMEOUTS.download;
/// Forgejo client pair: the typed `/api/v1` client plus a raw
/// `reqwest` client for web-router-only routes.
@ -94,20 +88,9 @@ impl Client {
.or_else(|| std::env::var("HIVE_FORGE_REPO").ok());
let url = url::Url::parse(&base).with_context(|| format!("parse HIVE_FORGE_URL {base}"))?;
let api = forgejo_api::sync::Forgejo::new(Auth::Token(&token), url)
.context("build forgejo client")?;
let mut headers = HeaderMap::new();
let auth = format!("token {token}");
let mut auth_val = HeaderValue::from_str(&auth).context("auth header")?;
auth_val.set_sensitive(true);
headers.insert(AUTHORIZATION, auth_val);
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
let web = HttpClient::builder()
.default_headers(headers)
.connect_timeout(HTTP_CONNECT_TIMEOUT)
.build()
.context("build reqwest client")?;
let token = StaticToken::new(token);
let api = forge_client::sync_client(url, &token).context("build forgejo client")?;
let web = forge_client::raw_blocking_client(&token, &TIMEOUTS)?;
Ok(Self {
api,
@ -576,7 +559,8 @@ impl std::error::Error for WebStatusError {}
/// `op` failed with `e`; a timeout names the bound that fired.
fn http_context(op: &str, e: &reqwest::Error, budget: std::time::Duration) -> String {
if e.is_connect() && e.is_timeout() {
format!("{op}: connect timed out after {HTTP_CONNECT_TIMEOUT:?}")
let connect = TIMEOUTS.connect.unwrap_or_default();
format!("{op}: connect timed out after {connect:?}")
} else if e.is_timeout() {
format!("{op}: timed out after {budget:?}")
} else {