Skip to content

Commit

Permalink
TEMP move querylogic into _resolve, execute ...
Browse files Browse the repository at this point in the history
...wraps around resolve to catch exceptions, reorganize self.url and
self.params logic, fixup test to produce expected output
  • Loading branch information
ingwinlu committed Jul 17, 2016
1 parent 71086b3 commit 771a137
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def test_query_url_replacement(self):
q.add_urlkw('channel','winlu')
self.assertEqual(
q.url,
'https://api.twitch.tv/kraken/stream/winlu')
'https://api.twitch.tv/kraken/stream/winlu?')
25 changes: 17 additions & 8 deletions twitch/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def __init__(self, url):
@property
def url(self):
formatted_url = self._url.format(**self._urlkws) # throws KeyError
return formatted_url
encoded_params = urlencode(self.params)
full_url = '?'.join([formatted_url, encoded_params])
return full_url

@property
def headers(self):
Expand All @@ -46,7 +48,7 @@ def update_headers(self, additional_headers):

@property
def params(self):
return urlencode(self._params)
return self._params

@property
def urlkws(self):
Expand All @@ -72,10 +74,17 @@ def __str__(self):
url=self.url, params=self.params, headers=self.headers)

def execute(self):
'''Executes the query by building the full url and making a Request'''
full_url = '?'.join([self.url, self.params])
log.debug('Querying ' + full_url)
request = Request(full_url, headers=self.headers)
'''Executes the Query, wraps resolve in try catch'''
try:
return self._resolve()
except URLError:
raise ResourceUnavailableException(str(self))


def _resolve(self):
'''Resolves the Query and tries to return data'''
log.debug('Querying ' + self.url)
request = Request(self.url, headers=self.headers)
answer = ""

for _ in range(MAX_RETRIES):
Expand All @@ -98,8 +107,8 @@ def execute(self):


class JsonQuery(Query):
def execute(self):
raw_json = super(JsonQuery, self).execute()
def _resolve(self):
raw_json = super(JsonQuery, self)._resolve()
jsonDict = json.loads(raw_json)
log.debug(json.dumps(jsonDict, indent=4))
return jsonDict
Expand Down

0 comments on commit 771a137

Please sign in to comment.