CEDAR
Home page CEDAR
CEDAR APIs
CEDAR API and Python
Prerequisites:
- Create file called: secret.py
- Add the following line of code: CedarApiKey = 'apiKey key'
- Where key is a long hexastring. Literally after '=' it should read: 'apiKey 3453a54f345c345345'.
- You can find your CEDAR API key by logging in to your CEDAR account, then navigating to your profile page. Your API key, and basic information about using it, should be visible on this page.
- Add External Access rule and turn the rule on
Rule name
| IP Address
| Port
| Reason
|
Cedar
| 171.67.213.63
| 443
| Accessing Cedar
|
Python code
See also attachment below
- import json
- import secret
- # Open the Cedar template in your browser, paste the url between the triple quotes in its completeness
- # This script will extract the template-instance id (URL is non working example)
- template_instance_url = '''
- https://cedar.metadatacenter.org/instances/edit/https://repo.metadatacenter.org/template-instances/74eb7227-205d-48bd-b200-c79f356ac470?folderId=https:%2F%2Frepo.metadatacenter.org%2Ffolders%2F61f487eb-a59a-4ac8-bc28-21b38cb81ebb
- '''
- # Getting the Cedar Secret key from secert.py
- CedarApiKey = secret.CedarApiKey
- # needed to codify the url needed for Cedar get and put
- def codify_url(uri):
- return uri.replace('/', '%2F').replace(':', '%3A')
- # getting the Template Instance
- ## extract the template_instance id
- find_string = 'template-instances/'
- pos = template_instance_url.find(find_string) + len(find_string)
- template_instance = template_instance_url[pos:pos+36]
- ## reconstructing the URL needed for get
- base_url = 'https://resource.metadatacenter.org/template-instances/'
- url = codify_url('https://repo.metadatacenter.org/template-instances/')
- url = base_url + url + template_instance
- # print(url)
- headers = {'Content-Type': 'application/json', 'Authorization': CedarApiKey}
- ## geting the Cedar template
- req = requests.get(url, headers=headers) #, files=files)
- template = json.loads(req.content)
- for item in template.items:
- print(item)
- ### do your stuff with template ###
- # examine the above structure to assign new values
- # e.g. template['Date']['@value'] = date_string #'2021-05-27'
- ## writing to Cedar
- headers = {'Content-Type': 'application/json', 'Authorization': CedarApiKey, 'Accept': 'application/json'}
- response = requests.put(url, data=json.dumps(template), headers=headers)
- if str(response) == '<Response [200]>':
- print('Cedar is successfully updated')
- else:
- print('Houston we had a problem updating Cedar, errorcode: ' + str(response))