v0.2.11-pre.003-fix.001
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-offchain-transport-lib/src/http_client.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
//! Crate-wide hardened REST client used internally by off-chain capability adapters.
|
||||
|
||||
@@ -8,7 +8,7 @@ pub(crate) struct HttpJsonDocument {
|
||||
bytes: std::vec::Vec<u8>,
|
||||
}
|
||||
|
||||
impl HttpJsonDocument {
|
||||
impl crate::HttpJsonDocument {
|
||||
/// Returns the validated raw JSON bytes for provider-specific typed deserialization.
|
||||
#[must_use]
|
||||
pub(crate) fn as_bytes(&self) -> &[u8] {
|
||||
@@ -16,7 +16,7 @@ impl HttpJsonDocument {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for HttpJsonDocument {
|
||||
impl std::fmt::Debug for crate::HttpJsonDocument {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
return formatter.debug_struct("HttpJsonDocument").field("byte_len", &self.bytes.len()).finish();
|
||||
}
|
||||
@@ -30,7 +30,7 @@ pub(crate) struct HttpGetRequest {
|
||||
url: reqwest::Url,
|
||||
}
|
||||
|
||||
impl HttpGetRequest {
|
||||
impl crate::HttpGetRequest {
|
||||
/// Creates one HTTPS GET request from a crate-owned official provider URL.
|
||||
pub(crate) fn new_https(url: &'static str) -> ksp_core_lib::Result<Self> {
|
||||
return Self::parse(url, false);
|
||||
@@ -106,7 +106,7 @@ impl HttpGetRequest {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for HttpGetRequest {
|
||||
impl std::fmt::Debug for crate::HttpGetRequest {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
return formatter.write_str("HttpGetRequest(<redacted>)");
|
||||
}
|
||||
@@ -119,7 +119,7 @@ pub(crate) struct HttpRestClient {
|
||||
settings: crate::HttpClientSettings,
|
||||
}
|
||||
|
||||
impl HttpRestClient {
|
||||
impl crate::HttpRestClient {
|
||||
/// Builds one hardened client with redirects, system proxies and reqwest automatic retries disabled.
|
||||
pub(crate) fn new(settings: crate::HttpClientSettings) -> ksp_core_lib::Result<Self> {
|
||||
let client_result = reqwest::Client::builder()
|
||||
@@ -151,7 +151,12 @@ impl HttpRestClient {
|
||||
}
|
||||
|
||||
/// Executes one GET request and returns only a bounded syntactically valid JSON document.
|
||||
pub(crate) async fn get_json(&self, provider: &'static str, operation: &'static str, request: HttpGetRequest) -> ksp_core_lib::Result<HttpJsonDocument> {
|
||||
pub(crate) async fn get_json(
|
||||
&self,
|
||||
provider: &'static str,
|
||||
operation: &'static str,
|
||||
request: crate::HttpGetRequest,
|
||||
) -> ksp_core_lib::Result<crate::HttpJsonDocument> {
|
||||
let send_result = self.client.get(request.url).headers(request.headers).send().await;
|
||||
let mut response = match send_result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
@@ -198,7 +203,7 @@ impl HttpRestClient {
|
||||
response_body_bytes = body.len(),
|
||||
"completed off-chain HTTP REST request"
|
||||
);
|
||||
return std::result::Result::Ok(HttpJsonDocument { bytes: body });
|
||||
return std::result::Result::Ok(crate::HttpJsonDocument { bytes: body });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +212,7 @@ fn classify_http_status(
|
||||
operation: &'static str,
|
||||
status: u16,
|
||||
retry_after: std::option::Option<std::time::Duration>,
|
||||
) -> ksp_core_lib::Result<HttpJsonDocument> {
|
||||
) -> ksp_core_lib::Result<crate::HttpJsonDocument> {
|
||||
if status == 401 || status == 403 {
|
||||
return std::result::Result::Err(http_status_error(
|
||||
crate::ERROR_CODE_HTTP_ACCESS_DENIED,
|
||||
@@ -303,7 +308,7 @@ fn parse_retry_after(headers: &reqwest::header::HeaderMap) -> std::option::Optio
|
||||
return std::option::Option::Some(std::cmp::min(std::time::Duration::from_secs(seconds), crate::HTTP_MAX_RETRY_AFTER));
|
||||
}
|
||||
|
||||
fn response_too_large(provider: &'static str, operation: &'static str, limit: usize) -> ksp_core_lib::Result<HttpJsonDocument> {
|
||||
fn response_too_large(provider: &'static str, operation: &'static str, limit: usize) -> ksp_core_lib::Result<crate::HttpJsonDocument> {
|
||||
return std::result::Result::Err(
|
||||
ksp_core_lib::Error::new(crate::ERROR_CODE_HTTP_RESPONSE_TOO_LARGE, "Off-chain provider response exceeded the configured body limit")
|
||||
.with_context("provider", provider)
|
||||
|
||||
Reference in New Issue
Block a user