Skip to content

Instantly share code, notes, and snippets.

@kpion
Last active March 6, 2018 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpion/2c04cf4e50577ed3af1e9940771837ff to your computer and use it in GitHub Desktop.
Save kpion/2c04cf4e50577ed3af1e9940771837ff to your computer and use it in GitHub Desktop.
<?php
/*
Question about architecture.
This is supposed to be a simple Wordpress rest api client.
The example of one of the endpoints (users) is here: https://developer.wordpress.org/rest-api/reference/users/#example-request
to list users: GET http://demo.wp-api.org/wp-json/wp/v2/users
to create a user: POST http://demo.wp-api.org/wp-json/wp/v2/users with some required arguments
The code below might seem to be a bit long, but there are really only 3 small parts of it:
The example.php shows how to use the stuff
The Api class which basically creates and stores the endpoint objects (they care about specific endpoints, like 'users','posts')
The Endpoint class itself.
This concept comes from the fact that there are some common things shared among the Endpoints,
like the base_uri or authentification. I wanted to simply inject this information as GuzzleClient into endpoints.
Maybe in the future the endpoints will be created lazily on demand, that is not part of my question.
Also maybe in the future there really will be a UserEndpoint or UserRepository or something, which will extend the Endpoint.
So, here we go, starting with a usage example:
*/
/////////////////////////////////////////////////////////////////////////////
//example.php
//The 'main' object, with some config. It will also instantiate GuzzleClient itself and endpoints (like 'users')
$wpApi = new Api([
// Base URI is used with relative requests
'base_uri' => 'http://demo.wp-api.org/wp-json/wp/v2/',
'timeout' => 5.0,
'auth' => ['admin', 'admin_pass']
]);
//endpoint usage example:
$wpApi->users()->create([
'username' => 'createdViaApi',
'email' => 'createdViaApi@example.com',
'password' => 'createdViaApi',
])
/////////////////////////////////////////////////////////////////////////////
//Api.php
/*
\GuzzleHttp\Client is just a http request class, we can do 'POST', 'GET' etc.
*/
class Api extends \GuzzleHttp\Client
{
//this will be our Endpoint objects
protected $users = null;
protected $posts = null;
//... and a few more
/*
* @param array $config - config we'll pass to guzzle client
*/
public function __construct($config = [])
{
parent::__construct($config);
//maybe later this will become a factory and will be created on demand only. Or maybe not.
$this->users = new Endpoint($this, 'users');
$this->posts = new Endpoint($this, 'posts');
}
//access to endpoint objects
public function users()
{
return $this->users;
}
public function posts()
{
return $this->posts;
}
}
/////////////////////////////////////////////////////////////////////////////
//Endpoint.php
class Endpoint
{
protected $api = null;
protected $endPointName = '';
public function __construct(Api $api, $endPointName)
{
$this->api = $api;
$this->endPointName = $endPointName;
}
/*
* $arguments example:
* ['search' => 'admin']
*/
public function get($arguments = [])
{
return $this->api->get(
$this->endPointName,[
'query' => $arguments
]
);
}
/*
* arguments:
* https://developer.wordpress.org/rest-api/reference/users/#create-a-user
* required: username, email, password
*/
public function create($arguments)
{
return $this->api->post(
$this->endPointName,[
'query' => $arguments
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment