|
| 1 | +""" |
| 2 | +Create a Postman folder for all Canvas API calls for quick tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import requests |
| 7 | +import re |
| 8 | + |
| 9 | +base_url = 'https://canvas.instructure.com/' |
| 10 | +postman_domain = '{{domain}}' |
| 11 | + |
| 12 | +print('Getting base doc') |
| 13 | +top_level_docs = requests.get('{}/doc/api/api-docs.json'.format(base_url)) |
| 14 | + |
| 15 | +# get the API portion of the JSON object returned from Canvas |
| 16 | +docs = top_level_docs.json()['apis'] |
| 17 | + |
| 18 | +# create a framework JSON object to collect the different APIs |
| 19 | +postman_collection = { |
| 20 | + 'info': { |
| 21 | + 'name': 'Canvas API', |
| 22 | + 'schema': 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json' |
| 23 | + }, |
| 24 | + 'item': [], |
| 25 | + 'auth': { |
| 26 | + 'type': 'bearer', |
| 27 | + 'bearer': [ |
| 28 | + { |
| 29 | + 'key': 'token', |
| 30 | + 'value': '{{user_token}}', |
| 31 | + 'type': 'string' |
| 32 | + } |
| 33 | + ] |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +# loop through each of the APIs |
| 38 | +# see here for the full list: https://canvas.instructure.com/doc/api/api-docs.json |
| 39 | +for doc in docs: |
| 40 | + folder = { |
| 41 | + 'name': doc['description'], |
| 42 | + 'item': [] |
| 43 | + } |
| 44 | + |
| 45 | + # get the JSON object for this API endpoint |
| 46 | + print('Getting {} doc'.format(folder['name'])) |
| 47 | + raw_doc_request = requests.get('{}/doc/api{}'.format(base_url,doc['path'])) |
| 48 | + doc_detail = raw_doc_request.json() |
| 49 | + |
| 50 | + # loop though the different calls for this API |
| 51 | + for item in doc_detail['apis']: |
| 52 | + # construct the path (including path variables) |
| 53 | + path_array = [ |
| 54 | + 'api' |
| 55 | + ] |
| 56 | + path_array_items = [] |
| 57 | + path_elements = re.findall('(\/[^/]*)', item['path']) |
| 58 | + for el in path_elements: |
| 59 | + el = el[1:] |
| 60 | + # path variables are translated here from {course_id} to :course_id |
| 61 | + if '{' in el: |
| 62 | + path_array_items = re.sub('{',':',re.sub('}','',el)) |
| 63 | + path_array.append(path_array_items) |
| 64 | + else: |
| 65 | + path_array.append(el) |
| 66 | + revised_path = '/'.join(path_array) |
| 67 | + |
| 68 | + # form parameters appear in the 'body' section in Postman |
| 69 | + form_parameters = [] |
| 70 | + # variables appear as path variables in Postman in the Params section |
| 71 | + variable_array = [] |
| 72 | + if 'parameters' in list(item['operations'][0]): |
| 73 | + for param in item['operations'][0]['parameters']: |
| 74 | + if param['paramType'] == 'form': |
| 75 | + param_type = 'text' |
| 76 | + if param['type'] == 'file': |
| 77 | + param_type = 'file' |
| 78 | + |
| 79 | + temp_param = { |
| 80 | + 'key': param['name'], |
| 81 | + 'value': '', |
| 82 | + 'type': param_type, |
| 83 | + 'description': param['description'] |
| 84 | + } |
| 85 | + form_parameters.append(temp_param) |
| 86 | + elif param['paramType'] == 'path': |
| 87 | + temp_var = { |
| 88 | + 'key': param['name'], |
| 89 | + 'description': param['description'] |
| 90 | + } |
| 91 | + variable_array.append(temp_var) |
| 92 | + |
| 93 | + # put it all together |
| 94 | + api_call = { |
| 95 | + 'name': item['operations'][0]['summary'], |
| 96 | + 'request': { |
| 97 | + 'method': item['operations'][0]['method'], |
| 98 | + 'header': [], |
| 99 | + 'url': { |
| 100 | + 'raw': 'https://{}/api/{}'.format(postman_domain, revised_path), |
| 101 | + 'protocol': 'https', |
| 102 | + 'host': [ postman_domain ], |
| 103 | + 'path': path_array, |
| 104 | + 'variable': variable_array |
| 105 | + }, |
| 106 | + 'description': item['description'] |
| 107 | + }, |
| 108 | + 'response': [] |
| 109 | + } |
| 110 | + |
| 111 | + if len(form_parameters) > 0: |
| 112 | + api_call['request']['body'] = { |
| 113 | + 'mode': 'formdata', |
| 114 | + 'formdata': form_parameters |
| 115 | + } |
| 116 | + folder['item'].append(api_call) |
| 117 | + postman_collection['item'].append(folder) |
| 118 | + |
| 119 | +# write the collection to a file |
| 120 | +with open('output.json', 'w') as f: |
| 121 | + json.dump(postman_collection, f, indent=3, separators=(',', ': ')) |
0 commit comments