Sign inSign up

stdevel/joke-api

By stdevel

•Updated over 6 years ago

Simple Python Flask application that manages (bad) jokes

Image
0

1.3K

stdevel/joke-api repository overview

⁠joke-api

Simple Python Flask application that manages jokes

⁠Overview

Jokes are ordered in categories and stored in a SQLite database. They can have an optional ranking (from 1 for bad to 10 for good).

⁠Requirements

For using this application, you will need:

  • Python 3
  • Flask
  • SQLite 3

⁠Docker container

There is a Dockerfile⁠ for running this application inside a container based on Alpine Linux⁠. To create the image, run the following command:

$ docker build -t joke_api .

This will take a couple of minutes, afterwards you should see the image:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
joke_api            latest              84572d3b8826        1 hour ago          59.8MB
...

To start a new container based on this image, execute:

$ docker run -d -p 5000:5000 joke_api
b4735f102afb6e288b0a35d17b0e63da4d6bd2652467709c5c28538088ae5d30

$ docker ps
CONTAINER ID        IMAGE                      COMMAND                   CREATED             STATUS                PORTS                                                                                                  NAMES
b4735f102afb        joke_api                   "/bin/sh -c \"/opt/jo…"   16 seconds ago      Up 14 seconds         0.0.0.0:6000->5000/tcp                                                                                 boring_chebyshev
...

It is also possible to use shipped docker-compose file⁠:

$ docker-compose create
$ docker-compose up
Starting joke_api ... done
Attaching to joke_api
...
joke_api    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

⁠API calls

There is also a pre-defined Postman collection⁠.

⁠Categories
CallMethodParametersDescription
/api/categoriesGET-returns all available categories
/api/categories/<id,name>GETcategory ID/namereturns a particular category
/api/categoriesPOST { "item": { "name": "<str>" } }creates a new category
/api/categories/<id>PUT,POSTcategory ID, { "item": {"id": <int>, "name": "<str>"} }updates an existing category
/api/categories/<id>DELETEcategory IDremoves a category
⁠Jokes
CallMethodParametersDescription
/api/jokes/<id>GETjoke IDretrieves a particular joke
/api/jokes/randomGET-retrieves a random joke
/api/jokes/random/<id,name>GETcategory ID/nameretrieves a random joke from a particular category
/api/jokes/random/<id,name>/<rank>GETcategory ID/nameretrieves a random joke from a particular category with a specific minimum ranking
/api/jokesPOST{ "item": { "category_id": <int>, "text": "<str>", "rank": <int> } }creates a new joke within a category
/api/jokes/<id>PUT/POSTjoke ID, { "item": {"newid": <int>, "category_id": <int>, "text": "<str>", "rank": <int>} }updates an existing joke
/api/jokes/<id>DELETEjoke IDremoves a joke

⁠Database layout

⁠categories
Field nameField typeDescription
category_idINTEGER PRIMARY KEY AUTOINCREMENTcategory ID
category_nameTEXT NOT NULLcategory name
⁠jokes
Field nameField typeDescription
joke_idINTEGER PRIMARY KEY AUTOINCREMENTjoke ID
category_idINTEGER NOT NULL (FOREIGN KEY)category ID
joke_textTEXT NOT NULLjoke
joke_rankINTEGER NULLjoke ranking (1 - 10)

Tag summary

Content type

Image

Digest

Size

22.7 MB

Last updated

over 6 years ago

docker pull stdevel/joke-api