Skip to content

Commit

Permalink
Remove ChinalableHttpClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Feb 24, 2022
1 parent b873430 commit 2ab9121
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 236 deletions.
122 changes: 105 additions & 17 deletions src/Kernel/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,22 @@

use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface;
use EasyWeChat\Kernel\Contracts\AccessTokenAwareHttpClient as AccessTokenAwareHttpClientInterface;
use EasyWeChat\Kernel\Contracts\ChainableHttpClient as ChainableHttpClientInterface;
use EasyWeChat\Kernel\Traits\ChainableHttpClient;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @method \Symfony\Contracts\HttpClient\ResponseInterface get(string|array $uri = [], array $options = [])
* @method \Symfony\Contracts\HttpClient\ResponseInterface post(string|array $uri = [], array $options = [])
* @method \Symfony\Contracts\HttpClient\ResponseInterface patch(string|array $uri = [], array $options = [])
* @method \Symfony\Contracts\HttpClient\ResponseInterface put(string|array $uri = [], array $options = [])
* @method \Symfony\Contracts\HttpClient\ResponseInterface delete(string|array $uri = [], array $options = [])
*/
class Client implements AccessTokenAwareHttpClientInterface, ChainableHttpClientInterface
class Client implements AccessTokenAwareHttpClientInterface
{
use DecoratorTrait;
use ChainableHttpClient;

protected ?AccessTokenInterface $accessToken;

public function __construct(
?HttpClientInterface $client = null,
string $uri = '/',
?AccessTokenInterface $accessToken = null,
protected ?AccessTokenInterface $accessToken = null,
) {
$this->uri = $uri;
$this->client = $client ?? HttpClient::create();
$this->accessToken = $accessToken;
}

public function withAccessToken(AccessTokenInterface $accessToken): static
Expand All @@ -56,4 +44,104 @@ public function request(string $method, string $url, array $options = []): \Symf

return $this->client->request($method, ltrim($url, '/'), $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function get(string $url, array $options = []): \Symfony\Contracts\HttpClient\ResponseInterface
{
return $this->request('GET', $url, $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function post(string $url, array $options = []): \Symfony\Contracts\HttpClient\ResponseInterface
{
if (!\array_key_exists('body', $options) && !\array_key_exists('json', $options)) {
$options['body'] = $options;
}

return $this->request('POST', $url, $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function patch(string $url, array $options = []): \Symfony\Contracts\HttpClient\ResponseInterface
{
if (!\array_key_exists('body', $options) && !\array_key_exists('json', $options)) {
$options['body'] = $options;
}

return $this->request('PATCH', $url, $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function put(string $url, array $options = []): \Symfony\Contracts\HttpClient\ResponseInterface
{
if (!\array_key_exists('body', $options) && !\array_key_exists('json', $options)) {
$options['body'] = $options;
}

return $this->request('PUT', $url, $options);
}

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function delete(string $url, array $options = []): \Symfony\Contracts\HttpClient\ResponseInterface
{
return $this->request('DELETE', $url, $options);
}

public function __call(string $name, array $arguments)
{
return \call_user_func_array([$this->client, $name], $arguments);
}

public static function mock(string $response = '', ?int $status = 200, ?string $contentType = 'application/json', array $headers = [], string $baseUri = 'https://example.com'): object
{
$mockResponse = new MockResponse(
$response,
array_merge([
'http_code' => $status,
'content_type' => $contentType,
], $headers)
);

return new class ($mockResponse, $baseUri) {
use DecoratorTrait;

public function __construct(public MockResponse $mockResponse, $baseUri)
{
$this->client = new Client(new MockHttpClient($this->mockResponse, $baseUri));
}

public function __call(string $name, array $arguments)
{
return \call_user_func_array([$this->client, $name], $arguments);
}

#[Pure]
public function getRequestMethod()
{
return $this->mockResponse->getRequestMethod();
}

#[Pure]
public function getRequestUrl()
{
return $this->mockResponse->getRequestUrl();
}

#[Pure]
public function getRequestOptions()
{
return $this->mockResponse->getRequestOptions();
}
};
}
}
13 changes: 0 additions & 13 deletions src/Kernel/Contracts/ChainableHttpClient.php

This file was deleted.

94 changes: 0 additions & 94 deletions src/Kernel/Traits/ChainableHttpClient.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/OfficialAccount/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function getUtils(): Utils

public function createClient(): Client
{
return new Client($this->getHttpClient(), '', $this->getAccessToken());
return new Client($this->getHttpClient(), $this->getAccessToken());
}

protected function getHttpClientDefaultOptions(): array
Expand Down
2 changes: 1 addition & 1 deletion src/OpenPlatform/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected function createAuthorizerOAuthFactory(string $authorizerAppId, Officia

public function createClient(): Client
{
return new Client($this->getHttpClient(), '', $this->getComponentAccessToken());
return new Client($this->getHttpClient(), $this->getComponentAccessToken());
}

protected function getHttpClientDefaultOptions(): array
Expand Down
36 changes: 11 additions & 25 deletions src/Pay/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Application implements \EasyWeChat\Pay\Contracts\Application
use InteractWithServerRequest;

protected ?ServerInterface $server = null;
protected ?HttpClientInterface $v2Client = null;
protected ?HttpClientInterface $v3Client = null;
protected ?HttpClientInterface $client = null;
protected ?Merchant $merchant = null;

public function getMerchant(): Merchant
Expand All @@ -37,29 +36,6 @@ public function getMerchant(): Merchant
return $this->merchant;
}

public function decorateMerchantAwareHttpClient(HttpClientInterface $httpClient): MerchantAwareHttpClient
{
return new MerchantAwareHttpClient($this->getMerchant(), $httpClient, $this->config->get('http', []));
}

public function getClient(): HttpClientInterface
{
if (!$this->v3Client) {
$this->v3Client = $this->decorateMerchantAwareHttpClient($this->getHttpClient())->withUri('v3');
}

return $this->v3Client;
}

public function getV2Client(): HttpClientInterface
{
if (!$this->v2Client) {
$this->v2Client = $this->decorateMerchantAwareHttpClient($this->getHttpClient());
}

return $this->v2Client;
}

public function getUtils(): Utils
{
return new Utils($this->getMerchant());
Expand Down Expand Up @@ -100,4 +76,14 @@ public function getConfig(): ConfigInterface
{
return $this->config;
}

public function getClient(): HttpClientInterface
{
return $this->client ?? $this->client = $this->decorateMerchantAwareHttpClient($this->getHttpClient());
}

protected function decorateMerchantAwareHttpClient(HttpClientInterface $httpClient): MerchantAwareHttpClient
{
return new MerchantAwareHttpClient($this->getMerchant(), $httpClient, $this->config->get('http', []));
}
}
1 change: 0 additions & 1 deletion src/Pay/Contracts/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public function getMerchant(): Merchant;
public function getConfig(): Config;
public function getHttpClient(): HttpClientInterface;
public function getClient(): HttpClientInterface;
public function getV2Client(): HttpClientInterface;
}
5 changes: 1 addition & 4 deletions src/Pay/MerchantAwareHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace EasyWeChat\Pay;

use EasyWeChat\Kernel\Contracts\ChainableHttpClient as ChainableHttpClientInterface;
use EasyWeChat\Kernel\Support\UserAgent;
use EasyWeChat\Kernel\Traits\ChainableHttpClient;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\Uri;
use Symfony\Component\HttpClient\HttpClient as SymfonyHttpClient;
Expand All @@ -16,10 +14,9 @@
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;

class MerchantAwareHttpClient implements HttpClientInterface, ChainableHttpClientInterface
class MerchantAwareHttpClient implements HttpClientInterface
{
use HttpClientTrait;
use ChainableHttpClient;

protected HttpClientInterface $client;

Expand Down
2 changes: 1 addition & 1 deletion src/Work/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function setAccessToken(AccessTokenInterface $accessToken): static

public function createClient(): Client
{
return new Client($this->getHttpClient(), '', $this->getAccessToken());
return new Client($this->getHttpClient(), $this->getAccessToken());
}

public function getOAuth(): WeWork
Expand Down
Loading

0 comments on commit 2ab9121

Please sign in to comment.