Skip to content

Commit 8814d18

Browse files
Remove redundant code in bfxapi.rest._interface.
1 parent 5a458a2 commit 8814d18

File tree

4 files changed

+91
-103
lines changed

4 files changed

+91
-103
lines changed

bfxapi/rest/_interface/interface.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
from typing import TYPE_CHECKING, Any, Optional
1+
from typing import Optional
22

33
from .middleware import Middleware
44

5-
if TYPE_CHECKING:
6-
from requests.sessions import _Params
7-
85

96
class Interface:
107
def __init__(
118
self, host: str, api_key: Optional[str] = None, api_secret: Optional[str] = None
129
):
13-
self.__middleware = Middleware(host, api_key, api_secret)
14-
15-
def _get(self, endpoint: str, params: Optional["_Params"] = None) -> Any:
16-
return self.__middleware.get(endpoint, params)
17-
18-
def _post(
19-
self,
20-
endpoint: str,
21-
body: Optional[Any] = None,
22-
params: Optional["_Params"] = None,
23-
) -> Any:
24-
return self.__middleware.post(endpoint, body, params)
10+
self._m = Middleware(host, api_key, api_secret)

bfxapi/rest/_interfaces/rest_auth_endpoints.py

+46-44
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141

4242
class RestAuthEndpoints(Interface):
4343
def get_user_info(self) -> UserInfo:
44-
return serializers.UserInfo.parse(*self._post("auth/r/info/user"))
44+
return serializers.UserInfo.parse(*self._m.post("auth/r/info/user"))
4545

4646
def get_login_history(self) -> List[LoginHistory]:
4747
return [
4848
serializers.LoginHistory.parse(*sub_data)
49-
for sub_data in self._post("auth/r/logins/hist")
49+
for sub_data in self._m.post("auth/r/logins/hist")
5050
]
5151

5252
def get_balance_available_for_orders_or_offers(
@@ -61,13 +61,13 @@ def get_balance_available_for_orders_or_offers(
6161
body = {"symbol": symbol, "type": type, "dir": dir, "rate": rate, "lev": lev}
6262

6363
return serializers.BalanceAvailable.parse(
64-
*self._post("auth/calc/order/avail", body=body)
64+
*self._m.post("auth/calc/order/avail", body=body)
6565
)
6666

6767
def get_wallets(self) -> List[Wallet]:
6868
return [
6969
serializers.Wallet.parse(*sub_data)
70-
for sub_data in self._post("auth/r/wallets")
70+
for sub_data in self._m.post("auth/r/wallets")
7171
]
7272

7373
def get_orders(
@@ -80,7 +80,7 @@ def get_orders(
8080

8181
return [
8282
serializers.Order.parse(*sub_data)
83-
for sub_data in self._post(endpoint, body={"id": ids})
83+
for sub_data in self._m.post(endpoint, body={"id": ids})
8484
]
8585

8686
def submit_order(
@@ -115,7 +115,7 @@ def submit_order(
115115
}
116116

117117
return _Notification[Order](serializers.Order).parse(
118-
*self._post("auth/w/order/submit", body=body)
118+
*self._m.post("auth/w/order/submit", body=body)
119119
)
120120

121121
def update_order(
@@ -150,7 +150,7 @@ def update_order(
150150
}
151151

152152
return _Notification[Order](serializers.Order).parse(
153-
*self._post("auth/w/order/update", body=body)
153+
*self._m.post("auth/w/order/update", body=body)
154154
)
155155

156156
def cancel_order(
@@ -161,7 +161,7 @@ def cancel_order(
161161
cid_date: Optional[str] = None,
162162
) -> Notification[Order]:
163163
return _Notification[Order](serializers.Order).parse(
164-
*self._post(
164+
*self._m.post(
165165
"auth/w/order/cancel", body={"id": id, "cid": cid, "cid_date": cid_date}
166166
)
167167
)
@@ -177,7 +177,7 @@ def cancel_order_multi(
177177
body = {"id": id, "cid": cid, "gid": gid, "all": all}
178178

179179
return _Notification[List[Order]](serializers.Order, is_iterable=True).parse(
180-
*self._post("auth/w/order/cancel/multi", body=body)
180+
*self._m.post("auth/w/order/cancel/multi", body=body)
181181
)
182182

183183
def get_orders_history(
@@ -198,13 +198,13 @@ def get_orders_history(
198198

199199
return [
200200
serializers.Order.parse(*sub_data)
201-
for sub_data in self._post(endpoint, body=body)
201+
for sub_data in self._m.post(endpoint, body=body)
202202
]
203203

204204
def get_order_trades(self, symbol: str, id: int) -> List[OrderTrade]:
205205
return [
206206
serializers.OrderTrade.parse(*sub_data)
207-
for sub_data in self._post(f"auth/r/order/{symbol}:{id}/trades")
207+
for sub_data in self._m.post(f"auth/r/order/{symbol}:{id}/trades")
208208
]
209209

210210
def get_trades_history(
@@ -225,7 +225,7 @@ def get_trades_history(
225225

226226
return [
227227
serializers.Trade.parse(*sub_data)
228-
for sub_data in self._post(endpoint, body=body)
228+
for sub_data in self._m.post(endpoint, body=body)
229229
]
230230

231231
def get_ledgers(
@@ -241,43 +241,43 @@ def get_ledgers(
241241

242242
return [
243243
serializers.Ledger.parse(*sub_data)
244-
for sub_data in self._post(f"auth/r/ledgers/{currency}/hist", body=body)
244+
for sub_data in self._m.post(f"auth/r/ledgers/{currency}/hist", body=body)
245245
]
246246

247247
def get_base_margin_info(self) -> BaseMarginInfo:
248248
return serializers.BaseMarginInfo.parse(
249-
*(self._post("auth/r/info/margin/base")[1])
249+
*(self._m.post("auth/r/info/margin/base")[1])
250250
)
251251

252252
def get_symbol_margin_info(self, symbol: str) -> SymbolMarginInfo:
253253
return serializers.SymbolMarginInfo.parse(
254-
*self._post(f"auth/r/info/margin/{symbol}")
254+
*self._m.post(f"auth/r/info/margin/{symbol}")
255255
)
256256

257257
def get_all_symbols_margin_info(self) -> List[SymbolMarginInfo]:
258258
return [
259259
serializers.SymbolMarginInfo.parse(*sub_data)
260-
for sub_data in self._post("auth/r/info/margin/sym_all")
260+
for sub_data in self._m.post("auth/r/info/margin/sym_all")
261261
]
262262

263263
def get_positions(self) -> List[Position]:
264264
return [
265265
serializers.Position.parse(*sub_data)
266-
for sub_data in self._post("auth/r/positions")
266+
for sub_data in self._m.post("auth/r/positions")
267267
]
268268

269269
def claim_position(
270270
self, id: int, *, amount: Optional[Union[str, float, Decimal]] = None
271271
) -> Notification[PositionClaim]:
272272
return _Notification[PositionClaim](serializers.PositionClaim).parse(
273-
*self._post("auth/w/position/claim", body={"id": id, "amount": amount})
273+
*self._m.post("auth/w/position/claim", body={"id": id, "amount": amount})
274274
)
275275

276276
def increase_position(
277277
self, symbol: str, amount: Union[str, float, Decimal]
278278
) -> Notification[PositionIncrease]:
279279
return _Notification[PositionIncrease](serializers.PositionIncrease).parse(
280-
*self._post(
280+
*self._m.post(
281281
"auth/w/position/increase", body={"symbol": symbol, "amount": amount}
282282
)
283283
)
@@ -286,7 +286,7 @@ def get_increase_position_info(
286286
self, symbol: str, amount: Union[str, float, Decimal]
287287
) -> PositionIncreaseInfo:
288288
return serializers.PositionIncreaseInfo.parse(
289-
*self._post(
289+
*self._m.post(
290290
"auth/r/position/increase/info",
291291
body={"symbol": symbol, "amount": amount},
292292
)
@@ -301,7 +301,7 @@ def get_positions_history(
301301
) -> List[PositionHistory]:
302302
return [
303303
serializers.PositionHistory.parse(*sub_data)
304-
for sub_data in self._post(
304+
for sub_data in self._m.post(
305305
"auth/r/positions/hist",
306306
body={"start": start, "end": end, "limit": limit},
307307
)
@@ -316,7 +316,7 @@ def get_positions_snapshot(
316316
) -> List[PositionSnapshot]:
317317
return [
318318
serializers.PositionSnapshot.parse(*sub_data)
319-
for sub_data in self._post(
319+
for sub_data in self._m.post(
320320
"auth/r/positions/snap",
321321
body={"start": start, "end": end, "limit": limit},
322322
)
@@ -334,15 +334,15 @@ def get_positions_audit(
334334

335335
return [
336336
serializers.PositionAudit.parse(*sub_data)
337-
for sub_data in self._post("auth/r/positions/audit", body=body)
337+
for sub_data in self._m.post("auth/r/positions/audit", body=body)
338338
]
339339

340340
def set_derivative_position_collateral(
341341
self, symbol: str, collateral: Union[str, float, Decimal]
342342
) -> DerivativePositionCollateral:
343343
return serializers.DerivativePositionCollateral.parse(
344344
*(
345-
self._post(
345+
self._m.post(
346346
"auth/w/deriv/collateral/set",
347347
body={"symbol": symbol, "collateral": collateral},
348348
)[0]
@@ -353,7 +353,7 @@ def get_derivative_position_collateral_limits(
353353
self, symbol: str
354354
) -> DerivativePositionCollateralLimits:
355355
return serializers.DerivativePositionCollateralLimits.parse(
356-
*self._post("auth/calc/deriv/collateral/limit", body={"symbol": symbol})
356+
*self._m.post("auth/calc/deriv/collateral/limit", body={"symbol": symbol})
357357
)
358358

359359
def get_funding_offers(self, *, symbol: Optional[str] = None) -> List[FundingOffer]:
@@ -364,7 +364,7 @@ def get_funding_offers(self, *, symbol: Optional[str] = None) -> List[FundingOff
364364

365365
return [
366366
serializers.FundingOffer.parse(*sub_data)
367-
for sub_data in self._post(endpoint)
367+
for sub_data in self._m.post(endpoint)
368368
]
369369

370370
def submit_funding_offer(
@@ -387,22 +387,24 @@ def submit_funding_offer(
387387
}
388388

389389
return _Notification[FundingOffer](serializers.FundingOffer).parse(
390-
*self._post("auth/w/funding/offer/submit", body=body)
390+
*self._m.post("auth/w/funding/offer/submit", body=body)
391391
)
392392

393393
def cancel_funding_offer(self, id: int) -> Notification[FundingOffer]:
394394
return _Notification[FundingOffer](serializers.FundingOffer).parse(
395-
*self._post("auth/w/funding/offer/cancel", body={"id": id})
395+
*self._m.post("auth/w/funding/offer/cancel", body={"id": id})
396396
)
397397

398398
def cancel_all_funding_offers(self, currency: str) -> Notification[Literal[None]]:
399399
return _Notification[Literal[None]](None).parse(
400-
*self._post("auth/w/funding/offer/cancel/all", body={"currency": currency})
400+
*self._m.post(
401+
"auth/w/funding/offer/cancel/all", body={"currency": currency}
402+
)
401403
)
402404

403405
def submit_funding_close(self, id: int) -> Notification[Literal[None]]:
404406
return _Notification[Literal[None]](None).parse(
405-
*self._post("auth/w/funding/close", body={"id": id})
407+
*self._m.post("auth/w/funding/close", body={"id": id})
406408
)
407409

408410
def toggle_auto_renew(
@@ -423,7 +425,7 @@ def toggle_auto_renew(
423425
}
424426

425427
return _Notification[FundingAutoRenew](serializers.FundingAutoRenew).parse(
426-
*self._post("auth/w/funding/auto", body=body)
428+
*self._m.post("auth/w/funding/auto", body=body)
427429
)
428430

429431
def toggle_keep_funding(
@@ -434,7 +436,7 @@ def toggle_keep_funding(
434436
changes: Optional[Dict[int, Literal[1, 2]]] = None,
435437
) -> Notification[Literal[None]]:
436438
return _Notification[Literal[None]](None).parse(
437-
*self._post(
439+
*self._m.post(
438440
"auth/w/funding/keep",
439441
body={"type": type, "id": ids, "changes": changes},
440442
)
@@ -455,7 +457,7 @@ def get_funding_offers_history(
455457

456458
return [
457459
serializers.FundingOffer.parse(*sub_data)
458-
for sub_data in self._post(
460+
for sub_data in self._m.post(
459461
endpoint, body={"start": start, "end": end, "limit": limit}
460462
)
461463
]
@@ -468,7 +470,7 @@ def get_funding_loans(self, *, symbol: Optional[str] = None) -> List[FundingLoan
468470

469471
return [
470472
serializers.FundingLoan.parse(*sub_data)
471-
for sub_data in self._post(endpoint)
473+
for sub_data in self._m.post(endpoint)
472474
]
473475

474476
def get_funding_loans_history(
@@ -486,7 +488,7 @@ def get_funding_loans_history(
486488

487489
return [
488490
serializers.FundingLoan.parse(*sub_data)
489-
for sub_data in self._post(
491+
for sub_data in self._m.post(
490492
endpoint, body={"start": start, "end": end, "limit": limit}
491493
)
492494
]
@@ -501,7 +503,7 @@ def get_funding_credits(
501503

502504
return [
503505
serializers.FundingCredit.parse(*sub_data)
504-
for sub_data in self._post(endpoint)
506+
for sub_data in self._m.post(endpoint)
505507
]
506508

507509
def get_funding_credits_history(
@@ -519,7 +521,7 @@ def get_funding_credits_history(
519521

520522
return [
521523
serializers.FundingCredit.parse(*sub_data)
522-
for sub_data in self._post(
524+
for sub_data in self._m.post(
523525
endpoint, body={"start": start, "end": end, "limit": limit}
524526
)
525527
]
@@ -542,12 +544,12 @@ def get_funding_trades_history(
542544

543545
return [
544546
serializers.FundingTrade.parse(*sub_data)
545-
for sub_data in self._post(endpoint, body=body)
547+
for sub_data in self._m.post(endpoint, body=body)
546548
]
547549

548550
def get_funding_info(self, key: str) -> FundingInfo:
549551
return serializers.FundingInfo.parse(
550-
*(self._post(f"auth/r/info/funding/{key}")[2])
552+
*(self._m.post(f"auth/r/info/funding/{key}")[2])
551553
)
552554

553555
def transfer_between_wallets(
@@ -567,7 +569,7 @@ def transfer_between_wallets(
567569
}
568570

569571
return _Notification[Transfer](serializers.Transfer).parse(
570-
*self._post("auth/w/transfer", body=body)
572+
*self._m.post("auth/w/transfer", body=body)
571573
)
572574

573575
def submit_wallet_withdrawal(
@@ -581,14 +583,14 @@ def submit_wallet_withdrawal(
581583
}
582584

583585
return _Notification[Withdrawal](serializers.Withdrawal).parse(
584-
*self._post("auth/w/withdraw", body=body)
586+
*self._m.post("auth/w/withdraw", body=body)
585587
)
586588

587589
def get_deposit_address(
588590
self, wallet: str, method: str, op_renew: bool = False
589591
) -> Notification[DepositAddress]:
590592
return _Notification[DepositAddress](serializers.DepositAddress).parse(
591-
*self._post(
593+
*self._m.post(
592594
"auth/w/deposit/address",
593595
body={"wallet": wallet, "method": method, "op_renew": op_renew},
594596
)
@@ -598,7 +600,7 @@ def generate_deposit_invoice(
598600
self, wallet: str, currency: str, amount: Union[str, float, Decimal]
599601
) -> LightningNetworkInvoice:
600602
return serializers.LightningNetworkInvoice.parse(
601-
*self._post(
603+
*self._m.post(
602604
"auth/w/deposit/invoice",
603605
body={"wallet": wallet, "currency": currency, "amount": amount},
604606
)
@@ -619,7 +621,7 @@ def get_movements(
619621

620622
return [
621623
serializers.Movement.parse(*sub_data)
622-
for sub_data in self._post(
624+
for sub_data in self._m.post(
623625
endpoint, body={"start": start, "end": end, "limit": limit}
624626
)
625627
]

0 commit comments

Comments
 (0)