A laravel wrapper for the Kayne REST API
I will presume you have a development environment already set up for PHP/Laravel.
- Run the following command through your terminal to clone the repository:
git clone https://github.com/JamieDW/kanye-rest
thencd kayne-rest
- You will then want to install the composer packages:
composer install
- Depending on your development environment, now is the time to serve the application locally.
- Run
cp .env.example .env or copy .env.example .env
- Generate your application key with
php artisan key:generate
- We are now ready to test the Laravel application!
I have used PHPUnit as my test framework as that already comes with a fresh Laravel installation; however, the tests will also work with the Pest testing framework if you prefer that.
- To run the tests run the following in your terminal from the root folder of the application:
php artisan test
- The tests will automatically create an authentication token for any authenticated API routes.
- If you would like to interact with the API yourself, you will need a REST client like Postman or Insomnia. The following API routes are available:
{localhost}/api/quotes
- retrieves cached quotes.{localhost}/api/quotes/new
- retrieves new quotes.{localhost}/api/quotes/purge
- purges the cached quotes
- However, all these routes are authenticated, so you will first need to generate an authentication token using the following artisan command.
php artisan app:create-token
- I have used bearer authentication method for this task. Simply copy the token and add it the request header of your REST client; it should look something like this.
Authorization: Bearer {token}
. Please note that the token expires after 10 minutes. - You are now set up to use the above API endpoints!
Just some notes, improvements, and additions I would make if time was not an issue.
- Because this project implements the 'Laravel Manager Design' pattern and only one quote driver was requested, I thought it would be useful to provide a second quote driver to show the power of this pattern using the following API: https://api.quotable.io/random
- You can easily switch between the two by changing the following config
config/quote.php
default
value.
- You can easily switch between the two by changing the following config
- For simplicity, I have chosen to cache the generated authentication tokens. This works nicely for this demonstration. However, I would lean towards storing tokens within a database normally. Note that the tokens expire after 10 minutes.
- Quotes from the following API endpoint,
{localhost}/api/quotes
are cached. The first call to the default quote driver will fetch the unique quotes. Subsequent calls will return the cached quotes instead.- You can test this out by quickly hitting the endpoint over and over again; you will notice the response time will be very quick!
- A new middleware was created called 'app/Http/Middleware/BearerTokenAuthentication.php' to handle authentication.
Finally, some improvements or additions I would make.
- Displaying the quotes wasn't mentioned. But we could have created an endpoint within the web route to display the quotes fetched from the API.
- We could include the options to retrieve a specific amount of quotes instead of 5, the ability to select the quote driver 'kayne-rest' or 'quotable' and a button to refresh the quotes.
- User accounts: because authentication was mentioned, we could have leveraged Laravel's inbuilt authentication, which provides us with a 'User' model and means of logging in and registering. This could have then been extended to provide access tokens to these users.
- Storing quotes. Rather than fetching the quotes from the quote provider, we could instead store these quotes within a 'quotes' table and fetch them within a background process. This removes the call to the external quote provider, resulting in quicker response times.