Skip to content

Instantly share code, notes, and snippets.

@hskrasek
Created December 10, 2014 20:31
Show Gist options
  • Save hskrasek/7469f2c5d0650fc58bd1 to your computer and use it in GitHub Desktop.
Save hskrasek/7469f2c5d0650fc58bd1 to your computer and use it in GitHub Desktop.
IODocsConverter
import json
from pprint import pprint
from os.path import expanduser
import os
home = expanduser("~")
baseDir = home + '/Documents/Projects/iodocs/public/data'
for subdir, dirs, files in os.walk(baseDir):
for file in files:
# if file == 'apiconfig.json' or file.startswith('.'):
if file == 'perkprizemob.json' or file == 'perkpopquiz.json':
print('Updating: ' + os.path.join(subdir, file))
apiDoc = open(os.path.join(subdir, file), 'r')
data = json.load(apiDoc)
data = data['endpoints']
newResource = dict()
newResource['resources'] = dict()
for resource in data:
resourceName = resource['name']
newResource['resources'][resourceName] = dict()
newResource['resources'][resourceName]['methods'] = dict()
for method in resource['methods']:
methodName = method['MethodName']
methodName = methodName.split()
methodName = "_".join(methodName)
newResource['resources'][resourceName]['methods'][methodName] = dict()
newResource['resources'][resourceName]['methods'][methodName]['name'] = method['MethodName']
newResource['resources'][resourceName]['methods'][methodName]['httpMethod'] = method['HTTPMethod']
newResource['resources'][resourceName]['methods'][methodName]['path'] = method['URI']
newResource['resources'][resourceName]['methods'][methodName]['parameters'] = dict()
if 'parameters' not in method:
continue
for param in method['parameters']:
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']] = dict()
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['title'] = param['Name']
if param['Required'] == 'Y':
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['required'] = True
else:
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['required'] = False
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['default'] = param['Default']
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['type'] = param['Type']
newResource['resources'][resourceName]['methods'][methodName]['parameters'][param['Name']]['description'] = param['Description']
with open(os.path.join(subdir, file), 'w') as f:
json.dump(newResource, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment