Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add async post_hook function support #942

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions progenitor-client/src/progenitor_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ pub enum Error<E = ()> {

/// An error occurred in the processing of a request pre-hook.
PreHookError(String),

/// An error occurred in the processing of a request post-hook.
PostHookError(String),
}

impl<E> Error<E> {
Expand All @@ -266,6 +269,7 @@ impl<E> Error<E> {
match self {
Error::InvalidRequest(_) => None,
Error::PreHookError(_) => None,
Error::PostHookError(_) => None,
Error::CommunicationError(e) => e.status(),
Error::ErrorResponse(rv) => Some(rv.status()),
Error::InvalidUpgrade(e) => e.status(),
Expand All @@ -283,6 +287,7 @@ impl<E> Error<E> {
match self {
Error::InvalidRequest(s) => Error::InvalidRequest(s),
Error::PreHookError(s) => Error::PreHookError(s),
Error::PostHookError(s) => Error::PostHookError(s),
Error::CommunicationError(e) => Error::CommunicationError(e),
Error::ErrorResponse(ResponseValue {
inner: _,
Expand Down Expand Up @@ -346,6 +351,9 @@ where
Error::PreHookError(s) => {
write!(f, "Pre-hook Error: {}", s)
}
Error::PostHookError(s) => {
write!(f, "Post-hook Error: {}", s)
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions progenitor-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct GenerationSettings {
pre_hook: Option<TokenStream>,
pre_hook_async: Option<TokenStream>,
post_hook: Option<TokenStream>,
post_hook_async: Option<TokenStream>,
extra_derives: Vec<String>,

unknown_crates: UnknownPolicy,
Expand Down Expand Up @@ -152,6 +153,16 @@ impl GenerationSettings {
self
}

/// Hook invoked prior to receiving the HTTP response.
/// Any hook requires the inner type to be set via
/// [Self::with_inner_type], otherwise the code will not compile.
/// The signature for the post hook function should be
/// `post_hook_func(&InnerType, &Result<reqwest::Response,reqwest::Error>) -> ()`
pub fn with_post_hook_async(&mut self, post_hook_async: TokenStream) -> &mut Self {
self.post_hook_async = Some(post_hook_async);
self
}

/// Additional derive macros applied to generated types.
pub fn with_derive(&mut self, derive: impl ToString) -> &mut Self {
self.extra_derives.push(derive.to_string());
Expand Down
9 changes: 9 additions & 0 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,14 @@ impl Generator {
(#hook)(&#client.inner, &#result_ident);
}
});
let post_hook_async = self.settings.post_hook_async.as_ref().map(|hook| {
quote! {
match (#hook)(&#client.inner, &#result_ident).await {
Ok(_) => (),
Err(e) => return Err(Error::PostHookError(e.to_string())),
}
}
});

let method_func = format_ident!("{}", method.method.as_str());

Expand All @@ -1172,6 +1180,7 @@ impl Generator {
.execute(#request_ident)
.await;
#post_hook
#post_hook_async

let #response_ident = #result_ident?;

Expand Down