/HGBackChallenge

The Cat API HostGator Code Challenge

Primary LanguagePHP

HostGator CodeChallenge

The Challenge

Create a PHP Web API that consults the Cat API: https://docs.thecatapi.com
The API must allow the search for Cat Breeds by name. The API should cache the results from the Cat API into a local MySQL database. If the cat breed is not found in the local MySQL database, it will query the cat API. The only endpoint that must be implemented is the GET to /breeds

The Solution

The challenge was solved using Laravel Framework for the base architecture. The only endpoints the API implements are GET to /breeds and GET to /breeds/{id}

Both endpoints share the same controller (ContentController), wich has two methods for handling the requests: getContentByName and getContentById

The WebApiController was created in order to implement a custom trait: the WebApiResponser. The WebApiResponser trait contains all the methods used on the ContentController (wich extends from the WebApiController) in order to keep the code clean and modularized.

When the api recives a request will search the data into a MySQL local database, if the data is not found, then it will query The Cat API, store the data on the database for 30 days and finally will be retrieved to the client. Requests to The Cat API are made through a Guzzle HTTP Client.

In order to increase the security of the api a middleware was implemented in both endpoints to limit the number of requests to 15 requests per minute per IP. This will help blocking malicious bots and it can mitigate DOS attacks.

Almost every possible exception is being handled by the App\Exceptions\Handler.php. The other few possible errors are being handled from both ContentController and WebApiResponser. Errors will always return in JSON format.

Search by Name Flow Diagram


Search by ID Flow Diagram


Documentation

Search by Breed Name

  • Breeds search by name can be requested via GET http://localhost:XX/breeds
  • The only query parameters accepted are name and per_page
  • The name parameter is required
  • The per_page parameter is optional. It's value must be between 2 and 50, and it's defualt value is 5

Example:
http://localhost:XX/breeds?name=fo (Search all breed names containing "fo")
http://localhost:XX/breeds?name=fo&per_page=20 (Search all breed names containing "fo", showing 20 breeds per page)

Search by Breed ID

Example:
http://localhost:XX/breeds/beng (Search for the Bengal cats)

Test Online

You can try the api using the following link:
https://api.gatortest.tk