Let’s clone — Github Octocat API Link to heading

Octocat API Image

Github offers a very simple API called octocat, which displays the iconic Github’s octocat mascot with a chat bubble for the given string. See here for full documentation. We will implement this API on our own.

Let’s first see what this API does in action

$ curl -XGET https://api.github.com/octocat

               MMM.           .MMM
               MMMMMMMMMMMMMMMMMMM
               MMMMMMMMMMMMMMMMMMM      ____________________________
              MMMMMMMMMMMMMMMMMMMMM    |
             MMMMMMMMMMMMMMMMMMMMMMM   | Practicality beats purity. |
            MMMMMMMMMMMMMMMMMMMMMMMM   |_   ________________________|
            MMMM::- -:::::::- -::MMMM    |/
             MM~:~ 00~:::::~ 00~:~MM
        .. MMMMM::.00:::+:::.00::MMMMM ..
              .MM::::: ._. :::::MM.
                 MMMM;:::::;MMMM
          -MM        MMMMMMM
          ^  M+     MMMMMMMMM
              MMMMMMM MM MM MM
                   MM MM MM MM
                   MM MM MM MM
                .~~MM~MM~MM~MM~~.
             ~~~~MM:~MM~~~MM~:MM~~~~
            ~~~~~~==~==~~~==~==~~~~~~
             ~~~~~~==~==~==~==~~~~~~
                 :~==~==~==~==~~

Here, we didn’t specify the parameter s, in which case Github outputs some message in random. We can specify the parameter with -G --data-urlencode option.

$ curl -XGET https://api.github.com/octocat -G --data-urlencode "s=I am octocat"

               MMM.           .MMM
               MMMMMMMMMMMMMMMMMMM
               MMMMMMMMMMMMMMMMMMM      ______________
              MMMMMMMMMMMMMMMMMMMMM    |
             MMMMMMMMMMMMMMMMMMMMMMM   | I am octocat |
            MMMMMMMMMMMMMMMMMMMMMMMM   |_   __________|
            MMMM::- -:::::::- -::MMMM    |/
             MM~:~ 00~:::::~ 00~:~MM
        .. MMMMM::.00:::+:::.00::MMMMM ..
              .MM::::: ._. :::::MM.
                 MMMM;:::::;MMMM
          -MM        MMMMMMM
          ^  M+     MMMMMMMMM
              MMMMMMM MM MM MM
                   MM MM MM MM
                   MM MM MM MM
                .~~MM~MM~MM~MM~~.
             ~~~~MM:~MM~~~MM~:MM~~~~
            ~~~~~~==~==~~~==~==~~~~~~
             ~~~~~~==~==~==~==~~~~~~
                 :~==~==~==~==~~

I’m going to use flask library for server-related implementations. Below is my implementation

from flask import Flask, request

app = Flask(__name__)

@app.route('/octocat')
def octocat():
    name = request.args.get('s')
    name = name or "hello world"
    blank = " " * len(name)
    top = "_" * (len(name) + 2)
    bottom = "_" * max(0, (len(name) - 2))
    s = """
               MMM.           .MMM
               MMMMMMMMMMMMMMMMMMM
               MMMMMMMMMMMMMMMMMMM      top
              MMMMMMMMMMMMMMMMMMMMM    | blank |
             MMMMMMMMMMMMMMMMMMMMMMM   | name |
            MMMMMMMMMMMMMMMMMMMMMMMM   |_   bottom|
            MMMM::- -:::::::- -::MMMM    |/
             MM~:~ 00~:::::~ 00~:~MM
        .. MMMMM::.00:::+:::.00::MMMMM ..
              .MM::::: ._. :::::MM.
                 MMMM;:::::;MMMM
          -MM        MMMMMMM
          ^  M+     MMMMMMMMM
              MMMMMMM MM MM MM
                   MM MM MM MM
                   MM MM MM MM
                .~~MM~MM~MM~MM~~.
             ~~~~MM:~MM~~~MM~:MM~~~~
            ~~~~~~==~==~~~==~==~~~~~~
             ~~~~~~==~==~==~==~~~~~~
                 :~==~==~==~==~~
    """.replace("name", name).replace("blank", blank).replace("top", top).replace("bottom", bottom)
    return s

if __name__ == '__main__':
    app.run(debug=True)

Here, we extract the parameter s using request.args and handle the chat bubble accordingly. To run the server, we can

$ python app.py
 * Serving Flask app 'app'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

and we can send request to our server

$ curl -XGET localhost:5000/octocat -G --data-urlencode "s=I am octocat"

               MMM.           .MMM
               MMMMMMMMMMMMMMMMMMM
               MMMMMMMMMMMMMMMMMMM      ______________
              MMMMMMMMMMMMMMMMMMMMM    |
             MMMMMMMMMMMMMMMMMMMMMMM   | I am octocat |
            MMMMMMMMMMMMMMMMMMMMMMMM   |_   __________|
            MMMM::- -:::::::- -::MMMM    |/
             MM~:~ 00~:::::~ 00~:~MM
        .. MMMMM::.00:::+:::.00::MMMMM ..
              .MM::::: ._. :::::MM.
                 MMMM;:::::;MMMM
          -MM        MMMMMMM
          ^  M+     MMMMMMMMM
              MMMMMMM MM MM MM
                   MM MM MM MM
                   MM MM MM MM
                .~~MM~MM~MM~MM~~.
             ~~~~MM:~MM~~~MM~:MM~~~~
            ~~~~~~==~==~~~==~==~~~~~~
             ~~~~~~==~==~==~==~~~~~~
                 :~==~==~==~==~~

Finally, we can test diff to see if my implementation produces the same result as Github’s

$ diff <(curl -XGET localhost:5000/octocat -G --data-urlencode "s=I am octocat" 2>/dev/null) <(curl -XGET https://api.github.com/octocat -G --data-urlencode "s=I am octocat" 2>/dev/null)

Hopefully, this simple example demystifies how a server responds to a client’s request.