Skip to content
Open
Show file tree
Hide file tree
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
53 changes: 43 additions & 10 deletions generator/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,14 @@ impl Client {
}
}

async fn request<Out>(
async fn request_raw(
&self,
method: http::Method,
uri: &str,
message: Message,
media_type: crate::utils::MediaType,
authentication: crate::auth::AuthenticationConstraint,
) -> ClientResult<(Option<crate::utils::NextLink>, Out)>
where
Out: serde::de::DeserializeOwned + 'static + Send,
{
) -> ClientResult<reqwest::Response> {
#[cfg(feature = "httpcache")]
let uri2 = uri.to_string();

Expand All @@ -231,10 +228,7 @@ impl Client {
}

req = req.header(http::header::USER_AGENT, &*instance.agent);
req = req.header(
http::header::ACCEPT,
&media_type.to_string()
);
req = req.header(http::header::ACCEPT, &media_type.to_string());

if let Some(auth_str) = auth {
req = req.header(http::header::AUTHORIZATION, &*auth_str);
Expand All @@ -243,7 +237,46 @@ impl Client {
if let Some(body) = message.body {
req = req.body(body);
}
let response = req.send().await?;
let resp = req.send().await?;
Ok(resp)
}

async fn get_header(&self, uri: &str, header_name: &reqwest::header::HeaderName) -> ClientResult<String> {
let resp = self
.request_raw(
http::Method::GET,
uri,
crate::Message {
body: None,
content_type: None,
},
crate::utils::MediaType::Json,
crate::auth::AuthenticationConstraint::Unconstrained,
)
.await?;
let location = resp
.headers()
.get(header_name)
.ok_or(ClientError::HttpError{status: resp.status(), error: format!("Missing {} header", header_name)})?
.to_str()?
.to_string();
Ok(location)
}

async fn request<Out>(
&self,
method: http::Method,
uri: &str,
message: Message,
media_type: crate::utils::MediaType,
authentication: crate::auth::AuthenticationConstraint,
) -> ClientResult<(Option<crate::utils::NextLink>, Out)>
where
Out: serde::de::DeserializeOwned + 'static + Send,
{
let response = self
.request_raw(method, uri, message, media_type, authentication)
.await?;

#[cfg(feature = "httpcache")]
let instance2 = <&Client>::clone(&self);
Expand Down
16 changes: 16 additions & 0 deletions generator/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ pub fn generate_files(
response_type = "String".to_string();
}

// If we did not find a content type ID, we may need to return a header
// Check if the response is a header and return the first one.
// Default to string.
if proper_name == "GitHub" && tid == TypeId(0) {
let response = o.responses.responses.first().unwrap();
if let Ok(i) = response.1.item() {
let header = i.headers.first();
if let Some((header_name, _value)) = header {
fn_inner = format!(
r#"let header_name = reqwest::header::HeaderName::try_from("{header_name}")?;
self.client.get_header(&url, &header_name).await"#
);
response_type = "String".to_string();
}
}
}
if let Some(te) = ts.id_to_entry.get(&tid) {
// If we have a one of, we can generate a few different subfunctions to
// help as well.
Expand Down
8 changes: 7 additions & 1 deletion generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,7 +2387,10 @@ pub enum ClientError {"#);
#[error("Rate limited for the next {duration} seconds")]
RateLimited{
duration: u64,
},"#);
},
/// str convertion error HeaderValue to str
#[error(transparent)]
ToStrError(#[from] reqwest::header::ToStrError),"#);
}
TemplateType::GenericApiKey | TemplateType::GenericClientCredentials => {
a(r#"/// utf8 convertion error
Expand Down Expand Up @@ -2423,6 +2426,9 @@ pub enum ClientError {"#);
ReqwestError(#[from] reqwest::Error),
/// Errors returned by reqwest::header
#[error(transparent)]
InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName),
/// Errors returned by reqwest::header
#[error(transparent)]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
/// Errors returned by reqwest middleware
#[error(transparent)]
Expand Down