Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support guzzlehttp7 #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/.idea
/composer.lock
/vendor
/build
/test-reports
!.editorconfig
!.gitattributes
!.gitignore
!.gitkeep
!.php-cs-fixer.dist.php
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"guzzlehttp/guzzle": "6.*"
"guzzlehttp/guzzle": "^6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "~5.7"
Expand Down
24 changes: 12 additions & 12 deletions src/Lamia/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HttpClient implements HttpClientInterface
*/
protected $client;

protected $headers = array();
protected $headers = [];
private $lastResponse;
private $lastRequest;

Expand All @@ -29,7 +29,7 @@ class HttpClient implements HttpClientInterface
* @param array $options
* @param ClientInterface $client
*/
public function __construct($baseUrl, array $options = array(), ClientInterface $client = null)
public function __construct($baseUrl, array $options = [], ClientInterface $client = null)
{
$this->options = array_merge($this->options, $options);
$this->options['base_uri'] = $baseUrl;
Expand Down Expand Up @@ -66,15 +66,15 @@ public function setHeaders(array $headers)
public function clearHeaders()
{
$this->headers = array(
'User-Agent' => sprintf('%s', $this->options['user_agent']),
'User-Agent' => \sprintf('%s', $this->options['user_agent']),
);
}


/**
* {@inheritDoc}
*/
public function get($path, array $parameters = array(), array $headers = array())
public function get($path, array $parameters = [], array $headers = [])
{
$path .= '?'. http_build_query($parameters);
return $this->request($path, null, 'GET', $headers);
Expand All @@ -83,7 +83,7 @@ public function get($path, array $parameters = array(), array $headers = array()
/**
* {@inheritDoc}
*/
public function post($path, $body = null, array $headers = array())
public function post($path, $body = null, array $headers = [])
{
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
Expand All @@ -95,7 +95,7 @@ public function post($path, $body = null, array $headers = array())
/**
* {@inheritDoc}
*/
public function patch($path, $body = null, array $headers = array())
public function patch($path, $body = null, array $headers = [])
{
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
Expand All @@ -107,15 +107,15 @@ public function patch($path, $body = null, array $headers = array())
/**
* {@inheritDoc}
*/
public function delete($path, $body = null, array $headers = array())
public function delete($path, $body = null, array $headers = [])
{
return $this->request($path, $body, 'DELETE', $headers);
}

/**
* {@inheritDoc}
*/
public function put($path, $body, array $headers = array())
public function put($path, $body, array $headers = [])
{
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
Expand All @@ -130,8 +130,8 @@ public function request(
$path,
$body = null,
$httpMethod = 'GET',
array $headers = array(),
array $options = array()
array $headers = [],
array $options = []
) {
$request = $this->createRequest($httpMethod, $path, $body, $headers);
try {
Expand Down Expand Up @@ -166,13 +166,13 @@ public function getLastResponse()
* @param string $httpMethod
* @param string $path
*
* @return mixed|\Psr\Http\Message\ResponseInterface
* @return Request
*/
protected function createRequest(
$httpMethod,
$path,
$body = null,
array $headers = array()
array $headers = []
) {
return new Request(
$httpMethod,
Expand Down
40 changes: 24 additions & 16 deletions src/Lamia/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Lamia\HttpClient;

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\GuzzleException;
use Lamia\HttpClient\Exception\ErrorException;
use Lamia\HttpClient\Exception\InvalidArgumentException;
use Lamia\HttpClient\Exception\RuntimeException;
use Psr\Http\Message\ResponseInterface;

interface HttpClientInterface
{
Expand All @@ -14,19 +17,19 @@ interface HttpClientInterface
* @param array $parameters GET Parameters
* @param array $headers Reconfigure the request headers for this call only
*
* @return Response
* @return ResponseInterface
*/
public function get($path, array $parameters = array(), array $headers = array());
public function get($path, array $parameters = [], array $headers = []);
/**
* Send a POST request
*
* @param string $path Request path
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return Response
* @return ResponseInterface
*/
public function post($path, $body = null, array $headers = array());
public function post($path, $body = null, array $headers = []);
/**
* Send a PATCH request
*
Expand All @@ -35,29 +38,29 @@ public function post($path, $body = null, array $headers = array());
* @param array $headers Reconfigure the request headers for this call only
*
* @internal param array $parameters Request body
* @return Response
* @return ResponseInterface
*/
public function patch($path, $body = null, array $headers = array());
public function patch($path, $body = null, array $headers = []);
/**
* Send a PUT request
*
* @param string $path Request path
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return Response
* @return ResponseInterface
*/
public function put($path, $body, array $headers = array());
public function put($path, $body, array $headers = []);
/**
* Send a DELETE request
*
* @param string $path Request path
* @param mixed $body Request body
* @param array $headers Reconfigure the request headers for this call only
*
* @return Response
* @return ResponseInterface
*/
public function delete($path, $body = null, array $headers = array());
public function delete($path, $body = null, array $headers = []);
/**
* Send a request to the server, receive a response,
* decode the response and returns an associative array
Expand All @@ -67,23 +70,28 @@ public function delete($path, $body = null, array $headers = array());
* @param string $httpMethod HTTP method to use
* @param array $headers Request headers
*
* @return Response
* @return ResponseInterface
*
* @throws ErrorException
* @throws RuntimeException
* @throws GuzzleException
*/
public function request($path, $body, $httpMethod = 'GET', array $headers = array());
public function request($path, $body, $httpMethod = 'GET', array $headers = []);
/**
* Change an option value.
*
* @param string $name The option name
* @param mixed $value The value
* @param string $name The option name
* @param mixed $value The value
*
* @throws InvalidArgumentException
*
* @return void
*/
public function setOption($name, $value);
/**
* Set HTTP headers
*
* @param array $headers
* @param array<string, string|int|float> $headers
* @return void
*/
public function setHeaders(array $headers);
Expand Down
7 changes: 4 additions & 3 deletions tests/Lamia/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Lamia\HttpClient;

Expand All @@ -23,22 +24,22 @@ public function testDummyGetRequest()
public function testDummyGetRequestWithParams()
{
$newInstance = new HttpClient(self::TEST_URL);
$result = json_decode($newInstance->get('posts', array('userId' => 2))->getBody()->getContents());
$result = json_decode($newInstance->get('posts', ['userId' => 2])->getBody()->getContents());
$this->assertEquals(2, $result[1]->userId);
}

public function testDummyPostRequest()
{
$newInstance = new HttpClient(self::TEST_URL);
$result = json_decode($newInstance->post('posts', json_encode(array('title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents());
$result = json_decode($newInstance->post('posts', json_encode(['title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents());

$this->assertNotEmpty($result->id);
}

public function testDummyPutRequest()
{
$newInstance = new HttpClient(self::TEST_URL);
$result = json_decode($newInstance->put('posts/1', json_encode(array('id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents());
$result = json_decode($newInstance->put('posts/1', json_encode(['id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents());

$this->assertEquals(1, $result->id);
}
Expand Down
21 changes: 0 additions & 21 deletions tests/bootstrap.php

This file was deleted.