33
44import requests
55
6- from .exceptions import EmptyResponse , InternalServerError , PricesTFError , RateLimited
7- from .utils import to_refined
6+ from .exceptions import TF2UtilsError
7+ from .utils import refinedify
88
99
10- class PricesTF :
11- URL = "https://api2.prices.tf"
10+ class PricesTFError (TF2UtilsError ):
11+ pass
12+
13+
14+ class UnauthorizedError (PricesTFError ):
15+ pass
16+
17+
18+ class InternalServerError (PricesTFError ):
19+ pass
20+
21+
22+ class RateLimited (PricesTFError ):
23+ pass
24+
1225
26+ class EmptyResponse (PricesTFError ):
27+ pass
28+
29+
30+ class PricesTF :
1331 def __init__ (self ) -> None :
32+ self .url = "https://api2.prices.tf"
1433 self ._access_token = ""
1534 self ._headers = {}
1635
1736 @staticmethod
18- def _format_price (data : dict ) -> dict :
37+ def format_price (data : dict ) -> dict :
38+ buy_keys = data .get ("buyKeys" , 0 )
39+ buy_metal = refinedify (data .get ("buyHalfScrap" , 0 ) / 18 )
40+ sell_keys = data .get ("sellKeys" , 0 )
41+ sell_metal = refinedify (data .get ("sellHalfScrap" , 0 ) / 18 )
42+
1943 return {
20- "buy" : {
21- "keys" : data ["buyKeys" ],
22- "metal" : to_refined (data ["buyHalfScrap" ] / 2 ),
23- },
24- "sell" : {
25- "keys" : data ["sellKeys" ],
26- "metal" : to_refined (data ["sellHalfScrap" ] / 2 ),
27- },
44+ "buy" : {"keys" : buy_keys , "metal" : buy_metal },
45+ "sell" : {"keys" : sell_keys , "metal" : sell_metal },
2846 }
2947
3048 @staticmethod
@@ -34,6 +52,9 @@ def _validate_response(response: dict[str, Any]) -> None:
3452
3553 status_code = response .get ("statusCode" )
3654
55+ if status_code == 401 :
56+ raise UnauthorizedError ("unauthorized, please request a new access token" )
57+
3758 if status_code == 500 :
3859 raise InternalServerError ("there was an interal server error" )
3960
@@ -44,22 +65,22 @@ def _set_header(self, header: dict) -> None:
4465 self ._headers = header
4566
4667 def _get (self , endpoint : str , params : dict = {}) -> dict :
47- url = self .URL + endpoint
68+ url = self .url + endpoint
4869 response = requests .get (url , headers = self ._headers , params = params )
4970 res = response .json ()
5071 self ._validate_response (res )
5172
5273 return res
5374
5475 def _post (self , endpoint : str ) -> tuple [dict , int ]:
55- url = self .URL + endpoint
76+ url = self .url + endpoint
5677 response = requests .post (url , headers = self ._headers )
5778 res = response .json ()
5879 self ._validate_response (res )
5980
6081 return (res , response .status_code )
6182
62- def _get_prices_till_page (
83+ def get_prices_till_page (
6384 self , page_limit : int , print_rate_limit : bool = False
6485 ) -> dict :
6586 prices = {}
@@ -83,7 +104,7 @@ def _get_prices_till_page(
83104 raise PricesTFError ("could not find any items in response" )
84105
85106 for item in response ["items" ]:
86- prices [item ["sku" ]] = self ._format_price (item )
107+ prices [item ["sku" ]] = self .format_price (item )
87108
88109 current_page = response ["meta" ]["currentPage" ] + 1
89110 total_pages = response ["meta" ]["totalPages" ]
@@ -107,16 +128,14 @@ def get_prices(self, page: int, limit: int = 100, order: str = "DESC") -> dict:
107128 return self ._get ("/prices" , {"page" : page , "limit" : limit , "order" : order })
108129
109130 def get_all_prices (self , print_rate_limit : bool = False ) -> dict :
110- return self ._get_prices_till_page (- 1 , print_rate_limit )
131+ return self .get_prices_till_page (- 1 , print_rate_limit )
111132
112133 def update_price (self , sku : str ) -> tuple [dict , int ]:
113134 return self ._post (f"/prices/{ sku } /refresh" )
114135
115136 def request_access_token (self ) -> None :
116137 res , _ = self ._post ("/auth/access" )
117-
118138 self ._validate_response (res )
119-
120139 self ._access_token = res ["accessToken" ]
121140
122141 self ._set_header (
0 commit comments