Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 02435fb

Browse files
committed
🛀 decouple from httpinterface lib
1 parent 51cb4da commit 02435fb

15 files changed

+153
-71
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
"ext-json":"*",
3030
"ext-simplexml":"*",
3131
"ext-zlib": "*",
32+
"chillerlan/php-http-message-utils": "^1.0",
3233
"chillerlan/php-settings-container": "^2.1",
33-
"chillerlan/php-httpinterface": "dev-main#404000a5ce159de8121f7eb98ecc58723cdb747c",
3434
"psr/http-client":"^1.0",
3535
"psr/http-message": "^1.0",
3636
"psr/log": "^1.1"
3737
},
3838
"require-dev": {
3939
"chillerlan/php-dotenv": "^2.1",
40+
"guzzlehttp/guzzle": "^7.3",
41+
"guzzlehttp/psr7": "^2.0.0-beta1",
4042
"phan/phan": "^4.0",
4143
"phpunit/phpunit": "^9.5"
4244
},

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@
2525
</logging>
2626
<php>
2727
<const name="TEST_IS_CI" value="true"/>
28+
<const name="TEST_CLIENT_FACTORY" value="chillerlan\OAuthTest\GuzzleClientFactory"/>
29+
<const name="REQUEST_FACTORY" value="GuzzleHttp\Psr7\HttpFactory"/>
30+
<const name="RESPONSE_FACTORY" value="GuzzleHttp\Psr7\HttpFactory"/>
31+
<const name="STREAM_FACTORY" value="GuzzleHttp\Psr7\HttpFactory"/>
2832
</php>
2933
</phpunit>

src/Core/OAuth1Provider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
namespace chillerlan\OAuth\Core;
1414

15-
use chillerlan\HTTP\Psr7\Query;
15+
use chillerlan\HTTP\Utils\Query;
1616
use DateTime;
1717
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
1818

1919
use function array_merge, base64_encode, bin2hex, function_exists, hash_hmac,
2020
implode, in_array, is_array, parse_url, random_bytes, strtoupper;
21-
use function chillerlan\HTTP\Psr7\{decompress_content, r_rawurlencode};
21+
use function chillerlan\HTTP\Utils\{decompress_content, r_rawurlencode};
2222

2323
/**
2424
* Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.

src/Core/OAuth2Provider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
namespace chillerlan\OAuth\Core;
1717

18-
use chillerlan\HTTP\Psr7\Query;
18+
use chillerlan\HTTP\Utils\Query;
1919
use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
2020

2121
use function array_merge, base64_encode, date, hash_equals, http_build_query,
2222
implode, is_array, json_decode, random_bytes, sha1, sprintf;
23-
use function chillerlan\HTTP\Psr7\decompress_content;
23+
use function chillerlan\HTTP\Utils\decompress_content;
2424

2525
use const JSON_THROW_ON_ERROR, PHP_QUERY_RFC1738;
2626

src/Core/OAuthProvider.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace chillerlan\OAuth\Core;
1414

15-
use chillerlan\HTTP\Psr17\{RequestFactory, StreamFactory, UriFactory};
16-
use chillerlan\HTTP\Psr7\Query;
15+
use GuzzleHttp\Psr7\HttpFactory;
16+
use chillerlan\HTTP\Utils\Query;
1717
use chillerlan\OAuth\MagicAPI\{ApiClientException, EndpointMap, EndpointMapInterface};
1818
use chillerlan\OAuth\Storage\OAuthStorageInterface;
1919
use chillerlan\Settings\SettingsContainerInterface;
@@ -162,9 +162,12 @@ public function __construct(
162162
$this->options = $options;
163163
$this->logger = $logger ?? new NullLogger;
164164

165-
$this->requestFactory = new RequestFactory;
166-
$this->streamFactory = new StreamFactory;
167-
$this->uriFactory = new UriFactory;
165+
// i hate this, but i also hate adding 3 more params to the constructor
166+
// no i won't use a DI container for this. don't @ me
167+
$factory = new HttpFactory;
168+
$this->requestFactory = $factory;
169+
$this->streamFactory = $factory;
170+
$this->uriFactory = $factory;
168171

169172
$this->serviceName = (new ReflectionClass($this))->getShortName();
170173

src/OAuthOptions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace chillerlan\OAuth;
1212

13-
use chillerlan\HTTP\HTTPOptionsTrait;
1413
use chillerlan\Settings\SettingsContainerAbstract;
1514

1615
/**
@@ -41,5 +40,5 @@
4140
* @property array $curl_multi_options
4241
*/
4342
class OAuthOptions extends SettingsContainerAbstract{
44-
use OAuthOptionsTrait, HTTPOptionsTrait;
43+
use OAuthOptionsTrait;
4544
}

tests/API/OAuthAPITestAbstract.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ protected function initOptions():SettingsContainerInterface{
4242
'key' => $this->dotEnv->get($this->ENV.'_KEY'),
4343
'secret' => $this->dotEnv->get($this->ENV.'_SECRET'),
4444
'tokenAutoRefresh' => true,
45-
// HTTPOptionsTrait
46-
'ca_info' => $this->CFG.DIRECTORY_SEPARATOR.'cacert.pem',
47-
'userAgent' => 'chillerlanPhpOAuth/4.0.0 +https://github.com/chillerlan/php-oauth-core',
48-
'sleep' => 0.25, // see OAuthTestHttpClient::sendRequest()
4945
]);
5046
}
5147

@@ -54,7 +50,7 @@ protected function initStorage(SettingsContainerInterface $options):OAuthStorage
5450
}
5551

5652
protected function initHttp(SettingsContainerInterface $options, LoggerInterface $logger, array $responses):ClientInterface{
57-
return new OAuthTestHttpClient($options, null, $logger);
53+
return new OAuthTestHttpClient($this->CFG, $logger);
5854
}
5955

6056
}

tests/GuzzleClientFactory.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Class GuzzleClientFactory
4+
*
5+
* @created 01.04.2021
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2021 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\OAuthTest;
12+
13+
use GuzzleHttp\Client;
14+
use Psr\Http\Client\ClientInterface;
15+
16+
class GuzzleClientFactory implements OAuthTestHttpClientFactoryInterface{
17+
18+
public static function getClient(string $cfgdir):ClientInterface{
19+
return new Client([
20+
'verify' => $cfgdir.'/cacert.pem',
21+
'headers' => [
22+
'User-Agent' => 'chillerlanPhpOAuth/4.0.0 +https://github.com/chillerlan/php-oauth-core',
23+
],
24+
]);
25+
}
26+
27+
}

tests/OAuthTestHttpClient.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,60 @@
1010

1111
namespace chillerlan\OAuthTest;
1212

13-
use chillerlan\HTTP\Psr18\{CurlClient, LoggingClient};
14-
use chillerlan\Settings\SettingsContainerInterface;
13+
use Psr\Http\Client\ClientExceptionInterface;
1514
use Psr\Http\Client\ClientInterface;
1615
use Psr\Http\Message\{RequestInterface, ResponseInterface};
17-
use Psr\Log\{LoggerAwareInterface, LoggerInterface, NullLogger};
16+
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
17+
use Exception, Throwable;
1818

19-
use function usleep;
19+
use function chillerlan\HTTP\Utils\message_to_string;
20+
use function constant, defined, get_class, usleep;
2021

2122
class OAuthTestHttpClient implements ClientInterface, LoggerAwareInterface{
23+
use LoggerAwareTrait;
2224

23-
protected SettingsContainerInterface $options;
24-
25-
protected ClientInterface $client;
25+
protected ClientInterface $http;
2626

2727
public function __construct(
28-
SettingsContainerInterface $options,
29-
ClientInterface $http = null,
28+
string $cfgdir,
3029
LoggerInterface $logger = null
3130
){
32-
$this->options = $options;
33-
$this->client = new LoggingClient(
34-
$http ?? new CurlClient($this->options),
35-
$logger ?? new NullLogger
36-
);
37-
}
3831

39-
/**
40-
* @inheritDoc
41-
*/
42-
public function setLogger(LoggerInterface $logger):void{
43-
$this->client->setLogger($logger);
32+
if(!defined('TEST_CLIENT_FACTORY')){
33+
throw new Exception('TEST_CLIENT_FACTORY in phpunit.xml not set');
34+
}
35+
36+
$clientFactory = constant('TEST_CLIENT_FACTORY');
37+
38+
$this->http = $clientFactory::getClient($cfgdir);
39+
$this->logger = $logger ?? new NullLogger;
4440
}
4541

4642
/**
4743
* @inheritDoc
4844
*/
4945
public function sendRequest(RequestInterface $request):ResponseInterface{
50-
usleep($this->options->sleep * 1000000);
46+
$this->logger->debug("\n----HTTP-REQUEST----\n".message_to_string($request));
47+
usleep(250000);
48+
49+
try{
50+
$response = $this->http->sendRequest($request);
51+
}
52+
catch(Throwable $e){
53+
$this->logger->debug("\n----HTTP-ERROR------\n".message_to_string($request));
54+
$this->logger->error($e->getMessage());
55+
$this->logger->error($e->getTraceAsString());
56+
57+
if(!$e instanceof ClientExceptionInterface){
58+
throw new Exception('unexpected exception, does not implement "ClientExceptionInterface": '.get_class($e));
59+
}
60+
61+
throw $e;
62+
}
63+
64+
$this->logger->debug("\n----HTTP-RESPONSE---\n".message_to_string($response));
5165

52-
return $this->client->sendRequest($request);
66+
return $response;
5367
}
5468

5569
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Interface OAuthTestHttpClientFactoryInterface
4+
*
5+
* @created 01.04.2021
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2021 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\OAuthTest;
12+
13+
use Psr\Http\Client\ClientInterface;
14+
15+
interface OAuthTestHttpClientFactoryInterface{
16+
17+
/**
18+
* Returns a fully prepared http client instance
19+
*/
20+
public static function getClient(string $cfgdir):ClientInterface;
21+
22+
}

0 commit comments

Comments
 (0)