PHP code example of mpemburn / api-consumer

1. Go to this page and download the library: Download mpemburn/api-consumer 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/ */

    

mpemburn / api-consumer example snippets




namespace App\Api\Shopify;

use Mpemburn\ApiConsumer\Endpoints\AbstractEndpoint;

class ShopifyEndpoint extends AbstractEndpoint
{
    public function __construct()
    {
        parent::__construct();

        $this->addHeader('Content-Type', 'application/json');
    }

    public function getApiName(): string
    {
        return 'shopify';
    }
}



namespace App\Api\Shopify;

class GetProducts extends ShopifyEndpoint
{
    public function getRequestType(): ?string
    {
        return 'GET';
    }

    public function getEndpoint(): ?string
    {
        return '/products.json';
    }

    public function getRequestName(): ?string
    {
        return 'Get Products';
    }
}

Route::get('get_products', function () {
    $requestManager = RequestManager::make();
    $products = new Products();
    if ($requestManager) {
        return $requestManager->send($products)
            ->getResponse();
    }
});




namespace App\Api\Shopify;

class CreateProduct extends ShopifyEndpoint
{
    public function getRequestType(): ?string
    {
        return 'POST';
    }

    public function getEndpoint(): ?string
    {
        return '/products.json';
    }

    public function getRequestName(): ?string
    {
        return 'Create Product';
    }

    public function create(array $product): void
    {
        $this->setParams($product);
    }
}


Route::post('create_product', function (Request $request) {
    $requestManager = RequestManager::make();
    $createProduct = new CreateProduct();
    $createProduct->create($request->toArray());

    if ($requestManager) {
        return $requestManager->send($createProduct)
            ->getResponse();
    }
});



namespace App\Api\Roster;

class UpdateUser extends RosterEndpoint
{
    public function getRequestType(): ?string
    {
        return 'PUT';
    }

    public function getEndpoint(): ?string
    {
        return $this->hydrateUrlParams('/users/update/{user_id}', $this->getUrlParams());
    }

    public function getRequestName(): ?string
    {
        return 'Update User';
    }

    public function update(int $userId, array $userData): void
    {
        $this->setParams($userData);
        $this->addUrlParam('user_id', $userId);
    }
}



namespace App\Api\Roster;

class GetAuthToken extends RosterEndpoint
{
    protected ?string $authTokenFetchKeyName = 'key';
    protected ?string $authTokenResponseName = 'auth_token';

    public function getRequestType(): ?string
    {
        return 'GET';
    }

    public function getEndpoint(): ?string
    {
        return '/get_auth';
    }

    public function getRequestName(): ?string
    {
        return 'Get Auth Token';
    }
}

...
class RosterEndpoint extends AbstractEndpoint
{
    protected ?string $authTokenEndpoint = GetAuthToken::class;
...

$requestManager = RequestManager::make();
$members = new GetMembers();

if ($requestManager) {
return $requestManager->preserveAuthToken()
    ->send($members)
    ->getResponse();
}

php artisan vendor:publish --provider="Mpemburn\ApiConsumer\ApiConsumerProvider"

php artisan config:cache