Skip to content

Commit

Permalink
Merge pull request #15 from suchmax/master
Browse files Browse the repository at this point in the history
Add support for custom inventory provider
  • Loading branch information
offish authored Mar 26, 2024
2 parents b85c24e + ee748d4 commit 7a2c8c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ provider = Inventory("steamsupply", "9st947vs0qmgfpeqde1gj92l0oqmhysm")
# using steam as inventory provider
provider = Inventory() # or Inventory("steamcommunity")

# using a custom api as inventory provider
# requests will be sent to {url}/inventory/{steam_id}/{app_id}/{context_id}?api_key=apikey
provider = Inventory("http://localhost:8000", "9st947vs0qmgfpeqde1gj92l0oqmhysm")

# get an inventory
inventory = provider.fetch("76561198253325712")
```
Expand Down Expand Up @@ -124,4 +128,4 @@ socket.listen()
```bash
# tf2-utils/
python -m unittest
```
```
6 changes: 6 additions & 0 deletions src/tf2_utils/inventory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .providers.steamcommunity import SteamCommunity
from .providers.steamsupply import SteamSupply
from .providers.steamapis import SteamApis
from .providers.custom import Custom
from .sku import get_sku

import requests
Expand Down Expand Up @@ -45,6 +46,11 @@ def __init__(
# already set
return

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

# loop through providers create object
for i in self.PROVIDERS:
if provider_name.lower() == i.__name__.lower():
Expand Down
12 changes: 12 additions & 0 deletions src/tf2_utils/providers/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Custom:
def __init__(self, api_key: str, url: str) -> None:
self.api_key = api_key
self.url = url.rstrip('/')

def get_url_and_params(
self, steam_id: str, app_id: int, context_id: int
) -> tuple[str, dict]:
return (
f"{self.url}/inventory/{steam_id}/{app_id}/{context_id}",
{"api_key": self.api_key},
)

0 comments on commit 7a2c8c3

Please sign in to comment.