Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

MARKETPLACE_TF_API_KEY = getenv("MARKETPLACE_TF_API_KEY")
BACKPACK_TF_TOKEN = getenv("BACKPACK_TF_TOKEN")
EXPRESS_LOAD_API_KEY = getenv("EXPRESS_LOAD_API_KEY")


@pytest.fixture
def steam_id() -> str:
return "76561198253325712"


@pytest.fixture
Expand All @@ -17,3 +23,8 @@ def marketplace_tf_api_key() -> str:
@pytest.fixture
def backpack_tf_token() -> str:
return BACKPACK_TF_TOKEN


@pytest.fixture
def express_load_api_key() -> str:
return EXPRESS_LOAD_API_KEY
3 changes: 2 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MARKETPLACE_TF_API_KEY=apikey
BACKPACK_TF_TOKEN=token
BACKPACK_TF_TOKEN=token
EXPRESS_LOAD_API_KEY=apikey
2 changes: 1 addition & 1 deletion src/tf2_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flake8: noqa
__title__ = "tf2-utils"
__author__ = "offish"
__version__ = "2.3.0"
__version__ = "2.3.1"
__license__ = "MIT"

from .currency import CurrencyExchange
Expand Down
17 changes: 5 additions & 12 deletions src/tf2_utils/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from .exceptions import InvalidInventory
from .providers.custom import Custom
from .providers.steamapis import SteamApis
from .providers.providers import PROVIDERS
from .providers.steamcommunity import SteamCommunity
from .providers.steamsupply import SteamSupply
from .sku import get_sku


Expand Down Expand Up @@ -39,12 +38,10 @@ def map_inventory(


class Inventory:
PROVIDERS = [SteamSupply, SteamApis]

def __init__(
self, provider_name: str = "steamcommunity", api_key: str = ""
) -> None:
# set default provider for intellisense
# default to steamcommunity
self.provider = SteamCommunity()

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

provider_name = provider_name.lower()

if provider_name == "steamcommunity":
# already set
return

# if provider_name is a url, assign it as a custom provider address
if provider_name.startswith("http"):
self.provider = Custom(api_key, provider_name)
return

# loop through providers create object
for i in self.PROVIDERS:
for i in PROVIDERS:
if provider_name == i.__name__.lower():
# set the first found provider and then stop
self.provider = i(api_key)
break

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

try:
return response.json()
except Exception as e:
return {"error": str(e)}
return {"success": False, "error": str(e)}
7 changes: 5 additions & 2 deletions src/tf2_utils/providers/custom.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Custom:
from .provider import Provider


class Custom(Provider):
def __init__(self, api_key: str, url: str) -> None:
self.api_key = api_key
super().__init__(api_key)
self.url = url.rstrip("/")

def get_url_and_params(
Expand Down
15 changes: 15 additions & 0 deletions src/tf2_utils/providers/express_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .provider import Provider


class ExpressLoad(Provider):
def __init__(self, api_key: str = ""):
super().__init__(api_key)
self.headers = {"X-API-Key": self.api_key}

def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
return (
f"https://api.express-load.com/inventory/{steam_id}/{app_id}/{context_id}",
{},
)
13 changes: 13 additions & 0 deletions src/tf2_utils/providers/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from abc import ABC, abstractmethod


class Provider(ABC):
def __init__(self, api_key: str = "") -> None:
self.api_key = api_key
self.headers = {}

@abstractmethod
def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
pass
6 changes: 6 additions & 0 deletions src/tf2_utils/providers/providers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .express_load import ExpressLoad
from .steam_supply import SteamSupply
from .steamapis import SteamApis
from .steamcommunity import SteamCommunity

PROVIDERS = set([ExpressLoad, SteamSupply, SteamApis, SteamCommunity])
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class SteamSupply:
def __init__(self, api_key: str) -> None:
self.api_key = api_key
from .provider import Provider


class SteamSupply(Provider):
def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
Expand Down
9 changes: 4 additions & 5 deletions src/tf2_utils/providers/steamapis.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class SteamApis:
def __init__(self, api_key: str) -> None:
self.api_key = api_key
from .provider import Provider


class SteamApis(Provider):
def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
url = "https://api.steamapis.com/steam/inventory/{}/{}/{}"
return (
url.format(steam_id, app_id, context_id),
f"https://api.steamapis.com/steam/inventory/{steam_id}/{app_id}/{context_id}",
{"api_key": self.api_key},
)
6 changes: 3 additions & 3 deletions src/tf2_utils/providers/steamcommunity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class SteamCommunity:
def __init__(self, api_key: str = "") -> None:
pass # we dont care about api_key for steam
from .provider import Provider


class SteamCommunity(Provider):
def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_inventory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from src.tf2_utils import InvalidInventory, map_inventory
from src.tf2_utils.inventory import Inventory
from src.tf2_utils.utils import read_json_file

INVENTORY = read_json_file("./tests/json/bot_inventory.json")
Expand All @@ -17,3 +18,56 @@ def test_inventory() -> None:
def test_raises_error() -> None:
with pytest.raises(InvalidInventory):
map_inventory({}, False)


def test_default_inventory_provider() -> None:
inventory = Inventory()
assert inventory.provider.__class__.__name__ == "SteamCommunity"
assert inventory.provider.api_key == ""


def test_steamcommunity_inventory_provider() -> None:
inventory = Inventory("steamcommunity", "api_key")
assert inventory.provider.__class__.__name__ == "SteamCommunity"
assert inventory.provider.api_key == "api_key"


def test_express_load_inventory_provider() -> None:
inventory = Inventory("expressload", "api_key")
assert inventory.provider.__class__.__name__ == "ExpressLoad"
assert inventory.provider.api_key == "api_key"


def test_steamsupply_inventory_provider() -> None:
inventory = Inventory("steamsupply", "api_key")
assert inventory.provider.__class__.__name__ == "SteamSupply"
assert inventory.provider.api_key == "api_key"


def test_custom_inventory_provider() -> None:
inventory = Inventory("http://example.com", "api_key")
assert inventory.provider.__class__.__name__ == "Custom"
assert inventory.provider.api_key == "api_key"


def test_steamapis_inventory_provider() -> None:
inventory = Inventory("steamapis", "api_key")
assert inventory.provider.__class__.__name__ == "SteamApis"
assert inventory.provider.api_key == "api_key"


def test_steamcommunity_inventory_fetch(steam_id: str) -> None:
provider = Inventory("steamcommunity")
inventory = provider.fetch(steam_id)

assert "assets" in inventory
assert "descriptions" in inventory


def test_express_load_inventory_fetch(express_load_api_key: str, steam_id: str) -> None:
provider = Inventory("expressload", express_load_api_key)
inventory = provider.fetch(steam_id)

assert "success" in inventory
assert "assets" in inventory
assert "descriptions" in inventory
24 changes: 24 additions & 0 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from src.tf2_utils.providers.express_load import ExpressLoad
from src.tf2_utils.providers.steamcommunity import SteamCommunity


def test_steam_community_inventory(steam_id: str) -> None:
provider = SteamCommunity()
url, params = provider.get_url_and_params(steam_id, 440, 2)

assert provider.api_key == ""
assert url == f"https://steamcommunity.com/inventory/{steam_id}/440/2"
assert params == {
"l": "english",
"count": 5000,
}
assert provider.headers == {}


def test_express_load_inventory(express_load_api_key: str, steam_id: str) -> None:
provider = ExpressLoad(express_load_api_key)
url, params = provider.get_url_and_params(steam_id, 440, 2)

assert url == f"https://api.express-load.com/inventory/{steam_id}/440/2"
assert params == {}
assert provider.headers == {"X-API-Key": express_load_api_key}