Skip to main content

PubkyHttpClient

Struct PubkyHttpClient 

Source
pub struct PubkyHttpClient { /* private fields */ }
Expand description

Transport client for Pubky homeserver APIs and generic HTTP, with PKARR-aware URL handling.

PubkyHttpClient is the low-level engine the higher-level actors (PubkySession, PubkyStorage, Pkdns, PubkyAuthFlow) are built on. It owns:

  • A pkarr DHT client (for resolving pkdns endpoints and publishing records).
  • One or more reqwest HTTP clients (platform-specific).

§What it does

  • Detects pkarr public-key hosts and resolves them to concrete endpoints.
  • Internally, uses a unified cross_request(..) that works the same on native rust and WASM (WASM performs endpoint resolution & header injection; native is a thin wrapper).

§What it doesn’t do

  • It is not session/identity aware. No cookies, no per-user scoping. For authenticated per-user flows use crate::PubkySession.

§When to use

  • You want direct control over the PubkyHttpClient (power users, libs).
  • You’re wiring custom flows/tests and don’t need the high-level ergonomics.

§Construction

Use PubkyHttpClient::builder() to tweak timeouts, relays, or user-agent; or pick sensible defaults via PubkyHttpClient::new(). A PubkyHttpClient::testnet() helper configures a local test network.

§Platform notes

  • Native (rust, not WASM target):
    • ICANN domains use standard X.509 TLS via the icann_http client.
    • Pubky/PKDNS hosts (public-key hostnames or _pubky.<pk> domains) use PubkyTLS (TLS with RFC 7250 Raw Public Keys), verifying the connection against the target public key—no CA chain involved.
  • WASM:
    • All requests use the browser’s standard X.509 TLS stack.
    • For Pubky/PKDNS hosts, private method cross_request(..) resolves the endpoint via PKARR, rewrites the URL (including testnet/localhost mapping), and may add a pubky-host header to convey the intended public-key host.

§Examples

Basic construction. Works out of the box for mainline DHT pkarr endpoints.

let client = PubkyHttpClient::new()?;

Fetching a standard ICANN URL or any URL with request:

let client = PubkyHttpClient::new()?;
let url = Url::parse("https://example.com")?;
let resp = client.request(Method::GET, &url)
    .send().await?;
assert!(resp.status().is_success());

Note: request(..) is available on native targets. On WASM, use the high-level actors (e.g., Pubky, SessionStorage, PublicStorage) or the JS bindings’ client.fetch(..) provided in bindings/js.

Fetching a Pubky resource via its transport URL:

let client = PubkyHttpClient::new()?;
// Pubky App profile of user Pubky https://pubky.app/profile/ihaqcthsdbk751sxctk849bdr7yz7a934qen5gmpcbwcur49i97y
let user = "ihaqcthsdbk751sxctk849bdr7yz7a934qen5gmpcbwcur49i97y";
let url = format!("https://_pubky.{user}/pub/pubky.app/profile.json");
let resp = client.request(Method::GET, &url).send().await?;
let info = resp.text().await?;

Tip: For authenticated reads/writes, prefer session.storage().get(...), which automatically scopes paths and attaches the right session cookie.

Implementations§

Source§

impl PubkyHttpClient

Source

pub fn new() -> Result<Self, BuildError>

Creates a client configured for public mainline DHT and pkarr relays.

§Errors
  • Returns BuildError if the underlying HTTP clients cannot be constructed or configured.
Source

pub fn builder() -> PubkyHttpClientBuilder

Returns a builder to edit settings before creating PubkyHttpClient. Prefer this when you need to control PKARR/DHT inputs (relays, bootstrap); resolution itself remains automatic during requests.

Source

pub fn testnet() -> Result<Self, BuildError>

Creates a PubkyHttpClient preconfigured to talk to a locally running Pubky testnet.

§What this configures

On non wasm targets (not(target_arch = "wasm32")):

  • DHT bootstrap to the local testnet node at: "localhost:6881"
  • PKARR relay base URL: "http://localhost:15411"

On WASM targets:

  • Browser environments can’t dial UDP DHT; the builder is adjusted to use the testnet HTTP endpoints suitable for the browser (no UDP bootstrap). PKARR HTTP relay still points at http://localhost:15411 unless you override it.
§Requirements

You must have pubky-testnet binary running locally (it provides a homeserver, a DHT bootstrap, and a PKARR relay on the ports above). For example:

# From the pubky repo:
cargo run -p pubky-testnet
§Examples
use pubky::PubkyHttpClient;

let client = PubkyHttpClient::testnet()?;
// Now all https://_pubky.<pubkey>/... requests resolve via the local testnet
// DHT/PKARR, and hit the local homeserver.
§See also
§Errors
  • Returns BuildError if the underlying HTTP clients cannot be constructed or configured for the testnet settings.
Source

pub const fn pkarr(&self) -> &Client

Returns a reference to the internal Pkarr Client.

Source§

impl PubkyHttpClient

Source

pub async fn prepare_request(&self, url: &mut Url) -> Result<Option<String>>

Detect pubky hosts and return the z32 public key when applicable.

Native builds do not rewrite URLs; we only detect pubky hosts and return the pubky-host value when applicable.

§Errors

Returns RequestError::Validation if the host uses a pubky prefix.

Source

pub fn request<U: IntoUrl>(&self, method: Method, url: &U) -> RequestBuilder

Start building a Request with the Method and Url (native-only).

Returns a RequestBuilder, which will allow setting headers and the request body before sending.

Differs from reqwest::Client::request, in that it can make requests to:

  1. HTTPS URLs with a crate::PublicKey as top-level domain, by resolving corresponding endpoints, and verifying TLS certificates accordingly. (example: https://o4dksfbqk85ogzdb5osziw6befigbuxmuxkuxq8434q89uj56uyy)
  2. _pubky.<public-key> URLs like https://_pubky.o4dksfbqk85ogzdb5osziw6befigbuxmuxkuxq8434q89uj56uyy
§Errors

This method fails whenever the supplied Url cannot be parsed.

Trait Implementations§

Source§

impl Clone for PubkyHttpClient

Source§

fn clone(&self) -> PubkyHttpClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PubkyHttpClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CompatExt for T

Source§

fn compat(self) -> Compat<T>

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more