PHPackages                             mpemburn/api-consumer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mpemburn/api-consumer

ActiveLibrary[API Development](/categories/api)

mpemburn/api-consumer
=====================

Configurable consumer for RESTful API's

v1.0(5y ago)020MITPHPPHP &gt;=7.4

Since Dec 6Pushed 5y ago1 watchersCompare

[ Source](https://github.com/mpemburn/api-consumer)[ Packagist](https://packagist.org/packages/mpemburn/api-consumer)[ RSS](/packages/mpemburn-api-consumer/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

API Consumer
------------

[](#api-consumer)

[![Latest Version](https://camo.githubusercontent.com/4a5d4c00178f5983c9c19383b339119f9a5ed9ab2f87d1e9467ae38cb03029f3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d70656d6275726e2f6170692d636f6e73756d65722e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/laravel-analytics/releases)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/d092b1e6f4fe8c9f51a8bd910b6b137f82fee489554efc2bc470fa92ec5568b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d70656d6275726e2f6170692d636f6e73756d65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-analytics)

### About

[](#about)

The `api-consumer` package allows you to create simple endpoint wrappers for RESTful API's.

### Installation

[](#installation)

You can install this package via composer:

`composer require mpemburn/api-consumer`

The package will automatically register the service provider `ApiConsumerProvider`.

Next, you will need to publish the package in order to copy `api-consumer.php` into your `config` directory:

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

```

### How to use

[](#how-to-use)

#### Configuration

[](#configuration)

The package will add the file `config/api-consumer.php` to your project. This returns an array containing the keys and variables used by the endpoint classes you'll create to consume one or more RESTful APIs. The format will look like this:

```
'shopify' => [
    'base_uri' => 'https://mystore.myshopify.com/admin/api/2020-10',
    'username' => '5ac3bd00f1ebc6a65caa4c0a6a3b1555',
    'password' => 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
],

```

To make this more secure, you should store the actual variables in your `.env` file:

```
SHOPIFY_USERNAME=5ac3bd00f1ebc6a65caa4c0a6a3b1555
SHOPIFY_PASSWORD=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

```

...and reference them like this:

```
'shopify' => [
    'base_uri' => 'https://mystore.myshopify.com/admin/api/2020-10',
    'username' => env('SHOPIFY_USERNAME'),
    'password' => env('SHOPIFY_PASSWORD')
],

```

You can add as many API's as you need to the config file, as long as each has the API name (i.e., 'shopify' in this instance) at top-level of the array.

**NOTE**: After making changes to a this config file, it's important to run:

```
php artisan config:cache

```

#### Class Structure

[](#class-structure)

While there's no absolute requirement to structure your class files this way, the suggested heirarchy is:

```
project
│
└───Api
│   │
│   └───Shopify
│       │   ShopifyEndpoint
│       │   CreateProduct
│       │   GetProducts
│       │   ...
│       Discourse
│       │   DiscourseEndpoint
│       │   CreateUser
│       │   GetUsers
│       │   ...

```

#### Parent Endpoint Classes

[](#parent-endpoint-classes)

Each parent endpoint class (e.g., `ShopifyEndpoint`, `DiscourseEndpoint` above) needs to extend this package's `AbstractEndpoint` class. Individual endpoints then extends its primary class. For example:

```
