Skip to content

Commit 91f1425

Browse files
committed
Fixed api_key_required decorator to be useable on instance methods. Changed urllib(2) code to use requests package. Added requirements.txt to store required packages. Added requests package to the requirements.txt
1 parent 6a9f8da commit 91f1425

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

requirements/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://pypi.python.org/pypi/requests
2+
# http://www.python-requests.org/en/latest/user/install/
3+
# -i http://simple.crate.io/ requests
4+
requests

steamwebapi/core.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
# >> IMPORTS
1111
# =============================================================================
1212
# Python Imports
13-
import urllib
14-
import urllib2
13+
#import urllib
14+
#import urllib2
1515
import re
16+
# Requests
17+
from requests import get as requests_get
18+
from requests import post as requests_post
19+
from requests.compat import urlencode
1620

1721
# API Imports
1822
from .settings import STEAM_API_KEY, DEFAULT_LANGUAGE
@@ -134,7 +138,8 @@ def _encode_url(self):
134138
self._interface,
135139
self._method,
136140
self._version,
137-
urllib.urlencode(self._parameters)
141+
# urllib.urlencode(self._parameters)
142+
urlencode(self._parameters)
138143
)
139144

140145
@property
@@ -245,9 +250,12 @@ def as_vdf(self):
245250
def _execute_query(self):
246251
# GET
247252
if self.httpmethod == 'GET':
248-
return urllib2.urlopen(self._url)
253+
# return urllib2.urlopen(self._url)
254+
return requests_get(self._url)
249255

250256
# POST
251-
data = urllib.urlencode(self.parameters)
252-
req = urllib2.Request(self._url, data)
253-
return urllib2.urlopen(req)
257+
# data = urllib.urlencode(self.parameters)
258+
data = urlencode(self.parameters)
259+
# req = urllib2.Request(self._url, data)
260+
# return urllib2.urlopen(req)
261+
return requests_post(self._url, data)

steamwebapi/user.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# =============================================================================
44
# Python Imports
55
import re
6-
from urllib2 import HTTPError
6+
from requests import HTTPError
77

88
# API Imports
99
from .core import SteamWebAPI

steamwebapi/util/decorators.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,26 @@ def public(f):
2929
public(public) # Emulate decorating ourself (make public)
3030

3131

32-
def api_key_required(f):
33-
def decorator(self, *args, **kwargs):
34-
if not self.key:
32+
class api_key_required(object):
33+
"""
34+
"""
35+
36+
def __init__(self, method):
37+
'''Store the method instance for further use'''
38+
self.method = method
39+
40+
def __get__(self, instance, owner):
41+
'''Return the class value instead of the methods directly'''
42+
return self.__class__(self.method.__get__(instance, owner))
43+
44+
def __call__(self, *args, **kwargs):
45+
'''Verifies that the key exists prior to calling the method'''
46+
47+
# Does the class have an API key
48+
if not self.method.__self__.key:
49+
50+
# Raise an error if no key is provided
3551
raise APIKeyRequiredError
36-
return decorator
52+
53+
# Call the method and return
54+
return self.method(*args, **kwargs)

0 commit comments

Comments
 (0)