Skip to content

Commit 720de50

Browse files
committed
feat: generalize RequestConfig itself
1 parent b04303e commit 720de50

File tree

7 files changed

+19
-32
lines changed

7 files changed

+19
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![Minimum Supported Rust Version](https://img.shields.io/badge/nightly-1.85+-ab6000.svg)
33
[<img alt="crates.io" src="https://img.shields.io/crates/v/v_exchanges.svg?color=fc8d62&logo=rust" height="20" style=flat-square>](https://crates.io/crates/v_exchanges)
44
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs&style=flat-square" height="20">](https://docs.rs/v_exchanges)
5-
![Lines Of Code](https://img.shields.io/badge/LoC-4861-lightblue)
5+
![Lines Of Code](https://img.shields.io/badge/LoC-4851-lightblue)
66
<br>
77
[<img alt="ci errors" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/errors.yml?branch=master&style=for-the-badge&style=flat-square&label=errors&labelColor=420d09" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->
88
[<img alt="ci warnings" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/warnings.yml?branch=master&style=for-the-badge&style=flat-square&label=warnings&labelColor=d16002" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->

v_exchanges_adapters/src/exchanges/binance.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,8 @@ where
172172
type Successful = R;
173173
type Unsuccessful = BinanceHandlerError;
174174

175-
fn patch_request_config(&self, config: &mut RequestConfig) {
176-
if self.options.http_url == BinanceHttpUrl::default() {
177-
config.url_prefix = self.options.http_url.as_str().to_owned();
178-
}
175+
fn base_url(&self) -> String {
176+
self.options.http_url.as_str().to_owned()
179177
}
180178

181179
#[tracing::instrument(skip_all, fields(?builder))]

v_exchanges_adapters/src/exchanges/bitflyer.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ where
125125
type Successful = R;
126126
type Unsuccessful = BitFlyerHandlerError;
127127

128-
fn patch_request_config(&self, config: &mut RequestConfig) {
129-
if self.options.http_url != BitFlyerHttpUrl::default() {
130-
config.url_prefix = self.options.http_url.as_str().to_owned();
131-
}
128+
fn base_url(&self) -> String {
129+
self.options.http_url.as_str().to_owned()
132130
}
133131

134132
fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {

v_exchanges_adapters/src/exchanges/bybit.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ where
148148
type Successful = R;
149149
type Unsuccessful = BybitHandlerError;
150150

151-
fn patch_request_config(&self, config: &mut RequestConfig) {
152-
if self.options.http_url != BybitHttpUrl::default() {
153-
config.url_prefix = self.options.http_url.as_str().to_owned();
154-
}
151+
fn base_url(&self) -> String {
152+
self.options.http_url.as_str().to_owned()
155153
}
156154

157155
fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {

v_exchanges_adapters/src/exchanges/coincheck.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ where
115115
type Successful = R;
116116
type Unsuccessful = CoincheckHandlerError;
117117

118-
fn patch_request_config(&self, config: &mut RequestConfig) {
119-
if self.options.http_url != CoincheckHttpUrl::default() {
120-
config.url_prefix = self.options.http_url.as_str().to_owned();
121-
}
118+
fn base_url(&self) -> String {
119+
self.options.http_url.as_str().to_owned()
122120
}
123121

124122
fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {

v_exchanges_adapters/src/exchanges/mexc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ where
150150
type Successful = R;
151151
type Unsuccessful = MexcHandlerError;
152152

153-
fn patch_request_config(&self, config: &mut RequestConfig) {
154-
if self.options.http_url != MexcHttpUrl::default() {
155-
config.url_prefix = self.options.http_url.as_str().to_owned();
156-
}
153+
fn base_url(&self) -> String {
154+
self.options.http_url.as_str().to_owned()
157155
}
158156

159157
#[tracing::instrument(skip_all, fields(?builder))]

v_exchanges_api_generics/src/http.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ impl Client {
4040
where
4141
Q: Serialize + ?Sized + std::fmt::Debug,
4242
H: RequestHandler<B>, {
43-
let mut config = self.config.clone();
44-
handler.patch_request_config(&mut config);
43+
let config = &self.config;
44+
let base_url = handler.base_url();
4545
config.verify();
4646

47-
let url = config.url_prefix + url;
47+
//Q: is it ever desirable, actually?
48+
let url = base_url + url;
4849

4950
for i in 1..=config.max_try {
5051
//HACK: hate to create a new request every time, but I haven't yet figured out how to provide by reference
@@ -195,9 +196,10 @@ pub trait RequestHandler<B> {
195196
/// The type that represents an error occurred in [build_request()][Self::build_request()].
196197
type BuildError;
197198

198-
/// Returns a [RequestConfig] that will be used to send a HTTP reqeust.
199-
//TODO!!!!: everyone has the exact same implementation of this, unify (would req a trait for default REST url)
200-
fn patch_request_config(&self, default: &mut RequestConfig) {}
199+
/// Produce a url prefix (if any).
200+
fn base_url(&self) -> String {
201+
String::default()
202+
}
201203

202204
/// Build a HTTP request to be sent.
203205
///
@@ -246,10 +248,6 @@ pub struct RequestConfig {
246248
/// It is possible for the [RequestHandler] to override this in [RequestHandler::build_request()].
247249
/// See also: [RequestBuilder::timeout()].
248250
pub timeout: Duration,
249-
/// The prefix which will be used for requests sent using this configuration. [Default]s to `""`.
250-
///
251-
/// Example usage: `"https://example.com"`
252-
pub url_prefix: String,
253251
}
254252

255253
impl RequestConfig {
@@ -271,7 +269,6 @@ impl Default for RequestConfig {
271269
max_try: 1,
272270
retry_cooldown: Duration::from_millis(500),
273271
timeout: Duration::from_secs(3),
274-
url_prefix: String::new(),
275272
}
276273
}
277274
}

0 commit comments

Comments
 (0)