Skip to main content

RestClient

Struct RestClient 

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

An authenticated HTTP client for one Alpaca endpoint.

Implementations§

Source§

impl RestClient

Source

pub fn new(credentials: &Credentials, config: RestConfig) -> Result<Self>

Builds a client from credentials and configuration.

Redirects are disabled deliberately. Because the base URL is overridable, an http:// endpoint that redirects to https:// would replay a POST body — and its auth headers — over cleartext, so the request must fail loudly instead. The one route that legitimately redirects, the broker document download, uses its own client.

§Errors

Returns Error::Credentials if the credentials cannot be encoded as headers, or Error::Transport if the underlying HTTP client fails to build.

Source

pub fn unauthenticated(config: RestConfig) -> Result<Self>

Builds a client that sends no authentication headers.

Alpaca’s crypto market data endpoints serve unauthenticated requests. Every other endpoint requires credentials.

§Errors

Returns Error::Transport if the underlying HTTP client fails to build.

Source

pub fn config(&self) -> &RestConfig

The configuration this client was built with.

Source

pub fn at_version(&self, api_version: &str) -> Self

The same client, targeting a different API version segment.

Alpaca versions routes individually rather than per API, and it is not unusual for a client’s own version to be wrong for a given route: the trading API is v2 but its locate routes are v1 and its per-market calendar is v3; the broker API is v1 but funding wallets are v1beta and logos v1beta1.

Writing that at the call site is deliberate. A version buried in a client constructor is the mistake that shipped three event streams pointing at routes Alpaca had retired; this way the version sits next to the path it belongs to.

Cheap: the underlying HTTP client is shared, not rebuilt.

Source

pub async fn get<T, Q>(&self, path: &str, query: &Q) -> Result<T>

Issues a GET, sending query as URL parameters.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn delete<T, Q>(&self, path: &str, query: &Q) -> Result<T>

Issues a DELETE, sending query as URL parameters.

DELETE parameters go in the query string, not the body — which is what the endpoints taking them (cancel_orders, close_position) expect.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn delete_effectful<T, Q>(&self, path: &str, query: &Q) -> Result<T>

Issues a DELETE that must never be replayed.

For the routes whose verb says “remove a record” and whose effect is “submit an order”: DELETE /v2/positions/{asset} and DELETE /v2/positions, plus their broker twins. HTTP calls a DELETE idempotent and for those four it is not — a replayed liquidation sells the same quantity twice. A timeout on one is reported, not retried; a 429 still is, because the rate limiter refused it before anything acted.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn post<T, B>(&self, path: &str, body: &B) -> Result<T>

Issues a POST with a JSON body.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn put<T, B>(&self, path: &str, body: &B) -> Result<T>

Issues a PUT with a JSON body.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn patch<T, B>(&self, path: &str, body: &B) -> Result<T>

Issues a PATCH with a JSON body.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn request<T, Q, B>( &self, method: Method, replay: Replay, path: &str, query: Option<&Q>, body: Option<&B>, ) -> Result<T>

Issues a request with both a query string and a body.

replay decides what happens on a retryable status. Replay::ByMethod is right for almost everything; pass Replay::Never for a route whose effect contradicts its verb — one that submits an order or moves money behind a PUT or a DELETE.

The six methods above cover routes that take one or the other, which is nearly all of them. A handful take both — PUT /v2/watchlists:by_name names the watchlist in the query and carries the update in the body — and folding the query into the path string would skip its encoding.

§Errors

Propagates transport, API, and decoding failures.

Source

pub async fn get_bytes<Q>(&self, path: &str, query: &Q) -> Result<Vec<u8>>
where Q: Serialize + ?Sized,

Issues a GET and returns the response body as bytes.

For the routes that answer with something other than JSON. The logo endpoint is the only one in this crate: it serves image/png, and putting it through RestClient::get would try to parse a PNG as JSON.

§Errors

Propagates transport and API failures.

Source

pub async fn request_raw<Q, B>( &self, method: Method, replay: Replay, path: &str, query: Option<&Q>, body: Option<&B>, ) -> Result<String>
where Q: Serialize + ?Sized, B: Serialize + ?Sized,

Issues a request and returns the raw response body without deserializing.

This is the escape hatch for a route this crate does not wrap, which makes replay load-bearing: the wrapped routes have their answer chosen for them, and here it is yours. Replay::Never for anything that acts — a replayed liquidation sells twice.

§Errors

Propagates transport and API failures.

Trait Implementations§

Source§

impl Clone for RestClient

Source§

fn clone(&self) -> Self

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 RestClient

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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