Skip to main content

MessageTab

Struct MessageTab 

Source
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

Source

pub fn new<F, Fut>( id: impl Into<String>, label: impl Into<String>, content: F, ) -> Self
where 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.

Source

pub fn visible_when<F, Fut>(self, visibility: F) -> Self
where 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§

Source§

impl Serialize for MessageTab

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V