1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-18 21:20:09 -07:00
2017-01-17 12:18:45 +01:00

70 lines
1.7 KiB
PHP

<?php
namespace AppBundle\Api\Client\Rebrickable;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\Asset\Exception\LogicException;
class Rebrickable
{
const BASE_URI = 'https://rebrickable.com/api/';
const FORMAT = 'json';
/**
* @var Client
*/
private $client;
/**
* @var string
*/
private $apiKey;
/**
* RebrickableAPI constructor.
*/
public function __construct($apiKey)
{
$this->client = new Client(['base_uri' => self::BASE_URI]);
$this->apiKey = $apiKey;
}
/**
* @param $method
* @param $parameters
*
* @return null|string
*/
public function call($method, $parameters = [])
{
$parameters['query']['key'] = $this->apiKey;
$parameters['query']['format'] = self::FORMAT;
try {
$response = $this->client->request('GET', $method, $parameters);
if ($response->getStatusCode() === 200) {
$content = $response->getBody()->getContents();
if ($content === 'INVALIDKEY') {
//TODO
throw new LogicException('Invalid API Key');
} elseif ($content === 'NOSET' || $content === 'NOPART') {
return null;
}
return $content;
}
//TODO
throw new LogicException($response->getStatusCode());
} catch (ConnectException $e) {
//TODO
throw new LogicException($e->getMessage());
} catch (ClientException $e) {
//TODO
throw new LogicException($e->getMessage());
}
}
}