Skip to content

Commit 881c50b

Browse files
committed
new coll func
1 parent 4fda84c commit 881c50b

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

dsaps/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,22 @@ def search(ctx, field, string, search_type):
6363
models.elapsed_time(start_time, 'Elapsed time')
6464

6565

66+
@main.command()
67+
@click.option('-c', '--comm_handle', prompt='Enter the community handle',
68+
help='The handle of the community in which to create the ,'
69+
'collection.')
70+
@click.option('-n', '--coll_name', prompt='Enter the name of the collection',
71+
help='The name of the collection to be created.')
72+
@click.pass_context
73+
def newcoll(ctx, comm_handle, coll_name):
74+
client = ctx.obj['client']
75+
coll_id = client.post_coll_to_comm(comm_handle, coll_name)
76+
logger.info(coll_id)
77+
# STEPS TO ADD
78+
# post items to collections
79+
# post bistreams to item_links
80+
# post prov notes
81+
82+
6683
if __name__ == '__main__':
6784
main()

dsaps/models.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616

1717
class Client:
1818
def __init__(self, url):
19-
self.url = url
19+
header = {'content-type': 'application/json', 'accept':
20+
'application/json'}
21+
self.url = url.rstrip('/')
22+
self.cookies = None
23+
self.header = header
2024
logger.info('Initializing client')
2125

2226
def authenticate(self, email, password):
27+
header = self.header
2328
data = {'email': email, 'password': password}
24-
header = {'content-type': 'application/json', 'accept':
25-
'application/json'}
26-
session = requests.post(self.url + '/rest/login', headers=header,
29+
session = requests.post(f'{self.url}/login', headers=header,
2730
params=data).cookies['JSESSIONID']
2831
cookies = {'JSESSIONID': session}
29-
status = requests.get(self.url + '/rest/status', headers=header,
32+
status = requests.get(f'{self.url}/status', headers=header,
3033
cookies=cookies).json()
3134
self.user_full_name = status['fullname']
3235
self.cookies = cookies
@@ -35,7 +38,7 @@ def authenticate(self, email, password):
3538

3639
def get_record(self, uuid, rec_type):
3740
"""Retrieve an individual record of a particular type."""
38-
url = f'{self.url}/rest/{rec_type}/{uuid}?expand=all'
41+
url = f'{self.url}/{rec_type}/{uuid}?expand=all'
3942
record = requests.get(url, headers=self.header,
4043
cookies=self.cookies).json()
4144
if rec_type == 'items':
@@ -55,7 +58,7 @@ def filtered_item_search(self, key, string, query_type,
5558
items = ''
5659
item_links = []
5760
while items != []:
58-
endpoint = f'{self.url}/rest/filtered-items?'
61+
endpoint = f'{self.url}/filtered-items?'
5962
params = {'query_field[]': key, 'query_op[]': query_type,
6063
'query_val[]': string, '&collSel[]':
6164
selected_collections, 'limit': 200, 'offset': offset}
@@ -71,6 +74,17 @@ def filtered_item_search(self, key, string, query_type,
7174
offset = offset + 200
7275
return item_links
7376

77+
def post_coll_to_comm(self, comm_handle, coll_name):
78+
endpoint = f'{self.url}/handle/{comm_handle}'
79+
community = requests.get(endpoint, headers=self.header,
80+
cookies=self.cookies).json()
81+
comm_id = community['uuid']
82+
collection = {'name': coll_name}
83+
endpoint2 = f'{self.url}/communities/{comm_id}/collections'
84+
coll_id = requests.post(endpoint2, headers=self.header,
85+
cookies=self.cookies, json=collection).json()
86+
return coll_id['link']
87+
7488
def _pop_inst(self, class_type, rec_obj):
7589
"""Populate class instance with data from record."""
7690
fields = [op(field) for field in attr.fields(class_type)]

tests/test_models.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@pytest.fixture
99
def client():
10-
client = models.Client('mock://example.com')
10+
client = models.Client('mock://example.com/')
1111
client.header = {}
1212
client.cookies = {}
1313
client.user_full_name = ''
@@ -17,26 +17,21 @@ def client():
1717
def test_authenticate(client):
1818
"""Test authenticate function."""
1919
with requests_mock.Mocker() as m:
20-
url1 = '/rest/login'
21-
url2 = '/rest/status'
2220
email = 'test@test.mock'
2321
password = '1234'
24-
header = {'content-type': 'application/json', 'accept':
25-
'application/json'}
2622
cookies = {'JSESSIONID': '11111111'}
2723
json_object = {'fullname': 'User Name'}
28-
m.post(url1, cookies=cookies)
29-
m.get(url2, json=json_object)
24+
m.post('mock://example.com/login', cookies=cookies)
25+
m.get('mock://example.com/status', json=json_object)
3026
client.authenticate(email, password)
3127
assert client.user_full_name == 'User Name'
3228
assert client.cookies == cookies
33-
assert client.header == header
3429

3530

3631
def test_get_record(client):
3732
"""Test get_record function."""
3833
with requests_mock.Mocker() as m:
39-
uri = '/rest/items/123?expand=all'
34+
uri = 'mock://example.com/items/123?expand=all'
4035
json_object = {'metadata': {'title': 'Sample title'}, 'type': 'item'}
4136
m.get(uri, json=json_object)
4237
rec_obj = client.get_record('123', 'items')
@@ -49,7 +44,7 @@ def test_filtered_item_search(client):
4944
key = 'dc.title'
5045
string = 'test'
5146
query_type = 'contains'
52-
endpoint = '/rest/filtered-items?'
47+
endpoint = 'mock://example.com/filtered-items?'
5348
json_object_1 = {'items': [{'link': '1234'}]}
5449
json_object_2 = {'items': []}
5550
m.get(endpoint, [{'json': json_object_1}, {'json': json_object_2}])
@@ -59,6 +54,19 @@ def test_filtered_item_search(client):
5954
assert '1234' in item_links
6055

6156

57+
def test_post_coll_to_comm(client):
58+
with requests_mock.Mocker() as m:
59+
comm_handle = '1234'
60+
coll_name = 'Test Collection'
61+
json_object_1 = {'uuid': 'a1b2'}
62+
json_object_2 = {'link': '5678'}
63+
m.get('mock://example.com/handle/1234', json=json_object_1)
64+
m.post('mock://example.com/communities/a1b2/collections',
65+
json=json_object_2)
66+
coll_id = client.post_coll_to_comm(comm_handle, coll_name)
67+
assert coll_id == '5678'
68+
69+
6270
def test__pop_inst(client):
6371
"""Test _pop_inst function."""
6472
class_type = models.Collection

0 commit comments

Comments
 (0)