From 66bc5d50d58a5320e73e9b9866d51e8b9dfa5601 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 28 Apr 2015 21:38:59 +0200 Subject: [PATCH] =?UTF-8?q?HttpClient=20f=C3=BCr=20GuzzleHttp=20geschriebe?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.php | 19 +++++++------------ src/HttpClient.php | 38 +++++++++++++++++++++++++++++++++++++ src/HttpClientInterface.php | 19 +++++++++++++++++++ 3 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 src/HttpClient.php create mode 100644 src/HttpClientInterface.php diff --git a/src/Client.php b/src/Client.php index 3a3106b..78a9e5b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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; diff --git a/src/HttpClient.php b/src/HttpClient.php new file mode 100644 index 0000000..79de82c --- /dev/null +++ b/src/HttpClient.php @@ -0,0 +1,38 @@ +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)); + } +} diff --git a/src/HttpClientInterface.php b/src/HttpClientInterface.php new file mode 100644 index 0000000..ffecc4d --- /dev/null +++ b/src/HttpClientInterface.php @@ -0,0 +1,19 @@ +