In CapAPI
class, add a method for uploading a repository from URL.
Example of how it looks for publishing:
def publish(self, pid):
return self._make_request(url='deposits/{}/actions/publish'.format(pid),
expected_status_code=202,
method='post',
headers={'Content-Type': 'application/json',
'Accept': 'application/basic+json'})
For upload repository we need to make request with:
- an endpoint that should be called is
deposits/{pid}/actions/upload
- expected status code is 201
- method
post
- data (needs json.dumps)
url
webhook
(true|false)
event_type
(release|push)'
- headers
{'Content-Type': 'application/json', 'Accept': 'application/basic+json'}
Currently, we don't pass repositories field in basic serializer (that's the one you're asking for in headers) - so no way to validate response JSON - for now just check that status code was 201
.
We will decide in a separate thread on creating a serializer for repositories part.
Write tests to check how your method behaves in cases:
- 400 returned from the server (e.g. wrong URL like
http://nongithubhost.com
)
- just repo upload
- repo upload with push webhook
- repo upload with release webhook
- user doesn't have sufficient permission
Connect method to CLI, create a file like cap_client/cli/files_cli.py
. It needs registering click group, like:
@click.group()
def repositories():
"""Repositories managing commands."""
And then upload command registered under this group so you can call it like:
cap-client repositories upload my_url --webhook push
We can have a separate command for upload and creating webhook, or as a parameter like above (to be decided)
Remember to add a line at the end of cap_client/cli/__init__.py
file:
cli.add_command(repositories)
Without this one, you won't see your repositories command when calling cap-client
UPDATE let's do this one after cernanalysispreservation/analysispreservation.cern.ch#1547
then, you want to make a request in your method with the header ('Accept', 'application/repositories+json')
, like:
def upload_repository(self, pid, url, event_type=None):
"""Your method."""
return self._make_request(
url=f'deposits/{pid}/actions/upload',
data=json.dumps(
dict(url=url,
event_type=event_type if event_type else None,
webhook=True if event_type else False)),
method='post',
headers={
'Content-Type': 'application/json',
'Accept': 'application/repositories+json'
})
write yours tests according to repositories serializer format