PHPackages                             raffie-rest/adapter - 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. raffie-rest/adapter

ActiveLibrary[API Development](/categories/api)

raffie-rest/adapter
===================

Laravel 5 Remote REST Adapter

5.1.x-dev(9y ago)101.1k3[1 issues](https://github.com/raffie-rest/adapter/issues)MITPHPPHP &gt;=5.5.0

Since Jul 26Pushed 9y ago2 watchersCompare

[ Source](https://github.com/raffie-rest/adapter)[ Packagist](https://packagist.org/packages/raffie-rest/adapter)[ Docs](https://github.com/raffie-rest)[ RSS](/packages/raffie-rest-adapter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Laravel 5 Remote REST adapter
=============================

[](#laravel-5-remote-rest-adapter)

Table of contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Future development](#future)
- [Getting started](#getting-started)
- [Supported methods](#supported-methods)
- [Examples](#examples)
    - [From a Laravel 5 ShouldBeQueued, statically](#static)
    - [From a controller or command, with delegate handles](#delegate)
- [Implementing your own Remote REST end-points](#implement)

 Introduction
-----------------------------------------------------

[](#-introduction)

I built this thing on top of guzzle mainly to facilitate remote REST calls from a Laravel 5 application in a more, "abstracted", manner. In L4 and prior, there were all sorts of neat packages and abstractions that seemed to do this well enough, although the L5 transition has rendered many of them incompatible with the new framework.

Although I will never be able to repay the tremendous debt I owe to the creators of the Laravel Framework, I thought the least I could do was share this.

Bear in mind that it is still being developed / bug tested. Any help / feedback is most welcome:

-
- [Issues](https://github.com/raffie-rest/adapter/issues)

---

 Future development
-----------------------------------------------------

[](#-future-development)

### Incorporate Google oAuth2 Service Account auth

[](#incorporate-google-oauth2-service-account-auth)

I already got this set up in another custom Remote REST implementation using this [Custom JWT Utility](https://github.com/raffie-rest/jwt), I wanna incorporate it as a Guzzle authentication method in this project also.

### Unit testing

[](#unit-testing)

It's not there yet, as it heavily depends on the API employed.

### Develop a base model / trait for mapping unto `Eloquent\Model` events

[](#develop-a-base-model--trait-for-mapping-unto-eloquentmodel-events)

Not sure whether it is necessary

---

 Getting started
-----------------------------------------------------------

[](#-getting-started)

Add the package to your repo:

```
"raffie-rest/adapter": "dev-master"

```

Register the service provider - `config/app.php`:

```
'Raffie\REST\Adapter\AdapterServiceProvider'

```

Publish config file:

```
php artisan vendor:publish

```

Amend it - `config/rest_resources.php`:

```
	'pushover_v1'	=> [
	'data_type' => 'json',
    'defaults'  => [
		'base_url'    	=> 'https://api.pushover.net/1',
		'defaults'	  	=> [
			'query'	 	=> [
				'token' => '',		// Input your created app token here
				'user'  => ''		// User / delivery group token
			]
		]
    ]

```

As you can see, as far as the defaults are concerned, it's all Guzzle.

- [Read up on Guzzle](http://guzzle.readthedocs.org/en/latest/)
- [Guzzle API Docs](http://api.guzzlephp.org/)

That's it, you're all set! No wait...

---

 Supported methods
---------------------------------------------------------------

[](#-supported-methods)

Things like:

```
Foo::get()
Foo::get(1)
Foo::get(1, 'addresses')
Foo::get(1, 'addresses', 2)
Foo::post([])
Foo::post(1, 'addresses' , [])
Foo::put(1, [])
Foo::put(1, 'addresses', 2, [])
Foo::delete(1)
Foo::delete(1, 'addresses', 2)

```

Notice the resemblance to Laravel 5 Resourceful controller URI format?

The same principal applies to non-statics also.

Supported HTTP request types:

- GET
- POST
- PUT
- DELETE
- HEAD
- OPTIONS

---

 Examples
---------------------------------------------

[](#-examples)

###  From a Laravel 5 ShouldBeQueued, statically

[](#-from-a-laravel-5-shouldbequeued-statically)

`App\Commands\SendPushOver.php` :

```
use Raffie\REST\Adapter\Adapters\PushOver\v1\Message;

class SendPushover extends Command implements SelfHandling, ShouldBeQueued
{
	use InteractsWithQueue, SerializesModels;

	protected $message = [
	    'title'     => 'Something went wrong',
	    'message'   => 'Comrade Leader, something went wrong',
	    'url'       => 'http://foo.com',
	    'url_title' => 'Foo',
	    'priority'  => 0,
	    'sound'     => 'bugle'
    ];

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct(array $data = [])
	{
		$this->message = $data;
		//
	}

	/**
	 * Execute the command.
	 *
	 * @return void
	 */
	public function handle()
	{
		//
		$message = Message::send($this->message);

		return $message;
	}

```

`Foo.php`:

```
Queue::bulk([new SendPushover($message)]);

```

---

###  From a controller / command, with delegate handles

[](#-from-a-controller--command-with-delegate-handles)

Implement the DelegateInterface and go from there:

`PushOverSend.php`:

```
use Raffie\REST\Adapter\Adapters\PushOver\v1\Message,
	Raffie\REST\Adapter\Interfaces\DelegateInterface;

class PushOverSend extends Command implements DelegateInterface {

	protected $name = 'pushover:send';

	protected $description = 'Sends a test push';

	protected $message = [
		'title'     => 'Something went wrong',
		'message'   => 'Comrade Leader, something went wrong',
		'url'       => 'http://foo.com',
		'url_title' => 'Foo',
		'priority'  => 0,
		'sound'     => 'bugle'
	];

	public function fire()
	{
		$message = new Message($this);
		return $message->post($this->message);
	}

    /**
     * Request Succeeds
     *
     * @param  mixed $data
     * @return mixed
     */
	public function requestSucceeds($data)
	{
		$this->info($data);
	}

    /**
     * Request Fails
     *
     * @param  Illuminate\Support\MessageBag  $errors
     * @return mixed
     */
	public function requestFails(MessageBag $errors)
	{
		foreach($errors->all() as $error)
		{
			$this->error($error);
		}
	}
}

```

---

 Implementing your own Remote REST end-points
----------------------------------------------------------------------------------

[](#-implementing-your-own-remote-rest-end-points)

Easy peasy lemon squeezy, with everything being inherited from its abstract parent:

`Postcode.php`:

```
