pub struct SignedRequestBuilder<'a> { /* private fields */ }Expand description
Builder for creating authenticated Binance API requests.
This builder encapsulates the common signing workflow:
- Credential validation
- Timestamp generation
- Parameter signing with HMAC-SHA256
- Authentication header injection
- HTTP request execution
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let data = binance.signed_request("/api/v3/account")
.param("symbol", "BTCUSDT")
.optional_param("limit", Some(100))
.execute()
.await?;Implementations§
Source§impl<'a> SignedRequestBuilder<'a>
impl<'a> SignedRequestBuilder<'a>
Sourcepub fn new(binance: &'a Binance, endpoint: impl Into<String>) -> Self
pub fn new(binance: &'a Binance, endpoint: impl Into<String>) -> Self
Creates a new signed request builder.
§Arguments
binance- Reference to the Binance exchange instanceendpoint- Full API endpoint URL
§Example
let binance = Binance::new(ExchangeConfig::default()).unwrap();
let builder = SignedRequestBuilder::new(&binance, "https://api.binance.com/api/v3/account");Sourcepub fn method(self, method: HttpMethod) -> Self
pub fn method(self, method: HttpMethod) -> Self
Sourcepub fn param(self, key: impl Into<String>, value: impl ToString) -> Self
pub fn param(self, key: impl Into<String>, value: impl ToString) -> Self
Adds a required parameter.
§Arguments
key- Parameter namevalue- Parameter value (will be converted to string)
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let data = binance.signed_request("/api/v3/myTrades")
.param("symbol", "BTCUSDT")
.param("limit", 100)
.execute()
.await?;Sourcepub fn optional_param<T: ToString>(
self,
key: impl Into<String>,
value: Option<T>,
) -> Self
pub fn optional_param<T: ToString>( self, key: impl Into<String>, value: Option<T>, ) -> Self
Adds an optional parameter (only if value is Some).
§Arguments
key- Parameter namevalue- Optional parameter value
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let since: Option<i64> = Some(1234567890);
let limit: Option<u32> = None;
let data = binance.signed_request("/api/v3/myTrades")
.param("symbol", "BTCUSDT")
.optional_param("startTime", since)
.optional_param("limit", limit) // Won't be added since it's None
.execute()
.await?;Sourcepub fn params(self, params: BTreeMap<String, String>) -> Self
pub fn params(self, params: BTreeMap<String, String>) -> Self
Adds multiple parameters from a BTreeMap.
§Arguments
params- Map of parameter key-value pairs
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let mut params = BTreeMap::new();
params.insert("symbol".to_string(), "BTCUSDT".to_string());
params.insert("side".to_string(), "BUY".to_string());
let data = binance.signed_request("/api/v3/order")
.params(params)
.execute()
.await?;Sourcepub fn merge_json_params(self, params: Option<Value>) -> Self
pub fn merge_json_params(self, params: Option<Value>) -> Self
Merges parameters from a JSON Value object.
Only string, number, and boolean values are supported. Nested objects and arrays are ignored.
§Arguments
params- Optional JSON Value containing parameters
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let extra_params = Some(json!({
"orderId": "12345",
"fromId": 67890
}));
let data = binance.signed_request("/api/v3/myTrades")
.param("symbol", "BTCUSDT")
.merge_json_params(extra_params)
.execute()
.await?;Sourcepub async fn execute(self) -> Result<Value>
pub async fn execute(self) -> Result<Value>
Executes the signed request and returns the response.
This method:
- Validates that credentials are configured
- Gets the signing timestamp (using cached offset if available)
- Signs the parameters with HMAC-SHA256
- Adds authentication headers
- Executes the HTTP request
§Returns
Returns the raw serde_json::Value response for further parsing.
§Errors
- Returns authentication error if credentials are missing
- Returns network error if the request fails
- Returns parse error if response parsing fails
§Example
let binance = Binance::new(ExchangeConfig::default())?;
let data = binance.signed_request("/api/v3/account")
.execute()
.await?;
println!("Response: {:?}", data);Auto Trait Implementations§
impl<'a> Freeze for SignedRequestBuilder<'a>
impl<'a> !RefUnwindSafe for SignedRequestBuilder<'a>
impl<'a> Send for SignedRequestBuilder<'a>
impl<'a> Sync for SignedRequestBuilder<'a>
impl<'a> Unpin for SignedRequestBuilder<'a>
impl<'a> !UnwindSafe for SignedRequestBuilder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more