Skip to main content

API Documentation Automation

# fetch needed packages and modules
# we'll use this to call the API using HTTPimport urllib.request
# we'll use this to convert JSON data into YAMLimport yaml
# we'll use this to parse string data into JSONimport simplejson
# we'll use this to make file backupsfrom shutil import copyfile
# we'll use regex to look for the YAML we want to replaceimport re
# set the API path variable for the documentation endpoint heredocUrl = 'http://api.test:5656/api/v1/docs'
# using the url library, grab the JSON docs data and save it into an objectdocObj = urllib.request.urlopen( docUrl, data = None )
# save that data into a buffer by reading (with the read function) from the memory address saved at docObjdocDat = docObj.read()
# Convert byte data into string data to allow parsing using the decode() functionstringDat = docDat.decode("utf-8")
# parse JSON from a string using the simplejson library's .loads() methodjsonDat = simplejson.loads(stringDat)
# parse YAML data from the JSON data we just created using the PyYAML library's .dump(method)yamlDat= yaml.dump(jsonDat)
# make a copy of index.md in case something goes wrong, name it "docUpdate.bak". This implementation overwrites the backup every time the script is run. The underscore should help hide the file from Statamic.copyfile('index.md', '_docUpdate.bak')
# open index.md and read it into a buffer using r+, so it can be updated laterindexFile = open('index.md', 'r+')indexRead = indexFile.read()
# parse index.md, looking for the '#parseMe' and '#parseStop' flags# within the parse flags, write the new API data into the index.md content#   This regex:#       opens the group: "("#       matches "#parseMe"#       but uses a positive lookbehind "?<=" to match only the string AFTER "#parseMe"#       uses '(?s)'to enable the wildcard '(.*)' to match every character on every line#       matches "#parseStop" and the new line character sequence "\n"#       but uses a positive lookahead "?=" to match only the string BEFORE "#parseStop"#       closes the group ")"indexWrite = re.sub('(?<=#parseMe\n)(?s)(.*)(?=#parseStop)', yamlDat, indexRead)
# adjust stream position back to the beginning of the fileindexFile.seek(0)
# clear the file in preparation for writing the new dataindexFile.truncate(0)
# write the newly modified data into the fileindexFile.write(indexWrite)
# close the fileindexFile.close()
# indicate completionprint('docUpdate completed')