Skip to content

Commit

Permalink
Merge pull request #16 from uc-cdis/fix/get-params
Browse files Browse the repository at this point in the history
Fix/get params
  • Loading branch information
Avantol13 authored Feb 28, 2018
2 parents b42f4e8 + 9c171eb commit 39fde06
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
16 changes: 14 additions & 2 deletions indexclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from urllib.parse import urljoin

import requests

import copy

UPDATABLE_ATTRS = ['file_name', 'urls', 'version']

Expand Down Expand Up @@ -90,8 +90,20 @@ def get_with_params(self, params=None):
Return a document object corresponding to the supplied parameters, such
as ``{'hashes': {'md5': '...', 'size': '...'}}``.
"""
# need to include all the hashes in the request
# index client like signpost or indexd will need to handle the
# query param `'hash': 'hash_type:hash'`
params_copy = copy.deepcopy(params)
reformatted_params = dict()
if 'hashes' in params_copy:
reformatted_params['hash'] = []
for hash_type, hash in params_copy['hashes'].items():
reformatted_params['hash'].append(str(hash_type) + ':' + str(hash))
del params_copy['hashes']
reformatted_params.update(params_copy)

try:
response = self._get("index", params=params)
response = self._get('index', params=reformatted_params)
except requests.HTTPError as e:
if e.response.status_code == 404:
return None
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest==3.0.6
mock
-e git+https://github.com/uc-cdis/[email protected]#egg=cdisutilstest
-e git+https://github.com/uc-cdis/[email protected]#egg=cdispyutils
-e git+https://github.com/uc-cdis/[email protected]#egg=indexd
34 changes: 34 additions & 0 deletions tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# Python 2 and 3 compatible
try:
from unittest.mock import patch
except ImportError:
from mock import patch


def test_import_index():
'''
Try to import the index package.
'''
import indexclient


@patch("indexclient.client.handle_error")
@patch("requests.get")
def test_hashes(get_request_mock, handle_error_mock):
from indexclient.client import IndexClient
input_params = {
'hashes': {
'md5': '00000000000000000000000000000001'
},
'size': '1'
}

expected_format = {
'hash': [
'md5:00000000000000000000000000000001'
],
'size': '1'
}

with patch("indexclient.client.IndexClient._get") as get_mock:
client = IndexClient('base_url')
client.get_with_params(input_params)

assert get_mock.called
args, kwargs = get_mock.call_args_list[0]
assert kwargs['params'] == expected_format

0 comments on commit 39fde06

Please sign in to comment.