Skip to content

Commit

Permalink
HttpClient für GuzzleHttp geschrieben
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Apr 28, 2015
1 parent 06fa496 commit 66bc5d5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public function get($path, array $data = array())
/**
* Set a http client
*
* @param object $client the http client
* @param HttpClientInterface $client the http client
* @return self
*/
public function setHttpClient($client)
public function setHttpClient(HttpClientInterface $client)
{
$this->http_client = $client;

Expand Down Expand Up @@ -114,27 +114,22 @@ protected function runRequest($path, $method = 'GET', array $data = array())
throw new \InvalidArgumentException('The method "' . $method . '" is not supported.');
}

$client = $this->getHttpClient();
$method = $methods[$method];

$request = $client->createRequest($method, $this->getUrl() . $path, array(
'query' => $data,
));

$response = $client->send($request);

return $response->json();
return $this->getHttpClient()
->$method($this->getUrl() . $path, array('query' => $data));
}

/**
* Returns the http client
*
* @return HttpClient The Http client
* @return HttpClientInterface The Http client
*/
protected function getHttpClient()
{
if ( $this->http_client === null )
{
$this->setHttpClient(new \GuzzleHttp\Client());
$this->setHttpClient(new HttpClient());
}

return $this->http_client;
Expand Down
38 changes: 38 additions & 0 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Youthweb\Api;

use GuzzleHttp;

/**
* Http client based on GuzzleHttp
*/
class HttpClient implements HttpClientInterface
{
protected $http_client = null;

/**
* Creates the http client
*
* @return HttpClient The Http client
*/
public function __construct()
{
$this->http_client = new GuzzleHttp\Client();
}

/**
* Send a GET request
*
* @param string|array $url URL
* @param array $options Array of request options to apply.
*
* @return mixed
*/
public function get($url = null, array $options = array())
{
$response = $this->http_client->get($url, $options);

return $response->json(array('object' => true));
}
}
19 changes: 19 additions & 0 deletions src/HttpClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Youthweb\Api;

/**
* Http client interface
*/
interface HttpClientInterface
{
/**
* Send a GET request
*
* @param string|array $url URL
* @param array $options Array of request options to apply.
*
* @return mixed
*/
public function get($url = null, array $options = array());
}

0 comments on commit 66bc5d5

Please sign in to comment.