Skip to content

Commit

Permalink
chore(catalog): Debug hook for responses
Browse files Browse the repository at this point in the history
This doesn't work for two reasons..

1. We need to use `async` to read the response body. This could be
solved by restructuring our client code generation so that we put these
functions into the same crate:

    error[E0658]: async closures are unstable
        --> catalog-api-v1/src/client.rs:1654:10
         |
    1654 |         (async |_, result: &reqwest::Result<reqwest::Response>| {
         |          ^^^^^
         |
         = note: see issue #62290 <rust-lang/rust#62290> for more information
         = help: to use an async block, remove the `||`: `async {`

2. Reading a response body takes ownership of the object because the
body is read from the wire and can't be re-read again:

    error[E0507]: cannot move out of `*response` which is behind a shared reference
        --> catalog-api-v1/src/client.rs:1663:36
         |
    1663 |             let mut body_content = response
         |                                    ^^^^^^^^ move occurs because `*response` has type `reqwest::Response`, which does not implement the `Copy` trait
    1664 |                 .text()
         |                  ------ `*response` moved due to this method call
         |
    note: `reqwest::Response::text` takes ownership of the receiver `self`, which moves `*response`
        --> /Users/dcarley/.cargo/registry/src/index.crates.io-6f17d22bba15001f/reqwest-0.11.27/src/async_impl/response.rs:147:23
         |
    147  |     pub async fn text(self) -> crate::Result<String> {
         |                       ^^^^

    error: lifetime may not live long enough
        --> catalog-api-v1/src/client.rs:1658:65
         |
    1658 |           (async |_, result: &reqwest::Result<reqwest::Response>| {
         |  ____________________________-__________________________________-_^
         | |                            |                                  |
         | |                            |                                  return type of closure `{async closure body@catalog-api-v1/src/client.rs:1658:65: 1676:10}` contains a lifetime `'2`
         | |                            let's call the lifetime of this reference `'1`
    1659 | |             let response = result.as_ref().unwrap();
    1660 | |             let status = response.status();
    1661 | |             let url = response.url().clone();
    ...    |
    1675 | |             );
    1676 | |         })(&self.inner, &result);
         | |_________^ returning this value requires that `'1` must outlive `'2`
  • Loading branch information
dcarley committed May 21, 2024
1 parent 8d3482e commit 1318768
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cli/catalog-api-v1/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ fn generate_client(spec: &OpenAPI) -> String {
tracing::debug!("catalog request: method={}, url={}, body={}", request.method(), request.url(), body_content);
}
});
settings.with_post_hook(quote! {
async |_, result: &reqwest::Result<reqwest::Response>| {
let response = result.as_ref().unwrap();
let status = response.status();
let url = response.url().clone();

let error_content = String::from("failed to parse body");
let mut body_content = response.text().await.unwrap_or(error_content.clone());
if let Ok(json_body) = serde_json::from_str::<serde_json::Value>(&body_content) {
body_content = serde_json::to_string_pretty(&json_body).unwrap_or(error_content);
}

tracing::debug!("catalog response: status={}, url={}, body={}", status, url, body_content);
}
});
let mut generator = progenitor::Generator::new(&settings);
let tokens = generator.generate_tokens(spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
Expand Down
76 changes: 76 additions & 0 deletions cli/catalog-api-v1/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,25 @@ Sends a `GET` request to `/api/v1/catalog/search`
);
})(&self.inner, &request);
let result = self.client.execute(request).await;
(async |_, result: &reqwest::Result<reqwest::Response>| {
let response = result.as_ref().unwrap();
let status = response.status();
let url = response.url().clone();
let error_content = String::from("failed to parse body");
let mut body_content = response
.text()
.await
.unwrap_or(error_content.clone());
if let Ok(json_body) = serde_json::from_str::<
serde_json::Value,
>(&body_content) {
body_content = serde_json::to_string_pretty(&json_body)
.unwrap_or(error_content);
}
tracing::debug!(
"catalog response: status={}, url={}, body={}", status, url, body_content
);
})(&self.inner, &result);
let response = result?;
match response.status().as_u16() {
200u16 => ResponseValue::from_response(response).await,
Expand Down Expand Up @@ -1725,6 +1744,25 @@ Sends a `GET` request to `/api/v1/catalog/packages/{attr_path}`
);
})(&self.inner, &request);
let result = self.client.execute(request).await;
(async |_, result: &reqwest::Result<reqwest::Response>| {
let response = result.as_ref().unwrap();
let status = response.status();
let url = response.url().clone();
let error_content = String::from("failed to parse body");
let mut body_content = response
.text()
.await
.unwrap_or(error_content.clone());
if let Ok(json_body) = serde_json::from_str::<
serde_json::Value,
>(&body_content) {
body_content = serde_json::to_string_pretty(&json_body)
.unwrap_or(error_content);
}
tracing::debug!(
"catalog response: status={}, url={}, body={}", status, url, body_content
);
})(&self.inner, &result);
let response = result?;
match response.status().as_u16() {
200u16 => ResponseValue::from_response(response).await,
Expand Down Expand Up @@ -1812,6 +1850,25 @@ Sends a `POST` request to `/api/v1/catalog/resolve`
);
})(&self.inner, &request);
let result = self.client.execute(request).await;
(async |_, result: &reqwest::Result<reqwest::Response>| {
let response = result.as_ref().unwrap();
let status = response.status();
let url = response.url().clone();
let error_content = String::from("failed to parse body");
let mut body_content = response
.text()
.await
.unwrap_or(error_content.clone());
if let Ok(json_body) = serde_json::from_str::<
serde_json::Value,
>(&body_content) {
body_content = serde_json::to_string_pretty(&json_body)
.unwrap_or(error_content);
}
tracing::debug!(
"catalog response: status={}, url={}, body={}", status, url, body_content
);
})(&self.inner, &result);
let response = result?;
match response.status().as_u16() {
200u16 => ResponseValue::from_response(response).await,
Expand Down Expand Up @@ -1868,6 +1925,25 @@ Sends a `GET` request to `/api/v1/metrics/status`
);
})(&self.inner, &request);
let result = self.client.execute(request).await;
(async |_, result: &reqwest::Result<reqwest::Response>| {
let response = result.as_ref().unwrap();
let status = response.status();
let url = response.url().clone();
let error_content = String::from("failed to parse body");
let mut body_content = response
.text()
.await
.unwrap_or(error_content.clone());
if let Ok(json_body) = serde_json::from_str::<
serde_json::Value,
>(&body_content) {
body_content = serde_json::to_string_pretty(&json_body)
.unwrap_or(error_content);
}
tracing::debug!(
"catalog response: status={}, url={}, body={}", status, url, body_content
);
})(&self.inner, &result);
let response = result?;
match response.status().as_u16() {
200u16 => ResponseValue::from_response(response).await,
Expand Down

0 comments on commit 1318768

Please sign in to comment.