<?php

namespace Powermash\ComicVine;

use Buzz\Browser;
use Buzz\Exception\RequestException;

/**
* 
*/
class Client
{
	protected $apiKey;
	protected $browser;

	public function __construct($key)
	{
		$this->apiKey = $key;

		$this->browser = new Browser();
	}

	public function randomCharacter()
	{
		$url = 'http://www.comicvine.com/api/characters?'.http_build_query([
				'offset' 		=> mt_rand(0, 87150),
				'limit' 		=> 1,
				'format' 		=> 'json',
				'field_list'	=> 'name,image,publisher,description',
				'api_key' 		=> $this->apiKey,
			]);

		$response = $this->browser->get($url);
		$characters = json_decode($response->getContent(), true);

		$character = reset($characters['results']);

		return [
			'name' => $character['name'],
			'description' => $character['description'],
			'thumbnail' => $character['image']['super_url']
		];
	}
}
<?php

namespace Powermash\ComicVine;

use Buzz\Browser;
use Buzz\Exception\RequestException;

/**
* 
*/
class Client
{
	const DEFAULT_ENDPOINT = 'http://www.comicvine.com/api/characters';

	protected $apiKey;
	protected $endpoint;
	protected $browser;

	public function __construct($key, $endpoint = null)
	{
		$this->apiKey = $key;
		$this->endpoint = $endpoint ?: self::DEFAULT_ENDPOINT;
		$this->browser = new Browser();
	}

	public function randomCharacter()
	{
		$payload = $this->sendRequest('characters', ['offset' => mt_rand(0, 87150), 'limit' => 1]);
		$character = reset($payload['results']);

		return [
			'name' => $character['name'],
			'description' => $character['description'],
			'thumbnail' => $character['image']['super_url']
		];
	}

	public function sendRequest($ressource, array $parameters = array())
	{
		$url = sprintf('%s?%s', $this->endpoint, http_build_query([
			'format' => 'json',
			'api_key' => $this->apiKey
		]));

		$response = $this->browser->get($url);
		$payload = json_decode($response->getContent(), true);

		if ($error = json_last_error()) {
			throw new JSONException($error);
		}

		return $payload;
	}
}

/**
* 
*/
class JSONException extends \RuntimeException
{
	
}
Add dedicated method to send request
Add dedicated method to send request
