Skip to content

Instantly share code, notes, and snippets.

@twyle
Created May 13, 2022 08:37
Show Gist options
  • Save twyle/bec5223f6cae5e47da6d920899716a0e to your computer and use it in GitHub Desktop.
Save twyle/bec5223f6cae5e47da6d920899716a0e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""This module creates the routes for our API."""
import random
from flask import request
from API import app
@app.route('/api/v1', methods=['GET'])
@app.route('/', methods=['GET'])
def api_home() -> dict:
"""Handle get requests to /api/v1 route.
Returns
-------
dict:
A dictionary showing:
{
'data': 'Hello from shileds.io json endpoint'
}
"""
data = {
'data': 'Hello from shileds.io json endpoint'
}
return data, 200
@app.route('/api/v1/data', methods=['GET'])
def shields_io_data() -> dict:
"""Generate JSON data for the shields.io server.
Parameters
----------
username: str
The users name
Returns
-------
data: dict
This dictionary contains info used to generate the dynamic badge by shields.io
data = {
"schemaVersion": 1,
"label": "name",
"message": "username",
"color": "color-name",
"labelColor": "color-name",
"style": "style-name"
}
"""
if request.args.get('username'):
username = request.args.get('username')
colors = ['red', 'green', 'yellow', 'blue', 'orange', 'purple', 'grey']
styles = ['flat', 'plastic', 'flat-square', 'for-the-badge', 'social']
data = {
"schemaVersion": 1,
"label": "name",
"message": username,
"color": random.choice(colors),
"labelColor": random.choice(colors),
"style": random.choice(styles)
}
return data, 200
error = {
'error': 'You must include your name in the request.'
}
return error, 400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment