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 app ip extractor #30

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion kanin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kanin"
version = "0.25.0"
version = "0.25.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The minor version should be incremented as a feature is added.

edition = "2021"
authors = ["Victor Nordam Suadicani <[email protected]>"]
description = "An RPC microservice framework for AMQP, protobuf and Rust built on lapin (https://github.com/amqp-rs/lapin)."
Expand Down
2 changes: 2 additions & 0 deletions kanin/src/extract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Interface for types that can extract themselves from requests.

mod app_id;
mod message;
mod req_id;
mod state;
Expand All @@ -11,6 +12,7 @@ use lapin::{acker::Acker, message::Delivery, Channel};

use crate::{error::HandlerError, Request};

pub use app_id::AppId;
pub use message::Msg;
pub use req_id::ReqId;
pub use state::State;
Expand Down
22 changes: 22 additions & 0 deletions kanin/src/extract/app_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! App IDs defined in the request.

use std::convert::Infallible;

use async_trait::async_trait;

use crate::{Extract, Request};

/// App ID extracted from the properties of the incoming request. Notice that this is already
/// logged as part of handling the request.
#[derive(Debug, Clone)]
pub struct AppId(pub Option<String>);

#[async_trait]
impl Extract for AppId {
type Error = Infallible;

async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
let app_id = req.app_id().map(|app_id| app_id.to_string());
Ok(Self(app_id))
}
}
15 changes: 14 additions & 1 deletion kanin/src/tests/send_recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing::info;

use crate::{
error::FromError,
extract::{ReqId, State},
extract::{AppId, ReqId, State},
tests::init_logging,
App, Extract, HandlerError, Request, Respond,
};
Expand Down Expand Up @@ -80,6 +80,12 @@ async fn handler_req_id(req_id: ReqId) -> MyResponse {
MyResponse("handler_req_id".into())
}

async fn handler_app_id(AppId(app_id): AppId) -> MyResponse {
assert_eq!(app_id.unwrap(), "my_app_id");
SYNC.get().unwrap().send(()).await.unwrap();
MyResponse("handler_app_id".into())
}

async fn handler_two_extractors(_channel: Channel, _delivery: Delivery) -> MyResponse {
SYNC.get().unwrap().send(()).await.unwrap();
MyResponse("handler_two_extractors".into())
Expand Down Expand Up @@ -121,6 +127,7 @@ async fn it_receives_various_messages_and_works_as_expected() {
.handler("handler_channel", handler_channel)
.handler("handler_delivery", handler_delivery)
.handler("handler_req_id", handler_req_id)
.handler("handler_app_id", handler_app_id)
.handler("handler_two_extractors", handler_two_extractors)
.handler("handler_state_extractor", handler_state_extractor)
.handler("listener", listener)
Expand Down Expand Up @@ -156,6 +163,7 @@ async fn it_receives_various_messages_and_works_as_expected() {
&[],
BasicProperties::default()
.with_reply_to(reply_to.into())
.with_app_id("my_app_id".into())
.with_headers(headers),
)
.await
Expand Down Expand Up @@ -183,6 +191,9 @@ async fn it_receives_various_messages_and_works_as_expected() {
send_msg("handler_req_id", "handler_message_reply_to").await;
recv.recv().await.unwrap();
recv.recv().await.unwrap();
send_msg("handler_app_id", "handler_message_reply_to").await;
recv.recv().await.unwrap();
recv.recv().await.unwrap();
send_msg("handler_two_extractors", "handler_message_reply_to").await;
recv.recv().await.unwrap();
recv.recv().await.unwrap();
Expand Down Expand Up @@ -236,6 +247,7 @@ async fn it_receives_various_messages_and_works_as_expected() {
// "handler_channel",
// "handler_delivery",
// "handler_req_id",
// "handler_app_id",
// "handler_two_extractors",
"handler_state_extractor",
"listener",
Expand All @@ -253,6 +265,7 @@ async fn it_receives_various_messages_and_works_as_expected() {
"handler_message",
"handler_message",
"handler_message",
"handler_message",
"shutdown",
],
recv_calls.as_ref()
Expand Down
Loading