wisespace-io / binance-rs

Rust Library for the Binance API
Other
636 stars 287 forks source link

Client construction API change #178

Open danieleades opened 1 year ago

danieleades commented 1 year ago

this is a feature request

The API for constructing binance clients is a little clunky. There is a trait (Binance) which contains two construction methods for creating clients for different parts of the API. This trait is effectively used as a factory. I have a couple of concerns with the approach

I see two possible alternatives-

  1. Remove the 'factory' trait, implement the construction methods directly on the client objects, and require the appropriate credentials for that endpoint in the constructor
  2. use a builder struct with a type-guard that describes the credentials that are passed. Something like-
let factory = client::Builder::build()  // client::Builder<NoCredentials>
    .with_api_key("API_KEY")  // client::Builder<ApiKey>
    .with_secret_key("SECRET_KEY");  // client::Builder<ApiAndSecretKeys>

impl client::Builder<NoCredentials> {
    pub fn build() -> Self {
        ...
    }

    pub fn with_api_key(self, api_key: String) -> client::Builder<ApiKey> {
        ...
    }

    // client constructors for anything that doesn't need an API_KEY or SECRET_KEY
}

impl client::Builder<ApiKey> {
    pub fn with_secret_key(self, secret_key: String) -> client::Builder<ApiAndSecretKeys> {
        ...
    }

    // client constructors for anything that needs an API_KEY (only)
}

...

you get the idea