-
Notifications
You must be signed in to change notification settings - Fork 1
Joke API
melihozcan1 edited this page Jun 13, 2021
·
5 revisions
-
category: The category of the joke that should be returned by the API.
- chucknorris.io API: This API is used to get a random joke in a given category.
When given a category, my API returns a random joke that is related to that topic and of course related to Chuck Norris. I am thinking about integrating it to the given tags in a story in the future but for now all the API does is return a random joke in a given category with a creation date and a logo.
- Get a random Chuck Norris Joke.
Request:
GET
http://localhost:8000/api/joke/str:category
Parameters:
{
category = The category of the joke
}
Response:
{
{
"categories": ["string"],
"created_at": "date",
"icon_url": "string",
"id": "string",
"updated_at": "date",
"url": "string",
"value": "string"
}
}
GET /api/joke/animal
{
"categories": [
"animal"
],
"created_at": "2020-01-05 13:42:19.104863",
"icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id": "o-vfxwx6rgecuo_f5cecpq",
"updated_at": "2020-01-05 13:42:19.104863",
"url": "https://api.chucknorris.io/jokes/o-vfxwx6rgecuo_f5cecpq",
"value": "They say curiosity killed the cat. This is false. Chuck Norris killed the cat. Every single one of them."
}
GET /api/joke/<invalid category>
non-existent
There is no need for an API key in order to use this API.
More information about the status codes can be found in: https://httpstatuses.com/
Name | Meaning |
---|---|
HTTP_200_OK | Successful Request |
HTTP_400_BAD_REQUEST | Connection failed. |
HTTP_404_NOT_FOUND | Given category doesn't exist |
- Models Used in API
This API doesn't use any models.
- Views
import requests
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from rest_framework import status
from rest_framework.response import Response
import json
import environ
from rest_framework.decorators import api_view
# @csrf_exempt
@api_view(['GET'])
def joke(request,category):
"""
Takes a category name as an argument and makes a call to the Chuck Norris Jokes API and gets
a random joke related to that category. This joke is then returned as a JsonResponse which is
used to display it in the frontend.
"""
resp=requests.get("https://api.chucknorris.io/jokes/random?category=%s" % (category))
if resp.status_code == 200:
resp = resp.json()
joke = resp["value"]
return JsonResponse({
'joke':joke
})
else:
return JsonResponse({
'status' : 'non-existent'
},status = 404)
- Tests
from django.http import response
from django.test import TestCase, Client
class JokesTestCase(TestCase):
def test_get_jokes_for_category(self):
c = Client()
response = c.get('/api/joke/ani')
self.assertEqual(response.status_code, 404)
response2 = c.get('/api/joke/animal')
self.assertEqual(response2.status_code,200)
🏠 Home
👪 Team Members
- Milestone Reports
- Project Plans
- Project Selection
- Requirements
- Questions to Customer
- Scenarios and Mockups
- Design
- Meeting 1 (26.03.2021)
- Meeting 2 (01.04.2021)
- Meeting 3 (04.04.2021)
- Meeting 4 (07.04.2021)
- Customer Meeting 1 (14.04.2021)
- Meeting 5 (15.04.2021)
- Meeting 6 (17.04.2021)
- Meeting 7 (21.04.2021)
- Meeting 8 (25.04.2021)
- Meeting 9 (28.04.2021)
- Meeting 10 (03.05.2021)
- Meeting 11 (06.05.2021)
- Meeting 12 (07.05.2021)
- Meeting 13 (22.05.2021)
- Meeting 14 (26.05.2021)
- Meeting 15 (01.06.2021)
- Meeting 16 (04.06.2021)
- Meeting 17 (07.06.2021)
- Meeting 18 (08.06.2021)
- Meeting 19 (09.06.2021)
- Meeting 20 (12.06.2021)
- Lab 1 (12.10.2021)
- Meeting 1 (15.10.2021)
- Lab 2 (19.10.2021)
- Meeting 2 (25.10.2021)
- Lab 3 (26.10.2021)
- Meeting 3 (31.10.2021)
- Lab 4 (02.11.2021)
- Meeting 4 (08.11.2021)
- Lab 5 (09.11.2021)
- Meeting 5 (15.11.2021)
- Meeting 6 (18.11.2021)
- Lab 6 (23.11.2021)
- Lab 7 (30.11.2021)
- Lab 8 (21.12.2021)
- Lab 9 (28.12.2021)