Skip to content

Commit 5ee01a1

Browse files
author
Michael Barr
committed
Updated exceptions.py by formatting the comments (some dealt with Issue #3). Fixed a bug in URL formatting where the logic allowed appending the '&format=<name>' multiple times. Updated test_core.py to test the content-type returned in the header of the request. This will make sure that we are getting the correct response type for XML, JSON, and VDF requests (Issue #4).
1 parent c4ef632 commit 5ee01a1

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

steamwebapi/core.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def __init__(self, interface, method, method_version=1, httpmethod='GET',
126126
self._version = 'v{0:04d}'.format(method_version)
127127
self._parameters = parameters
128128
self._httpmethod = str(httpmethod).upper()
129-
self._url = self._encode_url()
129+
130+
# Set self._url
131+
self._encode_url()
130132

131133
def _encode_url(self):
132134
""".. method:: _encode_url()
@@ -135,12 +137,12 @@ def _encode_url(self):
135137
136138
:returns: url
137139
"""
138-
return 'http://api.steampowered.com/{0}/{1}/{2}/?{3}'.format(
140+
self._url = 'http://api.steampowered.com/{0}/{1}/{2}/?{3}'.format(
139141
self._interface,
140142
self._method,
141143
self._version,
142144
# urllib.urlencode(self._parameters)
143-
urlencode(self._parameters)
145+
urlencode(self._parameters),
144146
)
145147

146148
@property
@@ -215,9 +217,9 @@ def xml(self):
215217
return self.as_xml()
216218

217219
@property
218-
def json(self, load=False):
220+
def json(self):
219221
""".. attribute:: json"""
220-
return self.as_json(load)
222+
return self.as_json()
221223

222224
@property
223225
def vdf(self):
@@ -232,7 +234,7 @@ def as_xml(self):
232234
self._url += '&format=xml'
233235
return self._execute_query()
234236

235-
def as_json(self, load=False):
237+
def as_json(self):
236238
""".. method:: as_json()"""
237239
# We need to encode the URL again to prevent appending the format
238240
# multiple times.

steamwebapi/util/exceptions.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
"""
2+
.. module:: exceptions
3+
:platform: Unix, Windows
4+
:synopsis: Contains the API's "core" classes for constructing queries.
5+
6+
.. moduleauthor:: Michael Barr <[email protected]>
7+
8+
"""
9+
# =============================================================================
10+
# >> EXCEPTIONS
11+
# =============================================================================
112
class SteamWebAPIError(Exception):
13+
"""The base Exception for the steamwebapi package."""
214
pass
315

416

tests/test_core.py

+52
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,58 @@ def test_default_language_initialized_from__init__no_keyword_arg(self):
188188
)
189189

190190

191+
class TestAPIQuery(TestCase):
192+
"""Tests the core.APIQuery() class to ensure the correct content-type is
193+
returned. We utilize ISteamWebAPIUtil().GetSupportedAPIList() as it
194+
does not require a key or arguments, and should always be available.
195+
196+
"""
197+
198+
def setUp(self):
199+
"""Initializes an APIQuery instance."""
200+
from steamwebapi.core import APIQuery
201+
self.apiquery = APIQuery(
202+
interface='ISteamWebAPIUtil',
203+
method='GetSupportedAPIList',
204+
method_version=1,
205+
httpmethod='GET',
206+
parameters={},
207+
)
208+
209+
def test_api_query_return_xml(self):
210+
"""Tests that calling the XML format returns the correct
211+
content-type.
212+
213+
"""
214+
self.assertEqual(
215+
self.apiquery.xml.headers['content-type'],
216+
'text/xml; charset=UTF-8',
217+
'The content-type headers returned do not match the XML type.',
218+
)
219+
220+
def test_api_query_return_json(self):
221+
"""Tests that calling the JSON format returns the correct
222+
content-type.
223+
224+
"""
225+
self.assertEqual(
226+
self.apiquery.json.headers['content-type'],
227+
'application/json; charset=UTF-8',
228+
'The content-type headers returned do not match the JSON type.',
229+
)
230+
231+
def test_api_query_return_vdf(self):
232+
"""Tests that calling the VDF format returns the correct
233+
content-type.
234+
235+
"""
236+
self.assertEqual(
237+
self.apiquery.vdf.headers['content-type'],
238+
'text/vdf; charset=UTF-8',
239+
'The content-type headers returned do not match the VDF type.',
240+
)
241+
242+
191243
# =============================================================================
192244
# >> TEST FUNCTIONS
193245
# =============================================================================

0 commit comments

Comments
 (0)