Grab your docs using the Github API

In attempt to minimize the number of places I have to update every time I make a change to some documentation, I’ve decided to pull markdown documentation straight from my git repo and render it on GAuthify. This was surprisingly easy and the final results will look like this:

From a markdown from Github, straight to HTML on GAuthify

Heres how to do this in python (very easy to port in any other language):

import requests
import base64
import json

def get_github_readme(owner, repo):
    response = requests.get(
        "https://api.github.com/repos/{}/{}/readme".format(owner, repo))
    content_b64 = response.json['content']
    content = base64.b64decode(content_b64)
    return content


def markdown_to_html(markdown):
    return requests.post("https://api.github.com/markdown",
                             data=json.dumps({'text': markdown})).text

And thats all there is to it. get_github_readme grabs the preferred readme from github (its as easy to do any other specific file), decodes the base64 response and returns the raw markdown. markdown_to_html uses Github’s API to return the HTML version of the markdown. I simply inject this into my django template and with no modifications I get results like this: https://www.gauthify.com/docs/libs/python/

Now, Github’s API allows for 60 unauthenticated requests per hour, to make sure we dont reach this and to improve site load times, I cache the docs in Redis for one hour using this function here:

def github_html_readme(owner, repo):
    cache_key = github_readme(owner, repo)
    content = redis_server.get(cache_key)
    if not content:
        content_md = get_github_readme(owner, repo)
        content = markdown_to_html(content_md)
        redis_server.set(cache_key, content)
        redis_server.expire(cache_key, 3600)
    return content

From here on out when I update the docs in the project directories and push them to github, the website will update on its own. Ill probably find more uses for this kind of thing in the future.

Also read...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>