/
withdrawals.py
148 lines (124 loc) · 5.68 KB
/
withdrawals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import requests
import json
from ._types import handle_error
from time import sleep, time
class Withdrawals(dict):
"""
A class representing a collection of Withdrawals with methods to interact with the trading API. Inherits from a dictionary.
Attributes:
api_key (str): The API key used for authorization.
api_base_url (str): The base URL for the trading API.
headers (dict): Headers for making API requests with the API key.
Methods:
bid(item_id: int, amount: int) -> bool:
Place a bid on a listed item.
Parameters:
item_id (int): The ID of the item to bid on.
amount (int): The bid amount.
Returns:
True if the bid is successful, otherwise raises an error.
get_items(per_page: int = 2500, page: int = 1, search: str = "", order: str = "market_value",
sort="desc", auction: str = "yes", price_min: int = 1, price_max: int = 100000,
price_max_above: int = 15) -> list:
Get a list of listed items with the specified filters.
Parameters:
per_page (int): Number of items per page.
page (int): The page number to fetch.
search (str): A search string to filter items by.
order (str): The ordering criteria for items.
sort (str): The sorting order (asc or desc).
auction (str): Whether or not to include auction items.
price_min (int): Minimum price for items.
price_max (int): Maximum price for items.
price_max_above (int): Maximum price above the market value.
Returns:
A list of items matching the specified filters.
"""
def __init__(self, api_key, api_base_url, *args, **kwargs):
"""
Initializes a new instance of the Withdrawals class.
Parameters:
- api_key (str): The API key used for authentication.
- api_base_url (str): The base URL for API requests.
- *args: Variable-length argument list.
- **kwargs: Arbitrary keyword arguments.
Returns:
- None
"""
super(Withdrawals, self).__init__(*args, **kwargs)
self.__dict__ = self
self.api_key = api_key
self.api_base_url = api_base_url
self.headers = {'Authorization': f'Bearer {self.api_key}', 'Content-Type': 'application/json'}
def __getattr__(self, attr):
return self[attr]
def bid(self, item_id: int, amount: int):
"""
Place a bid on a listed item.
Parameters:
- item_id (int): The ID of the item to bid on.
- amount (int): The bid amount.
Returns:
- True if the bid is successful, otherwise raises an error.
"""
url = f"{self.api_base_url}trading/deposit/{item_id}/bid"
data = {"bid_value": amount}
response = requests.post(url, headers=self.headers, data=json.dumps(data))
status = response.status_code
response = response.json()
if status == 200:
return True
else:
handle_error(status, response, "Withdrawal", "bid")
def get_items(self, per_page: int = 2500, page: int = 1, search: str = "", order: str = "market_value", sort="desc", auction: str = "yes", price_min: int = 1, price_max: int = 100000, price_max_above: int = 15):
"""
Get a list of listed items with the specified filters.
Parameters:
- per_page (int): Number of items per page.
- page (int): The page number to fetch.
- search (str): A search string to filter items by.
- order (str): The ordering criteria for items.
- sort (str): The sorting order (asc or desc).
- auction (str): Whether or not to include auction items.
- price_min (int): Minimum price for items.
- price_max (int): Maximum price for items.
- price_max_above (int): Maximum price above the market value.
Returns:
- A list of items matching the specified filters.
"""
items = []
base_params = {
"per_page": per_page,
"order": order,
"sort": sort,
"auction": auction,
"price_min": price_min,
"price_max": price_max,
"price_max_above": price_max_above
}
if search:
base_params["search"] = search
response = requests.get(f"{self.api_base_url}trading/items", headers=self.headers, params={**base_params, "page": page})
status = response.status_code
if status == 200:
response = response.json()
items.extend(response['data'])
total_pages = response['last_page']
else:
response = response.json()
handle_error(status, response, "Withdrawal", "get_items")
for i in range(page + 1, total_pages + 1):
ratelimit_delay = 3.1 if search else 3.4
start = int(time())
response = requests.get(f"{self.api_base_url}trading/items", headers=self.headers, params={**base_params, "page": i})
status = response.status_code
if status == 200:
response = response.json()
items.extend(response['data'])
else:
response = response.json()
handle_error(status, response, "Withdrawal", "bid")
delta = int(time()) - start
if delta < ratelimit_delay:
sleep(ratelimit_delay - delta)
return items