CEDAR

CEDAR

CEDAR

Home page CEDAR

https://more.metadatacenter.org/tools-training/orientation

CEDAR APIs

https://more.metadatacenter.org/tools-training/cedar-api

CEDAR API and Python

Prerequisites:

  1. Create file called: secret.py
    1. Add the following line of code: CedarApiKey = 'apiKey key'
      1. Where key is a long hexastring. Literally after '=' it should read: 'apiKey 3453a54f345c345345'.
      2. 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.
  1. Add External Access rule and turn the rule on
    1. Rule name
      IP Address
      Port
      Reason
      Cedar
      171.67.213.63
      443
      Accessing Cedar


Python code

See also attachment below
  1. import json
  2. import secret

  3. # Open the Cedar template in your browser, paste the url between the triple quotes in its completeness
  4. # This script will extract the template-instance id (URL is non working example)
  5. template_instance_url = '''
  6. 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
  7. '''

  8. # Getting the Cedar Secret key from secert.py
  9. CedarApiKey = secret.CedarApiKey

  10. # needed to codify the url needed for Cedar get and put
  11. def codify_url(uri):
  12.     return uri.replace('/', '%2F').replace(':', '%3A')


  13. # getting the Template Instance

  14. ## extract the template_instance id
  15. find_string = 'template-instances/'
  16. pos = template_instance_url.find(find_string) + len(find_string)
  17. template_instance = template_instance_url[pos:pos+36]

  18. ## reconstructing the URL needed for get
  19. base_url = 'https://resource.metadatacenter.org/template-instances/'
  20. url = codify_url('https://repo.metadatacenter.org/template-instances/')
  21. url = base_url + url + template_instance
  22. # print(url)

  23. headers = {'Content-Type': 'application/json', 'Authorization': CedarApiKey}

  24. ## geting the Cedar template
  25. req = requests.get(url, headers=headers) #, files=files)
  26. template = json.loads(req.content)
  27. for item in template.items:
  28.     print(item)

  29. ### do your stuff with template ###
  30. # examine the above structure to assign new values
  31. # e.g. template['Date']['@value'] = date_string #'2021-05-27'

  32. ## writing to Cedar
  33. headers = {'Content-Type': 'application/json', 'Authorization': CedarApiKey, 'Accept': 'application/json'}
  34. response = requests.put(url, data=json.dumps(template), headers=headers)

  35. if str(response) == '<Response [200]>':
  36.     print('Cedar is successfully updated')
  37. else:
  38.     print('Houston we had a problem updating Cedar, errorcode: ' + str(response))