- Published on
A Small Dice API
- Authors
- Name
- lalugue
Dice for fun, laughs, and trying out API stuff!
Inspiration π‘
This idea was loosely inspired by Steve Brazier's PHP-powered DiceAPI project π²
I also took this as a chance to try and play around with fastapi ποΈ
Development ποΈ
Routes πΊοΈ
First, I added a main route which returns sample dice data:
/
The response also included an example on how to call different sets of dice, valid dice values, and a link to some documentation (more on that later below π):
{
"dice_set": [
{
"id": 0,
"dice": "d6",
"value": 3
}
],
"links": [
{
"name": "dice",
"href": "/dice",
"query_params": [
"dice"
],
"valid_values": [
"d4",
"d6",
"d8",
"d10",
"d12",
"d20"
],
"example": "/dice/?dice=d4,d6,d8",
"type": "GET"
},
{
"name": "documentation",
"href": "/docs",
"type": "GET"
}
]
}
Afterwards, I also added a dice
route with a dice
parameter, which accepts different valid dice as parameters, separated by commas:
/dice/?dice=d4,d6,d6
The application would return a dice_set
object, which would return the id
, dice
, and value
of the corresponding dice!
{
"dice_set": [
{
"id": 0,
"dice": "d4",
"value": 3
},
{
"id": 1,
"dice": "d6",
"value": 5
},
{
"id": 2,
"dice": "d6",
"value": 1
}
]
}
I was also wondering whether there was an automated way to show a list of available routes and try them out. In this case, I learned that it is possible and I specified a link to the auto-generated "docs":
/docs
Tests π
I also wanted to try out the testing functionality of fastapi
by writing a few tests.
Should I add more features, the tests would help me check whether things are still working smoothly later on (and squash possible bugs) π€
Documentation π
Using the docs/
route from earlier, the application has a means for showing the available routes and a mini playground to try some dice π§ͺ
Formatting β¨
Flake8 and Black keep the application's code a bit tidier. I also included a workflow to run flake8
, black
, and pytest
on the repository π§Ή
Some Fun π²
With a dice API ready, it was handy for our dice-rolling needs π Definitely more features could be considered and added. Though for now, I just want to roll some dice π²
Some to-dos include finding a simple way to deploy a "live" demo, a user interface for the application, and further play around with fastapi
. As for tackling them, stay tuned to my blog for new posts and updates π·