coinbase_pro_rs/
lib.rs

1//! Coinbase pro client with sync/async + websocket-feed support
2//!
3//! ## Structure
4//!
5//! There are two main structures to work with: [`Private`] and [`Public`], which provide interfaces to
6//! work with [https://docs.pro.coinbase.com](https://docs.pro.coinbase.com) .
7//! The structures should be parametrised with: [`Sync`] or [`ASync`] adapter-type, which blocks
8//! future and returns result of its execution for Sync adapter or returns Future for ASync
9//! adapter.
10//!
11//! [`WSFeed`] provides futures::Stream of websocket message for different channels.
12//!
13//! ## Examples
14//!
15//! ### Async
16//! ```
17//! use std::future::Future;
18//! use coinbase_pro_rs::{Public, ASync, SANDBOX_URL};
19//! use futures::{TryFutureExt};
20//!
21//! #[tokio::main]
22//! async fn main() {
23//!     let client: Public<ASync> = Public::new_with_keep_alive(SANDBOX_URL, false);
24//!     // if keep_alive is not disables - tokio::run will hold the connection without exiting the example
25//!     client.get_time().await
26//!         .map_err(|_| ())
27//!         .and_then(|time| {
28//!             println!("Coinbase.time: {}", time.iso);
29//!             Ok(())
30//!         });
31//!
32//! }
33//! ```
34//! ### Sync
35//! ```
36//! use coinbase_pro_rs::{Public, Sync, SANDBOX_URL};
37//!
38//! fn main() {
39//!    let client: Public<Sync> = Public::new(SANDBOX_URL);
40//!    let time = client.get_time().unwrap();
41//!    println!("Coinbase.time: {}", time.iso);
42//!}
43//! ```
44//! ### Websocket
45//! ```
46//! use futures::{Future, Stream, StreamExt, TryStreamExt};
47//! use coinbase_pro_rs::{WSFeed, CBError, WS_SANDBOX_URL};
48//! use coinbase_pro_rs::structs::wsfeed::*;
49//!
50//! #[tokio::main]
51//! async fn main() {
52//!     let stream = WSFeed::connect(WS_SANDBOX_URL,
53//!         &["BTC-USD"], &[ChannelType::Heartbeat]).await.unwrap();
54//!
55//!     stream
56//!         .take(10)
57//!         .for_each(|msg: Result<Message, CBError>| async {
58//!         match msg.unwrap() {
59//!             Message::Heartbeat {sequence, last_trade_id, time, ..} => println!("{}: seq:{} id{}",
60//!                                                                                time, sequence, last_trade_id),
61//!             Message::Error {message} => println!("Error: {}", message),
62//!             Message::InternalError(_) => panic!("internal_error"),
63//!             other => println!("{:?}", other)
64//!         }
65//!     }).await;
66//! }
67//! ```
68
69pub mod adapters;
70mod error;
71pub mod private;
72pub mod public;
73pub mod structs;
74mod utils;
75
76pub mod wsfeed;
77
78pub use crate::adapters::{ASync, Sync};
79pub use crate::error::{CBError, WSError};
80pub use crate::private::Private;
81pub use crate::public::Public;
82pub use crate::wsfeed::WSFeed;
83
84#[cfg(test)]
85#[macro_use]
86extern crate serial_test;
87
88pub type Result<T> = std::result::Result<T, CBError>;
89
90/// https://api.pro.coinbase.com
91pub const MAIN_URL: &str = "https://api.pro.coinbase.com";
92/// https://api-public.sandbox.pro.coinbase.com
93pub const SANDBOX_URL: &str = "https://api-public.sandbox.pro.coinbase.com";
94/// wss://ws-feed.pro.coinbase.com
95pub const WS_URL: &str = "wss://ws-feed.pro.coinbase.com";
96/// wss://ws-feed-public.sandbox.pro.coinbase.com
97pub const WS_SANDBOX_URL: &str = "wss://ws-feed-public.sandbox.pro.coinbase.com";