Skip to content

Commit abdccb7

Browse files
author
Michael Barr
committed
Added a decorator for requiring an API key. Removed default format from core.py. Will need to remove this from settings.py in future commit.
1 parent 607b7ca commit abdccb7

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

steamwebapi/core.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import re
1616

1717
# API Imports
18-
from .settings import STEAM_API_KEY, DEFAULT_LANGUAGE, DEFAULT_DATA_FORMAT
18+
from .settings import STEAM_API_KEY, DEFAULT_LANGUAGE
1919
from .util.decorators import public
2020

2121

@@ -30,9 +30,8 @@
3030
# =============================================================================
3131
@public
3232
class SteamWebAPI(object):
33-
def __init__(self, key=STEAM_API_KEY, language=DEFAULT_LANGUAGE,
34-
data_format=DEFAULT_DATA_FORMAT):
35-
""".. method:: __init__(key=STEAM_API_KEY, language=DEFAULT_LANGUAGE, data_format=DEFAULT_DATA_FORMAT)
33+
def __init__(self, key=STEAM_API_KEY, language=DEFAULT_LANGUAGE):
34+
""".. method:: __init__(key=STEAM_API_KEY, language=DEFAULT_LANGUAGE)
3635
3736
API base class which contains the default settings for all queries.
3837
@@ -50,17 +49,9 @@ def __init__(self, key=STEAM_API_KEY, language=DEFAULT_LANGUAGE,
5049
if key and not API_KEY_RE.match(key):
5150
raise Exception('Bad Key')
5251

53-
# Make format lower case
54-
data_format = data_format.lower()
55-
56-
# Check the format and to make sure it is a valid format
57-
if data_format and not data_format in ('json', 'xml', 'vdf'):
58-
raise Exception('Invalid format.')
59-
6052
# Set the instance attributes
6153
self.key = key
6254
self.language = language
63-
self.data_format = data_format
6455

6556
def api_query(self, *args, **kwargs):
6657
""".. method:: api_query(interface, method, method_version=1, httpmethod='GET', parameters={})
@@ -83,12 +74,8 @@ def api_query(self, *args, **kwargs):
8374
:raises: ?
8475
8576
"""
86-
if not self.data_format:
87-
# Return the APIQuery instance
88-
return APIQuery(*args, **kwargs)
89-
90-
# Return an executed APIQuery call with the format of their choice
91-
return APIQuery(*args, **kwargs).__getattribute__(self.format)
77+
# Return the APIQuery instance
78+
return APIQuery(*args, **kwargs)
9279

9380

9481
@public
@@ -207,7 +194,7 @@ def url(self):
207194
def parameters(self):
208195
""".. attribute:: parameters
209196
210-
Returns private variable parameters (read-only).
197+
(str) - Returns private variable parameters (read-only).
211198
212199
(dict) The parameters for the query.
213200

steamwebapi/user.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
# API Imports
99
from .core import SteamWebAPI
10-
from .util.decorators import public
10+
from .util.decorators import public, api_key_required
1111

1212

1313
# =============================================================================
1414
# >> CLASSES
1515
# =============================================================================
1616
@public
1717
class ISteamUser(SteamWebAPI):
18+
@api_key_required
1819
def GetFriendList(self, steamid, relationship='all', method_version=1):
1920
""""""
2021
# Set up the parameters

steamwebapi/util/decorators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# =============================================================================
44
# Python Imports
55
import sys
6+
from functools import wraps
67

78

89
# =============================================================================
@@ -24,3 +25,10 @@ def public(f):
2425
return f
2526

2627
public(public) # Emulate decorating ourself (make public)
28+
29+
30+
def api_key_required(f):
31+
def decorator(self, *args, **kwargs):
32+
if not self.key:
33+
raise Exception('You dun fucked up.')
34+
return decorator
File renamed without changes.

0 commit comments

Comments
 (0)