Skip to main content

awsim_core/
router.rs

1/// Context extracted from an incoming AWS API request.
2///
3/// Contains the account ID, region, service, and request metadata
4/// needed by service handlers to process the request.
5#[derive(Debug, Clone)]
6pub struct RequestContext {
7    /// AWS account ID (default: "000000000000" in bypass mode)
8    pub account_id: String,
9
10    /// AWS region (e.g., "us-east-1")
11    pub region: String,
12
13    /// Service name extracted from the request
14    pub service: String,
15
16    /// Access key ID (if present in Authorization header)
17    pub access_key: Option<String>,
18
19    /// Unique request ID for this API call
20    pub request_id: String,
21
22    /// HTTP method of the original request
23    pub method: String,
24
25    /// URI path of the original request
26    pub uri: String,
27
28    /// Internal event bus — present for requests routed through the gateway;
29    /// `None` in unit tests or any context where no bus was configured.
30    pub event_bus: Option<crate::events::EventBus>,
31}
32
33impl RequestContext {
34    pub fn new(service: impl Into<String>, region: impl Into<String>) -> Self {
35        Self {
36            account_id: "000000000000".to_string(),
37            region: region.into(),
38            service: service.into(),
39            access_key: None,
40            request_id: uuid::Uuid::new_v4().to_string(),
41            method: "POST".to_string(),
42            uri: "/".to_string(),
43            event_bus: None,
44        }
45    }
46
47    /// Like [`new`] but with an explicit account id — used by background
48    /// pollers that fan out across every (account, region) pair.
49    pub fn new_with_account(
50        service: impl Into<String>,
51        region: impl Into<String>,
52        account_id: impl Into<String>,
53    ) -> Self {
54        Self {
55            account_id: account_id.into(),
56            region: region.into(),
57            service: service.into(),
58            access_key: None,
59            request_id: uuid::Uuid::new_v4().to_string(),
60            method: "POST".to_string(),
61            uri: "/".to_string(),
62            event_bus: None,
63        }
64    }
65
66    /// Returns an ARN prefix for this account and region.
67    /// e.g., "arn:aws:s3:us-east-1:000000000000"
68    pub fn arn_prefix(&self, service: &str) -> String {
69        format!("arn:aws:{}:{}:{}", service, self.region, self.account_id)
70    }
71}