grafbase_sdk/types/
context.rs

1use crate::{SdkError, types::Token, wit};
2
3/// Context available after the [on_request()](crate::HooksExtension::on_request()) hook.
4pub struct RequestContext(wit::RequestContext);
5
6impl From<wit::RequestContext> for RequestContext {
7    fn from(context: wit::RequestContext) -> Self {
8        Self(context)
9    }
10}
11
12impl RequestContext {
13    /// Returns the Hook context created by the [on_request()](crate::HooksExtension::on_request())
14    /// hook if any.
15    pub fn hooks_context(&self) -> Vec<u8> {
16        self.0.hooks_context()
17    }
18}
19
20/// Context available after the [authenticate()](crate::AuthenticationExtension::authenticate())
21pub struct AuthenticatedRequestContext(wit::AuthenticatedRequestContext);
22
23impl From<wit::AuthenticatedRequestContext> for AuthenticatedRequestContext {
24    fn from(context: wit::AuthenticatedRequestContext) -> Self {
25        Self(context)
26    }
27}
28
29impl AuthenticatedRequestContext {
30    /// Returns the Hook context created by the [on_request()](crate::HooksExtension::on_request())
31    /// hook if any.
32    pub fn hooks_context(&self) -> Vec<u8> {
33        self.0.hooks_context()
34    }
35    /// Returns the authentication token provided by an authentication extension if any.
36    pub fn token(&self) -> Token {
37        self.0.token().into()
38    }
39}
40
41/// Context available after the [authorize_query()](crate::AuthorizationExtension::authorize_query())
42pub struct AuthorizedOperationContext(wit::AuthorizedOperationContext);
43
44impl From<wit::AuthorizedOperationContext> for AuthorizedOperationContext {
45    fn from(context: wit::AuthorizedOperationContext) -> Self {
46        Self(context)
47    }
48}
49
50impl AuthorizedOperationContext {
51    /// Returns the Hook context created by the [on_request()](crate::HooksExtension::on_request())
52    /// hook if any.
53    pub fn hooks_context(&self) -> Vec<u8> {
54        self.0.hooks_context()
55    }
56    /// Returns the authentication token provided by an authentication extension if any.
57    pub fn token(&self) -> Token {
58        self.0.token().into()
59    }
60
61    /// Retrieve the current authorization context if any.
62    /// This method will fail if there is more one authorization context, from different extensions.
63    pub fn authorization_context(&self) -> Result<Vec<u8>, SdkError> {
64        self.0.authorization_context(None).map_err(Into::into)
65    }
66
67    /// Retrieve the current authorization state for a given extension.
68    /// The key must match the one used in the configuration.
69    /// Fails if the key doesn't point to an authorization extension.
70    ///
71    /// Use [authorization_context()](AuthorizedOperationContext::authorization_context()) if you have only one
72    /// authorization extension returning a non-empty state.
73    pub fn authorization_context_by_key(&self, key: &str) -> Result<Vec<u8>, SdkError> {
74        self.0.authorization_context(Some(key)).map_err(Into::into)
75    }
76}