Skip to content

Commit 649b786

Browse files
committed
fix: typing
1 parent 2998e5a commit 649b786

17 files changed

+340
-430
lines changed

cryptomarket/args.py

Lines changed: 109 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import asdict, dataclass
22
from enum import Enum
3-
from typing import Any, Dict, List, Optional
3+
from typing import Any, Dict, List, Literal, Optional, Type, TypeVar, Union
44

55
from cryptomarket.exceptions import ArgumentFormatException
66

@@ -13,6 +13,9 @@ def check_value(cls, value):
1313
item.value for item in cls])
1414

1515

16+
CheckerSubType = TypeVar('CheckerSubType', bound='Checker')
17+
18+
1619
class TransferType(Checker):
1720
TO_SUB_ACCOUNT = 'to_sub_account'
1821
FROM_SUB_ACCOUNT = 'from_sub_account'
@@ -146,10 +149,15 @@ class TransactionStatus(Checker):
146149

147150
class SortBy(Checker):
148151
TIMESTAMP = "timestamp"
149-
CREATED_AT = 'created_at'
150152
ID = 'id'
151153

152154

155+
class OrderBy(Checker):
156+
CREATED_AT = 'created_at'
157+
UPDATED_AT = 'updated_at'
158+
LAST_ACTIVITY_UP = 'last_activity_up'
159+
160+
153161
class Depth(Checker):
154162
_5 = 'D5'
155163
_10 = 'D10'
@@ -187,12 +195,12 @@ class FeeRequest:
187195

188196
@dataclass
189197
class ACLSettings:
190-
sub_account_id: str = None
191-
deposit_address_generation_enabled: bool = None
192-
withdraw_enabled: bool = None
193-
description: str = None
194-
created_at: str = None
195-
updated_at: str = None
198+
sub_account_id: Optional[str] = None
199+
deposit_address_generation_enabled: Optional[bool] = None
200+
withdraw_enabled: Optional[bool] = None
201+
description: Optional[str] = None
202+
created_at: Optional[str] = None
203+
updated_at: Optional[str] = None
196204

197205

198206
def clean_nones(a_dict: Dict[Any, Optional[Any]]) -> Dict[Any, Any]:
@@ -209,21 +217,27 @@ def build(self):
209217
orderedDict[parameter] = self.the_dict[parameter]
210218
return orderedDict
211219

212-
def add_coma_separated_list(self, key, val: List[str]):
213-
if val is not None:
214-
query = ','.join(val)
215-
if isinstance(val, str):
216-
query = val
217-
self.the_dict[key] = query
220+
def add_coma_separated_list(self, key: str, val: Optional[Union[str, List[str]]]):
221+
if val is None:
222+
return self
223+
elif isinstance(val, str):
224+
self.the_dict[key] = val
225+
else:
226+
self.the_dict[key] = ','.join(val)
218227
return self
219228

220-
def add_coma_separated_list_checking(self, checker: Checker, key, val: List[str]):
221-
if val is not None:
229+
def add_coma_separated_list_checking(self, checker: Type[CheckerSubType], key: str, val: Optional[Union[str, List[CheckerSubType]]]):
230+
if val is None:
231+
return self
232+
elif isinstance(val, str):
233+
elements = val.split(",")
234+
for element in elements:
235+
checker.check_value(element)
236+
self.the_dict[key] = val
237+
else:
222238
for element in val:
223239
checker.check_value(element)
224240
query = ','.join(val)
225-
if isinstance(val, str):
226-
query = val
227241
self.the_dict[key] = query
228242
return self
229243

@@ -232,230 +246,232 @@ def add(self, key, val):
232246
self.the_dict[key] = val
233247
return self
234248

235-
def add_cheking(self, checker: Checker, key, val):
249+
def add_cheking(self, checker: Type[Checker], key, val):
236250
if val is not None:
237251
checker.check_value(val)
238252
self.the_dict[key] = val
239253
return self
240254

241-
def currencies(self, val: List[str]):
255+
def currencies(self, val: Optional[Union[str, List[str]]]):
242256
return self.add_coma_separated_list("currencies", val)
243257

244-
def symbols(self, val: List[str]):
258+
def symbols(self, val: Optional[Union[str, List[str]]]):
245259
return self.add_coma_separated_list("symbols", val)
246260

247-
def currency(self, val: str):
261+
def currency(self, val: Optional[str]):
248262
return self.add("currency", val)
249263

250-
def from_(self, val: str):
264+
def from_(self, val: Optional[str]):
251265
return self.add("from", val)
252266

253-
def to(self, val: str):
267+
def to(self, val: Optional[str]):
254268
return self.add("to", val)
255269

256-
def symbol(self, val: str):
270+
def symbol(self, val: Optional[str]):
257271
return self.add("symbol", val)
258272

259-
def period(self, val: str):
273+
def period(self, val: Optional[str]):
260274
return self.add_cheking(Period, 'period', val)
261275

262-
def sort(self, val: str):
276+
def sort(self, val: Optional[str]):
263277
return self.add_cheking(Sort, 'sort', val)
264278

265-
def since(self, val: str):
279+
def since(self, val: Optional[str]):
266280
return self.add("from", val)
267281

268-
def until(self, val: str):
282+
def until(self, val: Optional[str]):
269283
return self.add("until", val)
270284

271-
def till(self, val: str):
285+
def till(self, val: Optional[str]):
272286
return self.add("till", val)
273287

274-
def limit(self, val: int):
288+
def limit(self, val: Optional[int]):
275289
return self.add("limit", val)
276290

277-
def offset(self, val: int):
291+
def offset(self, val: Optional[int]):
278292
return self.add("offset", val)
279293

280-
def by(self, val: str):
294+
def by(self, val: Optional[str]):
281295
return self.add("by", val)
282296

283-
def volume(self, val: str):
297+
def volume(self, val: Optional[int]):
284298
return self.add("volume", val)
285299

286-
def side(self, val: str):
300+
def side(self, val: Optional[str]):
287301
return self.add_cheking(Side, 'side', val)
288302

289-
def order_type(self, val: str):
303+
def order_type(self, val: Optional[str]):
290304
return self.add_cheking(OrderType, 'type', val)
291305

292-
def quantity(self, val: str):
306+
def quantity(self, val: Optional[str]):
293307
return self.add("quantity", val)
294308

295-
def price(self, val: str):
309+
def price(self, val: Optional[str]):
296310
return self.add("price", val)
297311

298-
def stop_price(self, val: str):
312+
def stop_price(self, val: Optional[str]):
299313
return self.add("stop_price", val)
300314

301-
def time_in_force(self, val: str):
315+
def time_in_force(self, val: Optional[str]):
302316
return self.add_cheking(TimeInForce, 'time_in_force', val)
303317

304-
def expire_time(self, val: str):
318+
def expire_time(self, val: Optional[str]):
305319
return self.add("expire_time", val)
306320

307-
def strict_validate(self, val: bool):
321+
def strict_validate(self, val: Optional[bool]):
308322
return self.add("strict_validate", val)
309323

310-
def post_only(self, val: bool):
324+
def post_only(self, val: Optional[bool]):
311325
return self.add("post_only", val)
312326

313-
def client_order_id(self, val: str):
327+
def client_order_id(self, val: Optional[str]):
314328
return self.add("client_order_id", val)
315329

316-
def new_client_order_id(self, val: str):
330+
def new_client_order_id(self, val: Optional[str]):
317331
return self.add("new_client_order_id", val)
318332

319-
def wait(self, val: int):
333+
def wait(self, val: Optional[int]):
320334
return self.add("wait", val)
321335

322-
def margin(self, val: str):
336+
def margin(self, val: Optional[str]):
323337
return self.add("margin", val)
324338

325-
def address(self, val: str):
339+
def address(self, val: Optional[str]):
326340
return self.add("address", val)
327341

328-
def amount(self, val: str):
342+
def amount(self, val: Optional[str]):
329343
return self.add("amount", val)
330344

331-
def payment_id(self, val: str):
345+
def payment_id(self, val: Optional[str]):
332346
return self.add("paymentId", val)
333347

334-
def include_fee(self, val: str):
348+
def include_fee(self, val: Optional[bool]):
335349
return self.add("include_fee", val)
336350

337-
def auto_commit(self, val: str):
351+
def auto_commit(self, val: Optional[bool]):
338352
return self.add("auto_commit", val)
339353

340-
def _from(self, val: str):
354+
def _from(self, val: Optional[str]):
341355
return self.add("from", val)
342356

343-
def from_currency(self, val: str):
357+
def from_currency(self, val: Optional[str]):
344358
return self.add("from_currency", val)
345359

346-
def to_currency(self, val: str):
360+
def to_currency(self, val: Optional[str]):
347361
return self.add("to_currency", val)
348362

349-
def source(self, val: str):
363+
def source(self, val: Optional[str]):
350364
return self.add_cheking(Account, "source", val)
351365

352-
def destination(self, val: str):
366+
def destination(self, val: Optional[str]):
353367
return self.add_cheking(Account, "destination", val)
354368

355-
def identify_by(self, val: str):
369+
def identify_by(self, val: Optional[str]):
356370
return self.add_cheking(IdentifyBy, 'by', val)
357371

358-
def identifier(self, val: str):
372+
def identifier(self, val: Optional[str]):
359373
return self.add("identifier", val)
360374

361-
def show_senders(self, val: bool):
375+
def show_senders(self, val: Optional[bool]):
362376
return self.add("show_senders", val)
363377

364-
def request_client_id(self, val: str):
378+
def request_client_id(self, val: Optional[str]):
365379
return self.add("request_client_id", val)
366380

367-
def depth(self, val: str):
381+
def depth(self, val: Optional[Union[int, str]]):
368382
return self.add("depth", val)
369383

370-
def speed(self, val: str):
384+
def speed(self, val: Optional[str]):
371385
return self.add("speed", val)
372386

373-
def make_rate(self, val: str):
387+
def make_rate(self, val: Optional[str]):
374388
return self.add("make_rate", val)
375389

376-
def take_rate(self, val: str):
390+
def take_rate(self, val: Optional[str]):
377391
return self.add("take_rate", val)
378392

379-
def order_id(self, val: str):
393+
def order_id(self, val: Optional[str]):
380394
return self.add("order_id", val)
381395

382-
def use_offchain(self, val: str):
396+
def use_offchain(self, val: Optional[str]):
383397
return self.add_cheking(Offchain, 'use_offchain', val)
384398

385-
def public_comment(self, val: str):
399+
def public_comment(self, val: Optional[str]):
386400
return self.add("public_comment", val)
387401

388-
def symbols_as_list(self, val: List[str]):
402+
def symbols_as_list(self, val: Optional[Union[str, List[str]]]):
389403
return self.add('symbols', val)
390404

391-
def currencies_as_list(self, val: List[str]):
405+
def currencies_as_list(self, val: Optional[Union[str, List[str]]]):
392406
return self.add('currencies', val)
393407

394-
def transaction_type(self, val: List[str]):
408+
def transaction_type(self, val: Optional[Union[str, List[str]]]):
395409
return self.add_cheking(TransactionType, 'type', val)
396410

397-
def transaction_types(self, val: List[str]):
411+
def transaction_types(self, val: Optional[Union[str, List[TransactionType]]]):
398412
return self.add_coma_separated_list_checking(TransactionType, 'types', val)
399413

400-
def transaction_subtype(self, val: str):
414+
def transaction_subtype(self, val: Optional[str]):
401415
return self.add_cheking(TransactionSubType, 'subtype', val)
402416

403-
def transaction_subtypes(self, val: str):
417+
def transaction_subtypes(self, val: Optional[Union[str, List[TransactionSubType]]]):
404418
return self.add_coma_separated_list_checking(TransactionSubType, 'subtypes', val)
405419

406-
def transaction_statuses(self, val: str):
420+
def transaction_statuses(self, val: Optional[Union[str, List[TransactionStatus]]]):
407421
return self.add_coma_separated_list_checking(TransactionStatus, 'statuses', val)
408422

409-
def id_from(self, val: str):
423+
def id_from(self, val: Optional[int]):
410424
return self.add('id_from', val)
411425

412-
def id_till(self, val: str):
426+
def id_till(self, val: Optional[int]):
413427
return self.add('id_till', val)
414428

415-
def tx_ids(self, val: List[str]):
429+
def tx_ids(self, val: Optional[Union[str, List[str]]]):
416430
return self.add_coma_separated_list('tx_ids', val)
417431

418-
# TODO: choose one, sort_by or order_by, and make it constant throughout the sdk.
419-
def sort_by(self, val: str):
420-
return self.add_cheking(SortBy, 'order_by', val)
432+
def sort_by(self, val: Optional[str]):
433+
return self.add_cheking(SortBy, 'sort_by', val)
434+
435+
def order_by(self, val: Optional[str]):
436+
return self.add_cheking(OrderBy, 'order_by', val)
421437

422-
def base_currency(self, val: str):
438+
def base_currency(self, val: Optional[str]):
423439
return self.add('base_currency', val)
424440

425-
def active_at(self, val: str):
441+
def active_at(self, val: Optional[str]):
426442
return self.add('active_at', val)
427443

428-
def transaction_id(self, val: str):
444+
def transaction_id(self, val: Optional[str]):
429445
return self.add('transaction_id', val)
430446

431-
def active(self, val: bool):
447+
def active(self, val: Optional[bool]):
432448
return self.add('active', val)
433449

434-
def order_list_id(self, val: str):
450+
def order_list_id(self, val: Optional[str]):
435451
return self.add('order_list_id', val)
436452

437-
def contingency_type(self, val: str):
453+
def contingency_type(self, val: Optional[str]):
438454
return self.add_cheking(ContingencyType, 'contingency_type', val)
439455

440456
def orders(self, val: List[OrderRequest]):
441457
return self.add('orders', [clean_nones(asdict(order)) for order in val])
442458

443-
def sub_account_ids(self, val: List[str]):
459+
def sub_account_ids(self, val: Optional[Union[str, List[str]]]):
444460
return self.add_coma_separated_list('sub_account_ids', val)
445461

446-
def sub_account_id(self, val: str):
462+
def sub_account_id(self, val: Optional[str]):
447463
return self.add('sub_account_id', val)
448464

449-
def subscription_mode(self, val: str):
465+
def subscription_mode(self, val: Optional[str]):
450466
return self.add_cheking(SubscriptionMode, 'mode', val)
451467

452-
def target_currency(self, val: str):
468+
def target_currency(self, val: Optional[str]):
453469
return self.add('target_currency', val)
454470

455-
def preferred_network(self, val: str):
471+
def preferred_network(self, val: Optional[str]):
456472
return self.add('preferred_network', val)
457473

458-
def type(self, val: str):
474+
def type(self, val: Optional[str]):
459475
return self.add('type', val)
460476

461477
def acl_settings(self, val: ACLSettings):

0 commit comments

Comments
 (0)