Swapi Integration for Laravel

dev-main 2023-04-03 17:13 UTC

This package is auto-updated.

Last update: 2024-06-03 19:41:08 UTC


README

68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f362f36632f537461725f576172735f4c6f676f2e7376672f3132303070782d537461725f576172735f4c6f676f2e7376672e706e67

Table of Contents

  1. About Swapi
  2. Technologies
  3. Installing Swapi package
  4. Usage
  5. Testing
  6. License

About Swapi

"Swapi package" is a simple wrapper package that provide the list of people and the information related to a planet accessible using the following APIs:

  • GET /api/people (Provide a paginated list of people, filterable and orderable)
  • GET /api/people/{peopleId} (Provide selected people data including planet details)

Technologies

Laravel 8.*

Dependencies

guzzlehttp/guzzle
phpunit/phpunit
inertiajs/inertia-laravel

Install and configure Swapi Package

To install Swapi package run this command:

composer require christiancocco/swapi --with-all-dependencies

Now you can configure testing environment file to be able to run test script.

  1. Create testing DB
  2. Create .env.testing file in your root application folder and change DB connection parameter
  3. Add following to phpunit.xml file:
<?xml version="1.0" encoding="UTF-8"?>

    <!-- ... -->

    <testsuites>

        <!-- ... -->

        <testsuite name="SwapiUnit">
            <directory suffix="Test.php">./vendor/christiancocco/swapi/tests/Unit</directory>
        </testsuite>
        <testsuite name="SwapiTest">
            <directory suffix="Test.php">./vendor/christiancocco/swapi/tests/Feature</directory>
        </testsuite>
    </testsuites>

Usage

After installation you have to run the following command:

php artisan swapi:install

This command initialize package (copy configuration file) and run migration for planet and people table.

php artisan swapi:init

This command initialize data retrieved data from swapi.dev.

(Full documentation: https://swapi.dev/documentation)

NB. To initialize testing database run this command:

php artisan migrate --env=testing

If you want to filtering, ordering and paging /api/people result you can use this querystring parameter:

  1. query: string to search in all people fields
  2. itemperpage: item per page (default value = 10)
  3. page: page number
  4. sort: sorting field
  5. sortVer: sorting versus (ASC: ascending - default value; DESC: descending)

Example: /api/people?query=fair&sort=name&sortVer=DESC&itemperpage=2&page=2

Testing

To launch the Unit Test run this command:

php artisan test --filter=Swapi --stop-on-failure

To test api run

php artisan serve

and visit

http://localhost:8000/swapi-test

License

The Swapi Laravel package is open-sourced software licensed under the MIT license.