pub struct MessageTab { /* private fields */ }Expand description
A tab displayed next to Headers, Cookies, Query, Body and Raw in the request or response section of the Inspector.
The content handler receives a SingleEntryContext and returns the
TabContent to render. A tab is visible for every entry unless
visible_when narrows it down.
use base64::prelude::{BASE64_URL_SAFE_NO_PAD, Engine};
use powhttp_sdk::{MessageTab, TabContent, ExtensionHandle, SingleEntryContext};
use powhttp_sdk::sessions::SessionEntry;
fn bearer_token(entry: &SessionEntry) -> Option<&str> {
entry.request.headers.get("authorization")?.strip_prefix("Bearer ")
}
fn decode_jwt_payload(token: &str) -> Option<String> {
let payload = token.split('.').nth(1)?;
let bytes = BASE64_URL_SAFE_NO_PAD.decode(payload).ok()?;
String::from_utf8(bytes).ok()
}
let tab = MessageTab::new(
"jwt",
"JWT",
async |ctx: SingleEntryContext, handle: ExtensionHandle| {
let entry = handle.get_session_entry(ctx.session_id, ctx.entry_id).await?;
let payload = entry
.as_ref()
.and_then(bearer_token)
.and_then(decode_jwt_payload)
.unwrap_or_default();
Ok(TabContent::json(payload))
},
)
.visible_when(async |ctx: SingleEntryContext, handle: ExtensionHandle| {
let entry = handle.get_session_entry(ctx.session_id, ctx.entry_id).await?;
Ok(entry.as_ref().and_then(bearer_token).is_some())
});Implementations§
Source§impl MessageTab
impl MessageTab
Sourcepub fn new<F, Fut>(
id: impl Into<String>,
label: impl Into<String>,
content: F,
) -> Selfwhere
F: Fn(SingleEntryContext, ExtensionHandle) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<TabContent, Error>> + Send + 'static,
pub fn new<F, Fut>(
id: impl Into<String>,
label: impl Into<String>,
content: F,
) -> Selfwhere
F: Fn(SingleEntryContext, ExtensionHandle) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<TabContent, Error>> + Send + 'static,
Creates a new tab with the given id, display label and async content handler.
Sourcepub fn visible_when<F, Fut>(self, visibility: F) -> Selfwhere
F: Fn(SingleEntryContext, ExtensionHandle) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<bool, Error>> + Send + 'static,
pub fn visible_when<F, Fut>(self, visibility: F) -> Selfwhere
F: Fn(SingleEntryContext, ExtensionHandle) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<bool, Error>> + Send + 'static,
Restricts the tab to entries for which visibility resolves to true.
The predicate runs whenever an entry is selected, so it should avoid expensive work such as fetching bodies.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for MessageTab
impl !UnwindSafe for MessageTab
impl Freeze for MessageTab
impl Send for MessageTab
impl Sync for MessageTab
impl Unpin for MessageTab
impl UnsafeUnpin for MessageTab
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more