Skip to content

Commit 25f690e

Browse files
committed
Update http client
1 parent c60870c commit 25f690e

File tree

6 files changed

+87
-56
lines changed

6 files changed

+87
-56
lines changed

composer.json

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,16 @@
55
"license": "MIT",
66
"keywords": ["mailerlite", "sdk", "email", "marketing"],
77
"type": "library",
8-
"authors": [
9-
{
10-
"name": "Justinas Pošiūnas",
11-
"email": "justinas.posiunas@mailerlite.com",
12-
"homepage": "https://mailerlite.com",
13-
"role": "Developer"
14-
}
15-
],
16-
"require": {
17-
"php" : "^5.5|^7.0",
18-
"guzzlehttp/psr7": "~1.2",
19-
"php-http/httplug": "^1.0",
20-
"php-http/curl-client": "^1.4",
21-
"php-http/message": "^1.2"
8+
"require": {
9+
"php" : "^7.1",
10+
"php-http/client-common": "^2.0",
11+
"php-http/discovery": "^1.7",
12+
"nyholm/psr7": "^1.0",
13+
"ext-json": "*"
2214
},
2315
"require-dev": {
24-
"php-http/guzzle6-adapter": "^1.0",
25-
"phpunit/phpunit": "5.3.*",
26-
"mockery/mockery": "^0.9.4"
16+
"php-http/guzzle6-adapter": "^2.0",
17+
"phpunit/phpunit": "6.*"
2718
},
2819
"autoload": {
2920
"psr-4": {

src/Common/RestClient.php

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace MailerLiteApi\Common;
44

55
use Http\Client\HttpClient;
6-
use Http\Client\Curl\Client as CurlClient;
7-
8-
use GuzzleHttp\Psr7\Request;
9-
use MailerLiteApi\Common\ApiConstants;
6+
use Http\Discovery\Psr17FactoryDiscovery;
7+
use Http\Discovery\Psr18ClientDiscovery;
8+
use Psr\Http\Client\ClientInterface;
9+
use Psr\Http\Message\RequestFactoryInterface;
1010
use Psr\Http\Message\ResponseInterface;
11-
12-
use Http\Message\MessageFactory\GuzzleMessageFactory;
13-
use Http\Message\StreamFactory\GuzzleStreamFactory;
11+
use Psr\Http\Message\StreamFactoryInterface;
1412

1513
class RestClient {
1614

@@ -20,10 +18,14 @@ class RestClient {
2018

2119
public $baseUrl;
2220

21+
public $requestFactory;
22+
23+
public $streamFactory;
24+
2325
/**
24-
* @param string $baseUrl
25-
* @param string $apiKey
26-
* @param HttpClient $httpClient
26+
* @param string $baseUrl
27+
* @param string $apiKey
28+
* @param \Http\Client\HttpClient|null $httpClient
2729
*/
2830
public function __construct($baseUrl, $apiKey, HttpClient $httpClient = null)
2931
{
@@ -90,11 +92,25 @@ public function delete($endpointUri)
9092
*/
9193
protected function send($method, $endpointUri, $body = null, array $headers = [])
9294
{
95+
9396
$headers = array_merge($headers, self::getDefaultHeaders());
9497
$endpointUrl = $this->baseUrl . $endpointUri;
9598

96-
$request = new Request($method, $endpointUrl, $headers, json_encode($body));
97-
$response = $this->getHttpClient()->sendRequest($request);
99+
$request = $this->getRequestFactory()->createRequest($method, $endpointUrl);
100+
101+
if ($body) {
102+
$stream = $this->getStreamFactory()
103+
->createStream(json_encode($body));
104+
$request = $request->withBody($stream);
105+
}
106+
107+
foreach ($headers as $name => $value) {
108+
$request = $request->withAddedHeader($name, $value);
109+
}
110+
111+
$response = $this->getHttpClient()->sendRequest(
112+
$request
113+
);
98114

99115
return $this->handleResponse($response);
100116
}
@@ -117,30 +133,52 @@ protected function handleResponse(ResponseInterface $response)
117133
}
118134

119135
/**
120-
* @return HttpClient
136+
* @return ClientInterface
121137
*/
122138
protected function getHttpClient()
123139
{
124140
if (is_null($this->httpClient)) {
125-
$options = [
126-
CURLOPT_CONNECTTIMEOUT => 10,
127-
CURLOPT_SSL_VERIFYPEER => false
128-
];
129-
130-
$this->httpClient = new CurlClient(new GuzzleMessageFactory(), new GuzzleStreamFactory(), $options);
141+
$this->httpClient = Psr18ClientDiscovery::find();
131142
}
132143

133144
return $this->httpClient;
134145
}
135146

147+
/**
148+
* @return HttpClient
149+
*/
150+
protected function getRequestFactory(): RequestFactoryInterface
151+
{
152+
if (null === $this->requestFactory) {
153+
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
154+
}
155+
156+
return $this->requestFactory;
157+
}
158+
159+
private function getStreamFactory(): StreamFactoryInterface
160+
{
161+
if (null === $this->streamFactory) {
162+
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
163+
}
164+
165+
return $this->streamFactory;
166+
}
167+
136168
/**
137169
* @return array
138170
*/
139171
protected function getDefaultHeaders() {
140-
return [
141-
'User-Agent' => ApiConstants::SDK_USER_AGENT . '/' . ApiConstants::SDK_VERSION,
142-
'X-MailerLite-ApiKey' => $this->apiKey,
143-
'Content-Type' => 'application/json'
172+
$headers = [
173+
'User-Agent' => ApiConstants::SDK_USER_AGENT.'/'.ApiConstants::SDK_VERSION,
174+
'Content-Type' => 'application/json',
144175
];
176+
177+
// Only adding it when provided. Not required for RestClientTest
178+
if ($this->apiKey) {
179+
$headers['X-MailerLite-ApiKey'] = $this->apiKey;
180+
}
181+
182+
return $headers;
145183
}
146-
}
184+
}

tests/GroupsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function check_groups_count()
8585
{
8686
$groupsCount = $this->groupsApi->count();
8787

88-
$this->assertTrue(array_key_exists('count', $groupsCount) && is_numeric($groupsCount->count));
88+
$this->assertTrue(isset($groupsCount->count) && is_numeric($groupsCount->count));
8989
}
9090

9191
/** @test **/
@@ -130,4 +130,4 @@ public function import_subscribers()
130130
$this->assertTrue( ! empty($addedSubscribers));
131131
}
132132

133-
}
133+
}

tests/MailerLiteTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class MailerLiteTest extends MlTestCase
99
/** @test **/
1010
public function invalid_api_key()
1111
{
12-
$this->setExpectedException('\\MailerLiteApi\\Exceptions\\MailerLiteSdkException');
12+
$this->expectException('\\MailerLiteApi\\Exceptions\\MailerLiteSdkException');
1313

1414
$ml = new MailerLite();
1515
}
16-
}
16+
}

tests/MlTestCase.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace MailerLiteApi\Tests;
44

5-
use MailerLiteApi\MailerLite;
65
use MailerLiteApi\Api\Groups;
76

8-
class MlTestCase extends \PHPUnit_Framework_TestCase
7+
class MlTestCase extends \PHPUnit\Framework\TestCase
98
{
9+
/** @var Groups */
10+
protected $groupsApi;
11+
1012
// helper functions
1113
protected function createGroup($name = 'New Group')
1214
{
@@ -40,12 +42,12 @@ protected function addSubscribers($groupId, $count = 5)
4042

4143
protected function assertContainsValue($list, $key, $value)
4244
{
43-
return $this->assertTrue($this->containsValue($list, $key, $value));
45+
$this->assertTrue($this->containsValue($list, $key, $value));
4446
}
4547

4648
protected function assertDoesNotContainValue($list, $key, $value)
4749
{
48-
return $this->assertFalse($this->containsValue($list, $key, $value));
50+
$this->assertFalse($this->containsValue($list, $key, $value));
4951
}
5052

5153
protected function containsValue($list, $key, $value)
@@ -62,4 +64,4 @@ protected function containsValue($list, $key, $value)
6264
return $found;
6365
}
6466

65-
}
67+
}

tests/RestClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,22 @@ public function delete_method()
7070
{
7171
$response = $this->client->delete('delete');
7272

73-
$this->assertEquals('null', $response['body']->data);
73+
$this->assertEquals('', $response['body']->data);
7474
}
7575

7676
/** @test **/
7777
public function error_internal_server()
7878
{
79-
$this->setExpectedException('Exception');
79+
$this->expectException('Exception');
8080

81-
$response = $this->client->get('status/500');
81+
$this->client->get('status/500');
8282
}
8383

8484
/** @test **/
8585
public function error_400()
8686
{
87-
$this->setExpectedException('Exception');
87+
$this->expectException('Exception');
8888

89-
$response = $this->client->get('status/400');
89+
$this->client->get('status/400');
9090
}
91-
}
91+
}

0 commit comments

Comments
 (0)