Skip to content

Joke API

melihozcan1 edited this page Jun 13, 2021 · 5 revisions

1. Glossary

  • 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.

2. Description

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.

3. Functionality

  • 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"
}
}

4. Example Responses

  • 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

5. Authentication

There is no need for an API key in order to use this API.

6. Errors and Successes

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

7. Code Documenation

  • 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

💬 Communication

📈 Effort Tracking

🌐 Postory

🤸‍♀️ Practice App

📆 Project Plans & Milestone Reports

💻 Project Documentation

📝 Regular Meeting Reports

CMPE 352 Meetings

CMPE 451 Meetings

Android Meetings

Backend Meetings

Frontend Meetings

🔍 Research

📎 Templates

Clone this wiki locally