PHP code example of albawebstudio / petfinder-api

1. Go to this page and download the library: Download albawebstudio/petfinder-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

albawebstudio / petfinder-api example snippets


albawebstudio\PetfinderApi\ServiceProvider::class,



namespace App\Http\Controllers;



use albawebstudio\PetfinderApi\PetfinderConnector;

class PetfinderApiController extends Controller
{
    public function __construct()
    {
        PetfinderConnector::init(
            config('petfinder.key'),
            config('petfinder.secret'),
            config('petfinder.organization'),
        );
    }
}




namespace App\Http\Controllers;


use albawebstudio\PetfinderApi\Animal;
use albawebstudio\PetfinderApi\exceptions\InvalidAuthorizationException;
use albawebstudio\PetfinderApi\exceptions\InvalidRequestException;
use albawebstudio\PetfinderApi\exceptions\PetfinderConnectorException;

class AnimalController extends PetfinderApiController
{
    /**
     * @var Animal
     */
    protected Animal $api;

    public function __construct()
    {
        parent::__construct();
        $this->api = new Animal();
    }

    /**
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function index()
    {
        return $this->api->animals([]);
    }

    /**
     * @param int $animalId
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function show(int $animalId): array
    {
        return $this->api->animal($animalId);
    }

    /**
     * Return all animal types from Petfinder
     *
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function types(): array
    {
        return $this->api->types();
    }

    /**
     * Return specific animal type from Petfinder
     *
     * @param string $type
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function type(string $type): array
    {
        return $this->api->type($type);
    }

    /**
     * Return all breeds for specific animal type from Petfinder
     * @param string $type
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function breeds(string $type): array
    {
        return $this->api->breeds($type);
    }
}





namespace App\Http\Controllers;

use albawebstudio\PetfinderApi\exceptions\InvalidAuthorizationException;
use albawebstudio\PetfinderApi\exceptions\InvalidRequestException;
use albawebstudio\PetfinderApi\exceptions\PetfinderConnectorException;
use albawebstudio\PetfinderApi\Organization;

class OrganizationController extends PetfinderApiController
{
    /**
     * @var Organization
     */
    protected Organization $api;
    public function __construct()
    {
        parent::__construct();
        $this->api = new Organization();
    }

    /**
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function index(): array
    {
        return $this->api->organizations([]);
    }

    /**
     * @param string $organizationId
     * @return array
     * @throws InvalidAuthorizationException
     * @throws InvalidRequestException
     * @throws PetfinderConnectorException
     */
    public function show(string $organizationId): array
    {
        return $this->api->organization($organizationId);
    }
}




Route::prefix('animals')->group(function() {
    Route::get('/', 'App\Http\Controllers\AnimalController@index')->name('animals');
    Route::get('/{id}', 'App\Http\Controllers\AnimalController@show')->name('animal');
});

Route::prefix('types')->group(function () {
    Route::get('/', 'App\Http\Controllers\AnimalController@types')->name('types');
    Route::get('/{type}', 'App\Http\Controllers\AnimalController@type')->name('type');
    Route::get('/{type}/breeds', 'App\Http\Controllers\AnimalController@breeds')->name('breeds');
});

Route::prefix('organizations')->group(function() {
    Route::get('/', 'App\Http\Controllers\OrganizationController@index')->name('organizations');
    Route::get('/{id}', 'App\Http\Controllers\OrganizationController@show')->name('organization');
});

shell
php artisan vendor:publish --provider="albawebstudio\\PetfinderApi\\ServiceProvider"