Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Merge 4d0449d into 6072301
Browse files Browse the repository at this point in the history
  • Loading branch information
ayust committed Apr 25, 2016
2 parents 6072301 + 4d0449d commit 9a210e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
14 changes: 12 additions & 2 deletions evelink/api.py
Expand Up @@ -212,7 +212,9 @@ def put(self, key, value, duration):
class API(object):
"""A wrapper around the EVE API."""

def __init__(self, base_url="api.eveonline.com", cache=None, api_key=None, user_agent=None):
def __init__(self,
base_url="api.eveonline.com", cache=None,
api_key=None, user_agent=None, sso_token=None):
self.base_url = base_url
self.user_agent = _user_agent

Expand All @@ -228,6 +230,10 @@ def __init__(self, base_url="api.eveonline.com", cache=None, api_key=None, user_
if api_key and len(api_key) != 2:
raise ValueError("The provided API key must be a tuple of (keyID, vCode).")
self.api_key = api_key
if sso_token and len(sso_token) != 2:
# TODO: maybe allow omitting type somehow? For now this is easier.
raise ValueError("The provided SSO token must be a tuple of (token, type).")
self.sso_token = sso_token
self._set_last_timestamps()

def _set_last_timestamps(self, current_time=0, cached_until=0):
Expand All @@ -253,7 +259,11 @@ def get(self, path, params=None):
params = dict((k, _clean(v)) for k,v in params.items())

_log.debug("Calling %s with params=%r", path, params)
if self.api_key:
if self.sso_token:
_log.debug("SSO token added")
params['accessToken'] = self.sso_token[0]
params['accessType'] = self.sso_token[1]
elif self.api_key:
_log.debug("keyID and vCode added")
params['keyID'] = self.api_key[0]
params['vCode'] = self.api_key[1]
Expand Down
54 changes: 54 additions & 0 deletions tests/test_api.py
Expand Up @@ -197,6 +197,60 @@ def test_get_with_apikey(self, mock_urlopen):

self.assertEqual(request_dict, expected_request_dict)

@mock.patch('evelink.thirdparty.six.moves.urllib.request.urlopen')
def test_get_with_sso_token(self, mock_urlopen):
mock_urlopen.return_value.read.return_value = self.test_xml
self.cache.get.return_value = None

sso_token = (123, 'character')
api = evelink_api.API(cache=self.cache, sso_token=sso_token)

api.get('foo', {'a':[2,3,4]})

# Make sure the sso token and access type were passed
self.assertTrue(mock_urlopen.called)
self.assertTrue(len(mock_urlopen.call_args[0]) > 0)

request = mock_urlopen.call_args[0][0]
self.assertEqual(
'https://api.eveonline.com/foo.xml.aspx',
request.get_full_url()
)

request_dict = urllib.parse.parse_qs(request.data.decode())
expected_request_dict = urllib.parse.parse_qs(
"a=2%2C3%2C4&accessType=character&accessToken=123")

self.assertEqual(request_dict, expected_request_dict)

@mock.patch('evelink.thirdparty.six.moves.urllib.request.urlopen')
def test_get_with_sso_token_and_apikey(self, mock_urlopen):
mock_urlopen.return_value.read.return_value = self.test_xml
self.cache.get.return_value = None

sso_token = (123, 'character')
api_key = (1, 'code')
api = evelink_api.API(cache=self.cache, sso_token=sso_token, api_key=api_key)

api.get('foo', {'a':[2,3,4]})

# Make sure the sso token and access type were passed,
# and that the api key is not (SSO overrides).
self.assertTrue(mock_urlopen.called)
self.assertTrue(len(mock_urlopen.call_args[0]) > 0)

request = mock_urlopen.call_args[0][0]
self.assertEqual(
'https://api.eveonline.com/foo.xml.aspx',
request.get_full_url()
)

request_dict = urllib.parse.parse_qs(request.data.decode())
expected_request_dict = urllib.parse.parse_qs(
"a=2%2C3%2C4&accessType=character&accessToken=123")

self.assertEqual(request_dict, expected_request_dict)

@mock.patch('evelink.thirdparty.six.moves.urllib.request.urlopen')
def test_get_with_error(self, mock_urlopen):
# I had to go digging in the source code for urllib2 to find out
Expand Down

0 comments on commit 9a210e7

Please sign in to comment.