Crates.io | sfox |
lib.rs | sfox |
version | 0.1.6 |
source | src |
created_at | 2024-01-01 20:50:02.868652 |
updated_at | 2024-10-14 01:01:03.469021 |
description | Unofficial HTTP and Websocket Client for the SFox API |
homepage | |
repository | https://github.com/daveminer/sfox |
max_upload_size | |
id | 1085555 |
size | 163,078 |
sfox
provides typed, asynchronous wrappers around the SFox.com API HTTP calls
as well as Serde types for websocket message deserialization.
FIX is not implemented.
Complete the steps in this section to make the sfox
client available in your Rust application.
Set SFOX_AUTH_TOKEN
(created in the SFox web console) in your environment:
SFOX_AUTH_TOKEN=<AUTH-TOKEN>
Note: The server URLs SFOX_HTTP_SERVER_URL
and SFOX_WS_SERVER_URL
are also overridable for testing and development.
Add the following line under [dependencies]
in your project's Cargo.toml
:
sfox = "0.1.5"
The sfox::http
module performs asynchronous calls to the SFox API and returns typed responses.
use sfox::http::{self, v1::order_book::OrderBook};
let sfox = http::new().unwrap();
let order_book: OrderBook = sfox.order_book("btcusd").await.unwrap();
println!("Order book currency: {:?}", order_book.bids[0]);
The terminal should then print a response like:
Order book currency: OpenOrder { price: 35000.012, volume: 1.0, exchange: "some-exchange" }
Usage of the WebSocket client includes instantiating the client, authenticating with the server, and subscribing/unsubscribing to feeds.
use sfox::websocket::Client;
let sfox_ws = Client::new().await?;
let (mut write, mut read) = sfox_ws.stream.split();
// Start a task to read messages from the SFox stream
let _sfox_handle = tokio::spawn(async move { handle_incoming_message(&mut read).await });
// Subscribe to a feed on the websocket server
let _ticker_subscription = Client::subscribe(&mut write, Feed::Ticker, vec!["btcusd".to_string()]).await;
// Authenticate to access private feeds
let _authentication_attempt = Client::authenticate(&mut write).await;
// Subscribe to a private feed
let _balance_subscription = Client::subscribe(&mut write, Feed::Balances, vec![]).await;
where handle_incoming_message
could be implemented like:
async fn handle_incoming_message(read: &mut SplitStream<WssStream> ) {
while let Some(message) = read.next().await {
println!("Received message: {:?}", message);
}
}
The current MSRV is 1.69. This version may change in future minor versions, so use a restricted version requirement if a specific Rust version is required.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thank you!
git checkout -b feature/AmazingFeature
git commit -m 'Add some AmazingFeature'
git push origin feature/AmazingFeature
Distributed under the MIT License. See LICENSE.txt
for more information.