Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Add publicKey URL argument and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matslindh committed Jan 3, 2017
1 parent 50ccc21 commit 34ec3ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions imboclient/test/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_images_url_with_user(self, mocked_url_images):
def test_images_url_with_user_used(self):
images_url = self._client_with_user.images_url()
assert images_url.url().startswith('http://imbo.local/users/foo/images')
assert 'publicKey=public' in images_url.url()

@patch('imboclient.url.image.UrlImage')
def test_image_url(self, mocked_url_image):
Expand All @@ -94,6 +95,7 @@ def test_image_url_with_user(self, mocked_url_image):
def test_image_url_with_user_used(self):
image_url = self._client_with_user.image_url('ff')
assert image_url.url().startswith('http://imbo.local/users/foo/images/ff?')
assert 'publicKey=public' in image_url.url()

@patch('imboclient.url.metadata.UrlMetadata')
def test_metadata_url(self, mocked_url_metadata):
Expand All @@ -110,6 +112,7 @@ def test_metadata_url_with_user(self, mocked_url_metadata):
def test_metadata_url_with_user_used(self):
metadata_url = self._client_with_user.metadata_url('ff')
assert metadata_url.url().startswith('http://imbo.local/users/foo/images/ff/metadata?')
assert 'publicKey=public' in metadata_url.url()

@patch('imboclient.header.authenticate.Authenticate.headers')
@patch('imboclient.url.images.UrlImages.url')
Expand Down
34 changes: 22 additions & 12 deletions imboclient/url/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, base_url, public_key, private_key, user=None):
self._public_key = public_key
self._private_key = private_key
self._user = user
self._query_params = None
self._query_params = []

self.access_token = accesstoken.AccessToken()

Expand All @@ -27,20 +27,26 @@ def user_url(self, resource):

def url(self):
url = self.resource_url()
query_string = self.query_string()

if self._query_params and len(self._query_params) > 0:
url = url + '?' + query_string
# create copy of list
params = list(self._query_params)

# if we have a user, we'll have to supply the public key as a GET argument
if self._user:
params.append(('publicKey', self._public_key))

query_string = self.query_stringify(params)

if query_string:
url += '?' + query_string

if self._public_key is None or self._private_key is None:
return url

generated_token = self.access_token.generate_token(url, self._private_key)
sep = '?' if not query_string else '&'

if self._query_params is None:
return url + '?accessToken=' + generated_token

return url + '&accessToken=' + generated_token
return url + sep + 'accessToken=' + generated_token

def add_query_param(self, key, value):
if self._query_params is None:
Expand All @@ -63,10 +69,7 @@ def add_query(self, query):
return self

def query_string(self):
if not self._query_params:
return ''

return urlencode(self._query_params)
return self.query_stringify(self._query_params)

def resource_url(self):
raise NotImplementedError("Missing implementation. You may want to use a Url implementation instead.")
Expand All @@ -75,3 +78,10 @@ def reset(self):
self._query_params = []

return self

@classmethod
def query_stringify(cls, parameters):
if not parameters:
return ''

return urlencode(parameters)

0 comments on commit 34ec3ad

Please sign in to comment.