Skip to content

Commit

Permalink
Исправление ошибок (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bananchiki authored Sep 20, 2024
1 parent f2ec495 commit e8e28fd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='steam-trader',
version='0.3.0',
version='0.3.1',
author='Lemon4ksan (Bananchiki)',
author_email='senya20151718@gmail.com',
license='BSD License',
Expand Down
2 changes: 1 addition & 1 deletion steam_trader/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '0.3.0'
__version__ = '0.3.1'
__license__ = 'BSD 3-Clause License'
__copyright__ = 'Copyright (c) 2024-present, Lemon4ksan (aka Bananchiki) <https://github.com/Lemon4ksan>'
3 changes: 2 additions & 1 deletion steam_trader/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ def __init__(
self._httpx_client = None
self.proxy = proxy

def __enter__(self):
def __enter__(self) -> 'Client':
self._httpx_client = httpx.Client(proxy=self.proxy)
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self._httpx_client.close()
Expand Down
3 changes: 2 additions & 1 deletion steam_trader/_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def __init__(

self.proxy = proxy

async def __aenter__(self):
async def __aenter__(self) -> 'ClientAsync':
self._async_client = httpx.AsyncClient(proxy=self.proxy)
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._async_client.aclose()
Expand Down
44 changes: 33 additions & 11 deletions steam_trader/ext/_client_async_ext.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio
import logging
import functools
from typing import Optional, Sequence, TypeVar, Callable, Any
from typing import Optional, Sequence, TypeVar, Callable, Any, LiteralString

from ._misc import TradeMode, PriceRange

from steam_trader.constants import SUPPORTED_APPIDS
from steam_trader.exceptions import UnsupportedAppID
from steam_trader.exceptions import UnsupportedAppID, UnknownItem
from steam_trader import (
ClientAsync,
Filters,
Expand Down Expand Up @@ -250,26 +250,48 @@ async def set_trade_mode(self, state: int) -> 'TradeMode':
return TradeMode.de_json(result.json(), self)

@log
async def get_price_range(self, gid: int) -> 'PriceRange':
async def get_price_range(self, gid: int, *, mode: LiteralString = 'sell') -> 'PriceRange':
"""Получить размах цен в истории покупок. Проверяет только последние 100 покупок.
Args:
gid (:obj:`int`): ID группы предметов.
mode (:obj:`LiteralString`): Режим получения:
'sell' - Цены запросов на продажу. Значение по умолчанию.
'buy' - Цены запросов на покупку.
'history' - Цены из истории продаж. Максимум 100 пунктов.
Returns:
:NamedTuple:`PriceRange(lowest: float, highest: float)`: Размах цен в истории покупок.
Raises:
InternalError: При выполнении запроса произошла неизвестная ошибка.
UnknownItem: Неизвестный предмет.
ValueError: Указано недопустимое значение mode.
UnknownItem: Отсутствуют предложения о продаже/покупке или отсутствует история продаж.
"""

lowest = highest = None
sell_history = (await self.get_item_info(gid)).sell_history
for item in sell_history:
if lowest is None or item.price < lowest:
lowest = item.price
if highest is None or item.price > highest:
highest = item.price

match mode:
case 'sell':
sell_offers = (await self.get_order_book(gid)).sell
for item in sell_offers:
if lowest is None or item[0] < lowest:
lowest = item[0]
if highest is None or item[0] > highest:
highest = item[0]
case 'buy':
buy_offers = (await self.get_order_book(gid)).buy
for item in buy_offers:
if lowest is None or item[0] < lowest:
lowest = item[0]
if highest is None or item[0] > highest:
highest = item[0]
case 'history':
sell_history = (await self.get_item_info(gid)).sell_history
for item in sell_history:
if lowest is None or item.price < lowest:
lowest = item.price
if highest is None or item.price > highest:
highest = item.price
if lowest is None or highest is None:
raise UnknownItem('Отсутствуют предложения о продаже/покупке или отсутствует история продаж.')
return PriceRange(float(lowest), float(highest))
10 changes: 8 additions & 2 deletions steam_trader/ext/_client_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ._misc import TradeMode, PriceRange

from steam_trader.constants import SUPPORTED_APPIDS
from steam_trader.exceptions import UnsupportedAppID
from steam_trader.exceptions import UnsupportedAppID, UnknownItem
from steam_trader import (
Client,
Filters,
Expand Down Expand Up @@ -277,6 +277,11 @@ def get_price_range(self, gid: int, *, mode: LiteralString = 'sell') -> 'PriceRa
Returns:
:NamedTuple:`PriceRange(lowest: float, highest: float)`: Размах цен в истории покупок.
Raises:
InternalError: При выполнении запроса произошла неизвестная ошибка.
ValueError: Указано недопустимое значение mode.
UnknownItem: Отсутствуют предложения о продаже/покупке или отсутствует история продаж.
"""

lowest = highest = None
Expand All @@ -303,5 +308,6 @@ def get_price_range(self, gid: int, *, mode: LiteralString = 'sell') -> 'PriceRa
lowest = item.price
if highest is None or item.price > highest:
highest = item.price

if lowest is None or highest is None:
raise UnknownItem('Отсутствуют предложения о продаже/покупке или отсутствует история продаж.')
return PriceRange(float(lowest), float(highest))

0 comments on commit e8e28fd

Please sign in to comment.