Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool committed Jul 25, 2019
0 parents commit 9330840
Show file tree
Hide file tree
Showing 13 changed files with 710 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/phpunit.xml.dist export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
4 changes: 4 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
filter:
excluded_paths:
- tests/*
- vendor/*
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- nightly

matrix:
fast_finish: true
include:
- php: 7.1
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable"
allow_failures:
- php: nightly

install:
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

script:
- vendor/bin/phpunit --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-2018 Jérémy DECOOL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Clubhouse API client
====================

A simple PHP client for Clubhouse.io REST API.

## Install it

Install using [composer](https://getcomposer.org):

```bash
composer require jdecool/clubhouse-api "`php-http/guzzle6-adapter`:^1.0"
```

The library is decoupled from any HTTP message client with [HTTPlug](http://httplug.io). That's why you need to install a client implementation `http://httplug.io/` in this example.

## Getting started

```php
<?php

require __DIR__.'/vendor/autoload.php';

$builder = new JDecool\Clubhouse\ClientBuilder();
$client = $builder->createClientV2('your-clubhouse-token'); // create client for Clubhouse API v2 (v1 is also available)

$story = $client->get('stories/144');
```

## LICENSE

This library is licensed under the MIT License.
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "jdecool/clubhouse-api",
"type": "library",
"description": "PHP SDK for Clubhouse",
"license": "MIT",
"authors": [
{
"name": "Jérémy DECOOL",
"email": "[email protected]"
}
],
"require": {
"php": "^7.1",
"ext-json": "*",
"php-http/client-common": "^1.7",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.4",
"php-http/httplug": "^1.1"
},
"require-dev": {
"php-http/guzzle6-adapter": "^1.0",
"phpunit/phpunit": "^7.5"
},
"autoload": {
"psr-4": {
"JDecool\\Clubhouse\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JDecool\\Clubhouse\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Clubhouse PHP client">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
113 changes: 113 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace JDecool\Clubhouse;

use Http\Client\Common\HttpMethodsClient;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;

class Client
{
private const ENDPOINT_V1 = 'https://api.clubhouse.io/api/v1';
private const ENDPOINT_V2 = 'https://api.clubhouse.io/api/v2';

private $http;
private $baseUri;
private $token;

public static function createV1(HttpMethodsClient $http, string $token): self
{
return new self($http, self::ENDPOINT_V1, $token);
}

public static function createV2(HttpMethodsClient $http, string $token): self
{
return new self($http, self::ENDPOINT_V2, $token);
}

public function __construct(HttpMethodsClient $http, string $baseUri, string $token)
{
if (false === filter_var($baseUri, FILTER_VALIDATE_URL)) {
throw new RuntimeException('Invalid Clubouse base URI.');
}

$this->http = $http;
$this->baseUri = $baseUri;
$this->token = $token;
}

public function get(string $uri): array
{
$response = $this->http->get(
$this->endpoint($uri)
);

if (200 !== $response->getStatusCode()) {
throw $this->createExceptionFromResponse($response);
}

return json_decode((string) $response->getBody(), true);
}

public function post(string $uri, array $data): array
{
$response = $this->http->post(
$this->endpoint($uri),
['Content-Type' => 'application/json'],
json_encode($data)
);

if (201 !== $response->getStatusCode()) {
throw $this->createExceptionFromResponse($response);
}

return json_decode((string) $response->getBody(), true);
}

public function put(string $uri, array $data): array
{
$response = $this->http->put(
$this->endpoint($uri),
['Content-Type' => 'application/json'],
json_encode($data)
);

if (200 !== $response->getStatusCode()) {
throw $this->createExceptionFromResponse($response);
}

return json_decode((string) $response->getBody(), true);
}

public function delete(string $uri): array
{
$response = $this->http->delete(
$this->endpoint($uri)
);

if (204 !== $response->getStatusCode()) {
throw $this->createExceptionFromResponse($response);
}

return json_decode((string) $response->getBody(), true);
}

private function endpoint(string $uri): string
{
return sprintf(
'%s/%s?token=%s',
rtrim($this->baseUri, '/'),
ltrim($uri, '/'),
$this->token
);
}

private function createExceptionFromResponse(ResponseInterface $response): ClubhouseException
{
$content = json_decode((string) $response->getBody(), true);

return new ClubhouseException($content['message'] ?? 'An error occured.');
}
}
54 changes: 54 additions & 0 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace JDecool\Clubhouse;

use Http\{
Client\Common\HttpMethodsClient,
Client\HttpClient,
Discovery\HttpClientDiscovery,
Discovery\MessageFactoryDiscovery,
Message\MessageFactory
};
use RuntimeException;

class ClientBuilder
{
public const V1 = 'v1';
public const V2 = 'v2';

private $httpClient;
private $messageFactory;

public function __construct(?HttpClient $httpClient = null, ?MessageFactory $messageFactory = null)
{
$this->httpClient = $httpClient ?? HttpClientDiscovery::find();
$this->messageFactory = $messageFactory ?? MessageFactoryDiscovery::find();
}

public function createClientV1(string $token): Client
{
return $this->create(self::V1, $token);
}

public function createClientV2(string $token): Client
{
return $this->create(self::V2, $token);
}

public function create(string $version, string $token): Client
{
$http = new HttpMethodsClient($this->httpClient, $this->messageFactory);

switch ($version) {
case self::V1:
return Client::createV1($http, $token);

case self::V2:
return Client::createV2($http, $token);
}

throw new RuntimeException("Version '$version' is not supported.");
}
}
11 changes: 11 additions & 0 deletions src/ClubhouseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace JDecool\Clubhouse;

use RuntimeException;

class ClubhouseException extends RuntimeException
{
}
59 changes: 59 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace JDecool\Clubhouse\Tests;

use JDecool\{
Clubhouse\Client,
Clubhouse\ClientBuilder
};
use PHPUnit\Framework\TestCase;
use RuntimeException;

class ClientBuilderTest extends TestCase
{
public function testCreateClientV1(): void
{
$builder = new ClientBuilder();
$client = $builder->createClientV1('foo');

$this->assertInstanceOf(Client::class, $client);
}

public function testCreateClientV2(): void
{
$builder = new ClientBuilder();
$client = $builder->createClientV2('foo');

$this->assertInstanceOf(Client::class, $client);
}

/**
* @dataProvider versions
*/
public function testCreateClient(string $version): void
{
$builder = new ClientBuilder();
$client = $builder->create($version, 'foo');

$this->assertInstanceOf(Client::class, $client);
}

public function testCreateClientWithInvalidVersion(): void
{
$builder = new ClientBuilder();

$this->expectException(RuntimeException::class);

$builder->create('foo', 'bar');
}

public function versions(): array
{
return [
[ClientBuilder::V1],
[ClientBuilder::V2],
];
}
}
Loading

0 comments on commit 9330840

Please sign in to comment.