apca/
lib.rs

1// Copyright (C) 2019-2024 The apca Developers
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#![allow(clippy::let_unit_value, clippy::unreadable_literal)]
5#![warn(
6  bad_style,
7  dead_code,
8  future_incompatible,
9  improper_ctypes,
10  late_bound_lifetime_arguments,
11  missing_copy_implementations,
12  missing_debug_implementations,
13  missing_docs,
14  no_mangle_generic_items,
15  non_shorthand_field_patterns,
16  nonstandard_style,
17  overflowing_literals,
18  path_statements,
19  patterns_in_fns_without_body,
20  proc_macro_derive_resolution_fallback,
21  renamed_and_removed_lints,
22  rust_2018_compatibility,
23  rust_2018_idioms,
24  stable_features,
25  trivial_bounds,
26  trivial_numeric_casts,
27  type_alias_bounds,
28  tyvar_behind_raw_pointer,
29  unconditional_recursion,
30  unreachable_code,
31  unreachable_patterns,
32  unreachable_pub,
33  unstable_features,
34  unstable_name_collisions,
35  unused,
36  unused_comparisons,
37  unused_import_braces,
38  unused_lifetimes,
39  unused_qualifications,
40  unused_results,
41  while_true,
42  rustdoc::broken_intra_doc_links
43)]
44
45//! A crate for interacting with the [Alpaca
46//! API](https://alpaca.markets/docs/). In many ways it mirrors the
47//! structure of the upstream API, which is composed of functionality
48//! for trading (represented here by the [`api`] module) as well as
49//! market data retrieval (provided as part of the [`data`] module).
50//!
51//! Most operations require a [`Client`] object, instantiation of which
52//! in turn happens via a [`ApiInfo`] instance, which captures relevant
53//! configuration data such as credentials and URLs to use. A common
54//! workflow is to just set the `APCA_API_KEY_ID` (to the Alpaca key ID)
55//! and `APCA_API_SECRET_KEY` (to the Alpaca secret key) environment
56//! variables and use default values for everything else.
57//!
58//! ```no_run
59//! use apca::ApiInfo;
60//! use apca::Client;
61//!
62//! // Assumes credentials to be present in the `APCA_API_KEY_ID` and
63//! // `APCA_API_SECRET_KEY` environment variables.
64//! let api_info = ApiInfo::from_env().unwrap();
65//! let client = Client::new(api_info);
66//! # let _client = client;
67//! ```
68//!
69//! With a [`Client`] instance available, we can now start issuing
70//! requests to the upstream Trading API.
71//!
72//! ```no_run
73//! # use apca::ApiInfo;
74//! # use apca::Client;
75//! # let api_info = ApiInfo::from_env().unwrap();
76//! # let client = Client::new(api_info);
77//! use apca::api::v2::account;
78//!
79//! # tokio::runtime::Runtime::new().unwrap().block_on(async move {
80//! // Inquire general information about the account, such as available
81//! // cash and buying power.
82//! let account = client.issue::<account::Get>(&()).await.unwrap();
83//! let currency = account.currency;
84//! println!("cash:\t{} {currency}", account.cash);
85//! println!("buying power:\t{} {currency}", account.buying_power);
86//! # })
87//! ```
88
89#[macro_use]
90extern crate http_endpoint;
91
92#[macro_use]
93mod endpoint;
94
95/// A module comprising the functionality backing interactions with the
96/// trading API.
97pub mod api;
98
99/// A module for retrieving market data.
100pub mod data;
101
102mod api_info;
103mod client;
104mod error;
105mod subscribable;
106mod util;
107mod websocket;
108
109use std::borrow::Cow;
110
111pub use crate::api_info::ApiInfo;
112pub use crate::client::Client;
113pub use crate::endpoint::ApiError;
114pub use crate::error::Error;
115pub use crate::error::RequestError;
116pub use crate::subscribable::Subscribable;
117
118type Str = Cow<'static, str>;