Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions crates/catalog/rest/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,39 @@ impl HttpClient {
.request(Method::POST, &self.token_endpoint)
.form(&params)
.build()?;
let auth_url = auth_req.url().clone();
let auth_resp = self.client.execute(auth_req).await?;

let auth_res: TokenResponse = if auth_resp.status().as_u16() == OK {
let text = auth_resp.bytes().await?;
let text = auth_resp
.bytes()
.await
.map_err(|err| err.with_url(auth_url.clone()))?;
Ok(serde_json::from_slice(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("operation", "auth")
.with_context("url", auth_url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?)
} else {
let code = auth_resp.status();
let text = auth_resp.bytes().await?;
let text = auth_resp
.bytes()
.await
.map_err(|err| err.with_url(auth_url.clone()))?;
let e: ErrorResponse = serde_json::from_slice(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("json", String::from_utf8_lossy(&text))
.with_context("code", code.to_string())
.with_context("operation", "auth")
.with_context("url", auth_url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?;
Err(Error::from(e))
Expand Down Expand Up @@ -193,28 +204,41 @@ impl HttpClient {
) -> Result<R> {
self.authenticate(&mut request).await?;

let method = request.method().clone();
let url = request.url().clone();

let resp = self.client.execute(request).await?;

if resp.status().as_u16() == SUCCESS_CODE {
let text = resp.bytes().await?;
let text = resp
.bytes()
.await
.map_err(|err| err.with_url(url.clone()))?;
Ok(serde_json::from_slice::<R>(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("method", method.to_string())
.with_context("url", url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?)
} else {
let code = resp.status();
let text = resp.bytes().await?;
let text = resp
.bytes()
.await
.map_err(|err| err.with_url(url.clone()))?;
let e = serde_json::from_slice::<E>(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("json", String::from_utf8_lossy(&text))
.with_context("code", code.to_string())
.with_context("method", method.to_string())
.with_context("url", url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?;
Err(e.into())
Expand All @@ -227,20 +251,28 @@ impl HttpClient {
) -> Result<()> {
self.authenticate(&mut request).await?;

let method = request.method().clone();
let url = request.url().clone();

let resp = self.client.execute(request).await?;

if resp.status().as_u16() == SUCCESS_CODE {
Ok(())
} else {
let code = resp.status();
let text = resp.bytes().await?;
let text = resp
.bytes()
.await
.map_err(|err| err.with_url(url.clone()))?;
let e = serde_json::from_slice::<E>(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("json", String::from_utf8_lossy(&text))
.with_context("code", code.to_string())
.with_context("method", method.to_string())
.with_context("url", url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?;
Err(e.into())
Expand All @@ -255,19 +287,27 @@ impl HttpClient {
) -> Result<R> {
self.authenticate(&mut request).await?;

let method = request.method().clone();
let url = request.url().clone();

let resp = self.client.execute(request).await?;

if let Some(ret) = handler(&resp) {
Ok(ret)
} else {
let code = resp.status();
let text = resp.bytes().await?;
let text = resp
.bytes()
.await
.map_err(|err| err.with_url(url.clone()))?;
let e = serde_json::from_slice::<E>(&text).map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"Failed to parse response from rest catalog server!",
)
.with_context("code", code.to_string())
.with_context("method", method.to_string())
.with_context("url", url.to_string())
.with_context("json", String::from_utf8_lossy(&text))
.with_source(e)
})?;
Expand Down
Loading