Let’s create our create_repo function.
Similar to our login() we need to create a data package, a request object and with “urlopen” send our request so we can get a response.
def create_repo(api_url, auth_token, name, repo_code, org_code = '', image_url = '', url = ''):
'''This function sends a create request to the ArchivesSpace REST API'''
data = json.JSONEncoder().encode({
'jsonmodel_type': 'repository',
'name': name,
'repo_code': repo_code,
'org_code': org_code,
'image_url': image_url,
'url': url
}).encode('utf-8')
req = urllib.request.Request(
url = api_url+'/repositories',
data = None,
headers = {'X-ArchivesSpace-Session': auth_token})
try:
response = urllib.request.urlopen(req, data)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read())
return ""
except urllib.error.URLError as e:
print(e.reason())
return ""
src = response.read().decode('utf-8')
return json.JSONDecoder().decode(src)