Skip to content

Commit 633f5a0

Browse files
authored
Merge pull request #18 from offish/v2.3.1
v2.3.1
2 parents c6d1708 + b3bc017 commit 633f5a0

File tree

13 files changed

+146
-27
lines changed

13 files changed

+146
-27
lines changed

conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
MARKETPLACE_TF_API_KEY = getenv("MARKETPLACE_TF_API_KEY")
99
BACKPACK_TF_TOKEN = getenv("BACKPACK_TF_TOKEN")
10+
EXPRESS_LOAD_API_KEY = getenv("EXPRESS_LOAD_API_KEY")
11+
12+
13+
@pytest.fixture
14+
def steam_id() -> str:
15+
return "76561198253325712"
1016

1117

1218
@pytest.fixture
@@ -17,3 +23,8 @@ def marketplace_tf_api_key() -> str:
1723
@pytest.fixture
1824
def backpack_tf_token() -> str:
1925
return BACKPACK_TF_TOKEN
26+
27+
28+
@pytest.fixture
29+
def express_load_api_key() -> str:
30+
return EXPRESS_LOAD_API_KEY

example.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
MARKETPLACE_TF_API_KEY=apikey
2-
BACKPACK_TF_TOKEN=token
2+
BACKPACK_TF_TOKEN=token
3+
EXPRESS_LOAD_API_KEY=apikey

src/tf2_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# flake8: noqa
22
__title__ = "tf2-utils"
33
__author__ = "offish"
4-
__version__ = "2.3.0"
4+
__version__ = "2.3.1"
55
__license__ = "MIT"
66

77
from .currency import CurrencyExchange

src/tf2_utils/inventory.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from .exceptions import InvalidInventory
44
from .providers.custom import Custom
5-
from .providers.steamapis import SteamApis
5+
from .providers.providers import PROVIDERS
66
from .providers.steamcommunity import SteamCommunity
7-
from .providers.steamsupply import SteamSupply
87
from .sku import get_sku
98

109

@@ -39,12 +38,10 @@ def map_inventory(
3938

4039

4140
class Inventory:
42-
PROVIDERS = [SteamSupply, SteamApis]
43-
4441
def __init__(
4542
self, provider_name: str = "steamcommunity", api_key: str = ""
4643
) -> None:
47-
# set default provider for intellisense
44+
# default to steamcommunity
4845
self.provider = SteamCommunity()
4946

5047
# default to steam if no api_key is given
@@ -53,27 +50,23 @@ def __init__(
5350

5451
provider_name = provider_name.lower()
5552

56-
if provider_name == "steamcommunity":
57-
# already set
58-
return
59-
6053
# if provider_name is a url, assign it as a custom provider address
6154
if provider_name.startswith("http"):
6255
self.provider = Custom(api_key, provider_name)
6356
return
6457

6558
# loop through providers create object
66-
for i in self.PROVIDERS:
59+
for i in PROVIDERS:
6760
if provider_name == i.__name__.lower():
6861
# set the first found provider and then stop
6962
self.provider = i(api_key)
7063
break
7164

7265
def fetch(self, steam_id: str, app_id: int = 440, context_id: int = 2) -> dict:
7366
url, params = self.provider.get_url_and_params(steam_id, app_id, context_id)
74-
response = requests.get(url, params=params)
67+
response = requests.get(url, params=params, headers=self.provider.headers)
7568

7669
try:
7770
return response.json()
7871
except Exception as e:
79-
return {"error": str(e)}
72+
return {"success": False, "error": str(e)}

src/tf2_utils/providers/custom.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
class Custom:
1+
from .provider import Provider
2+
3+
4+
class Custom(Provider):
25
def __init__(self, api_key: str, url: str) -> None:
3-
self.api_key = api_key
6+
super().__init__(api_key)
47
self.url = url.rstrip("/")
58

69
def get_url_and_params(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .provider import Provider
2+
3+
4+
class ExpressLoad(Provider):
5+
def __init__(self, api_key: str = ""):
6+
super().__init__(api_key)
7+
self.headers = {"X-API-Key": self.api_key}
8+
9+
def get_url_and_params(
10+
self, steam_id: str, app_id: int, context_id: int
11+
) -> tuple[str, dict]:
12+
return (
13+
f"https://api.express-load.com/inventory/{steam_id}/{app_id}/{context_id}",
14+
{},
15+
)

src/tf2_utils/providers/provider.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Provider(ABC):
5+
def __init__(self, api_key: str = "") -> None:
6+
self.api_key = api_key
7+
self.headers = {}
8+
9+
@abstractmethod
10+
def get_url_and_params(
11+
self, steam_id: str, app_id: int, context_id: int
12+
) -> tuple[str, dict]:
13+
pass

src/tf2_utils/providers/providers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .express_load import ExpressLoad
2+
from .steam_supply import SteamSupply
3+
from .steamapis import SteamApis
4+
from .steamcommunity import SteamCommunity
5+
6+
PROVIDERS = set([ExpressLoad, SteamSupply, SteamApis, SteamCommunity])

src/tf2_utils/providers/steamsupply.py renamed to src/tf2_utils/providers/steam_supply.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
class SteamSupply:
2-
def __init__(self, api_key: str) -> None:
3-
self.api_key = api_key
1+
from .provider import Provider
42

3+
4+
class SteamSupply(Provider):
55
def get_url_and_params(
66
self, steam_id: str, app_id: int, context_id: int
77
) -> tuple[str, dict]:

src/tf2_utils/providers/steamapis.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
class SteamApis:
2-
def __init__(self, api_key: str) -> None:
3-
self.api_key = api_key
1+
from .provider import Provider
42

3+
4+
class SteamApis(Provider):
55
def get_url_and_params(
66
self, steam_id: str, app_id: int, context_id: int
77
) -> tuple[str, dict]:
8-
url = "https://api.steamapis.com/steam/inventory/{}/{}/{}"
98
return (
10-
url.format(steam_id, app_id, context_id),
9+
f"https://api.steamapis.com/steam/inventory/{steam_id}/{app_id}/{context_id}",
1110
{"api_key": self.api_key},
1211
)

0 commit comments

Comments
 (0)