diff --git a/dsaps/cli.py b/dsaps/cli.py index e209cc1..2f4a9f5 100644 --- a/dsaps/cli.py +++ b/dsaps/cli.py @@ -63,5 +63,22 @@ def search(ctx, field, string, search_type): models.elapsed_time(start_time, 'Elapsed time') +@main.command() +@click.option('-c', '--comm_handle', prompt='Enter the community handle', + help='The handle of the community in which to create the ,' + 'collection.') +@click.option('-n', '--coll_name', prompt='Enter the name of the collection', + help='The name of the collection to be created.') +@click.pass_context +def newcoll(ctx, comm_handle, coll_name): + client = ctx.obj['client'] + coll_id = client.post_coll_to_comm(comm_handle, coll_name) + logger.info(coll_id) + # STEPS TO ADD + # post items to collections + # post bistreams to item_links + # post prov notes + + if __name__ == '__main__': main() diff --git a/dsaps/models.py b/dsaps/models.py index 873174d..d399d56 100644 --- a/dsaps/models.py +++ b/dsaps/models.py @@ -16,17 +16,20 @@ class Client: def __init__(self, url): - self.url = url + header = {'content-type': 'application/json', 'accept': + 'application/json'} + self.url = url.rstrip('/') + self.cookies = None + self.header = header logger.info('Initializing client') def authenticate(self, email, password): + header = self.header data = {'email': email, 'password': password} - header = {'content-type': 'application/json', 'accept': - 'application/json'} - session = requests.post(self.url + '/rest/login', headers=header, + session = requests.post(f'{self.url}/login', headers=header, params=data).cookies['JSESSIONID'] cookies = {'JSESSIONID': session} - status = requests.get(self.url + '/rest/status', headers=header, + status = requests.get(f'{self.url}/status', headers=header, cookies=cookies).json() self.user_full_name = status['fullname'] self.cookies = cookies @@ -35,7 +38,7 @@ def authenticate(self, email, password): def get_record(self, uuid, rec_type): """Retrieve an individual record of a particular type.""" - url = f'{self.url}/rest/{rec_type}/{uuid}?expand=all' + url = f'{self.url}/{rec_type}/{uuid}?expand=all' record = requests.get(url, headers=self.header, cookies=self.cookies).json() if rec_type == 'items': @@ -55,7 +58,7 @@ def filtered_item_search(self, key, string, query_type, items = '' item_links = [] while items != []: - endpoint = f'{self.url}/rest/filtered-items?' + endpoint = f'{self.url}/filtered-items?' params = {'query_field[]': key, 'query_op[]': query_type, 'query_val[]': string, '&collSel[]': selected_collections, 'limit': 200, 'offset': offset} @@ -71,6 +74,17 @@ def filtered_item_search(self, key, string, query_type, offset = offset + 200 return item_links + def post_coll_to_comm(self, comm_handle, coll_name): + endpoint = f'{self.url}/handle/{comm_handle}' + community = requests.get(endpoint, headers=self.header, + cookies=self.cookies).json() + comm_id = community['uuid'] + collection = {'name': coll_name} + endpoint2 = f'{self.url}/communities/{comm_id}/collections' + coll_id = requests.post(endpoint2, headers=self.header, + cookies=self.cookies, json=collection).json() + return coll_id['link'] + def _pop_inst(self, class_type, rec_obj): """Populate class instance with data from record.""" fields = [op(field) for field in attr.fields(class_type)] diff --git a/tests/test_models.py b/tests/test_models.py index 403ce8b..ba41103 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,7 +7,7 @@ @pytest.fixture def client(): - client = models.Client('mock://example.com') + client = models.Client('mock://example.com/') client.header = {} client.cookies = {} client.user_full_name = '' @@ -17,26 +17,21 @@ def client(): def test_authenticate(client): """Test authenticate function.""" with requests_mock.Mocker() as m: - url1 = '/rest/login' - url2 = '/rest/status' email = 'test@test.mock' password = '1234' - header = {'content-type': 'application/json', 'accept': - 'application/json'} cookies = {'JSESSIONID': '11111111'} json_object = {'fullname': 'User Name'} - m.post(url1, cookies=cookies) - m.get(url2, json=json_object) + m.post('mock://example.com/login', cookies=cookies) + m.get('mock://example.com/status', json=json_object) client.authenticate(email, password) assert client.user_full_name == 'User Name' assert client.cookies == cookies - assert client.header == header def test_get_record(client): """Test get_record function.""" with requests_mock.Mocker() as m: - uri = '/rest/items/123?expand=all' + uri = 'mock://example.com/items/123?expand=all' json_object = {'metadata': {'title': 'Sample title'}, 'type': 'item'} m.get(uri, json=json_object) rec_obj = client.get_record('123', 'items') @@ -49,7 +44,7 @@ def test_filtered_item_search(client): key = 'dc.title' string = 'test' query_type = 'contains' - endpoint = '/rest/filtered-items?' + endpoint = 'mock://example.com/filtered-items?' json_object_1 = {'items': [{'link': '1234'}]} json_object_2 = {'items': []} m.get(endpoint, [{'json': json_object_1}, {'json': json_object_2}]) @@ -59,6 +54,19 @@ def test_filtered_item_search(client): assert '1234' in item_links +def test_post_coll_to_comm(client): + with requests_mock.Mocker() as m: + comm_handle = '1234' + coll_name = 'Test Collection' + json_object_1 = {'uuid': 'a1b2'} + json_object_2 = {'link': '5678'} + m.get('mock://example.com/handle/1234', json=json_object_1) + m.post('mock://example.com/communities/a1b2/collections', + json=json_object_2) + coll_id = client.post_coll_to_comm(comm_handle, coll_name) + assert coll_id == '5678' + + def test__pop_inst(client): """Test _pop_inst function.""" class_type = models.Collection