Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
polidog committed May 28, 2015
0 parents commit 3a70f57
Show file tree
Hide file tree
Showing 10 changed files with 2,353 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
demo.php
vendor
.idea
96 changes: 96 additions & 0 deletions ApiMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace Polidog\Esa;
use GuzzleHttp\ClientInterface;

/**
* Class ApiMethods
*/
class ApiMethods
{
private $httpClient;
private $currentTeam;

/**
* @param ClientInterface $client
* @param $currentTeam
*/
public function __construct(ClientInterface $client, $currentTeam)
{
$this->httpClient = $client;
$this->currentTeam = $currentTeam;
}

/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function teams()
{
return $this->httpClient->request('GET','teams');
}

/**
* @param null $name
* @return \Psr\Http\Message\ResponseInterface
*/
public function team($name = null)
{
return $this->httpClient->request('GET',"teams/{$name}");
}

/**
* @param array $params
* @return \Psr\Http\Message\ResponseInterface
*/
public function posts(array $params = [])
{
return $this->httpClient->request('GET',"teams/{$this->currentTeam}/posts",[
'query' => $params
]);
}

/**
* @param $number
* @param array $params
* @return \Psr\Http\Message\ResponseInterface
*/
public function post($number)
{
return $this->httpClient->request('GET',"teams/{$this->currentTeam}/posts/{$number}");
}

/**
* @param $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function createPost($data)
{
return $this->httpClient->request('POST',"teams/{$this->currentTeam}/posts",[
'json' => [
'post' => $data
]
]);
}

/**
* @param $number
* @param $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function updatePost($number, $data)
{
return $this->httpClient->request('PATCH',"teams/{$this->currentTeam}/posts/{$number}",[
'json' => [
'post' => $data
]
]);
}

/**
* @param $number
* @return \Psr\Http\Message\ResponseInterface
*/
public function deletePost($number)
{
return $this->httpClient->request('DELETE',"teams/{$this->currentTeam}/posts/{$number}");
}
}
72 changes: 72 additions & 0 deletions Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Polidog\Esa;

use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;

final class Client
{
private $accessToken;

private $currentTeam;

private $httpClient;

private $apiMethods;

/**
* @var array
*/
private $httpOptions = [
'base_uri' => 'https://api.esa.io/v1/',
'timeout' => 60,
'allow_redirect' => false,
'headers' => [
'User-Agent' => 'esa-php-api v1',
'Accept' => 'application/json',
]
];

/**
* @param $accessToken
* @param $currentTeam
* @param HttpClientInterface $httpClient
* @param array $httpOptions
*/
public function __construct($accessToken, $currentTeam, HttpClientInterface $httpClient = null, $httpOptions = [])
{
$this->accessToken = $accessToken;
$this->currentTeam = $currentTeam;
$this->httpOptions = array_merge($this->httpOptions, $httpOptions);
$this->httpOptions['handler'] = $this->createAuthStack();

if (empty($httpClient)) {
$httpClient = new \GuzzleHttp\Client($this->httpOptions);
}
$this->httpClient = $httpClient;
$this->apiMethods = new ApiMethods($httpClient, $currentTeam);

}

public function __call($name, $args)
{
/** @var Response $response */
$response = call_user_func_array([$this->apiMethods,$name],$args);
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody()->getContents(), true);
}
}

private function createAuthStack()
{
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function(RequestInterface $request){
return $request->withHeader('Authorization', "Bearer ".$this->accessToken);
}));
return $stack;
}

}
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# esa-php

esa API v1 client library, written in PHP

## Installation

The recommended way to install esa-php is through Composer.

```
# Install Composer
curl -sS https://getcomposer.org/installer | php
```
Next, run the Composer command to install the lasted stable version of esa-php.

```
composer.phar install polidog/esa-php
```

## Usage

```
<?php
require 'vendor/autoload.php';
$client = new Polidog\Esa\Client("<access_token>", "foobar");
$client->teams();
// GET /v1/teams
$client->team('bar');
// GET /v1/teams/bar
$client->posts();
// GET /v1/teams/foobar/posts
$client->posts(["q" => "in:help"]);
// GET /v1/teams/foobar/posts?q=in%3Ahelp
$client->createPost(["name" => "foo"]);
// POST /v1/teams/foobar/posts
$client->updatePost(1, ["name" => "bar"]);
// PATCH /v1/teams/foobar/posts/1
$client->deletePost(1);
// DELETE /v1/teams/foobar/posts/1
```

## Contributing

1. Fork it ( https://github.com/polidog/esa-php/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "polidog/esa-php",
"description": "esa.io api library",
"license": "MIT",
"authors": [
{
"name": "polidog",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "4.6.7",
"phpunit/phpcov": "*",
"phake/phake": "2.0.*@dev",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {
"Polidog\\Esa\\": ""
}
}
}
Loading

0 comments on commit 3a70f57

Please sign in to comment.