-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加自定义ScopingHttpClient降低使用复杂度 (#2766)
- Loading branch information
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EasyWeChat\Kernel\HttpClient; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpClient\HttpClientTrait; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
use Symfony\Contracts\HttpClient\ResponseStreamInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
class ScopingHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface | ||
{ | ||
use HttpClientTrait; | ||
|
||
private HttpClientInterface $client; | ||
private array $defaultOptionsByRegexp; | ||
|
||
public function __construct(HttpClientInterface $client, array $defaultOptionsByRegexp) | ||
{ | ||
$this->client = $client; | ||
$this->defaultOptionsByRegexp = $defaultOptionsByRegexp; | ||
} | ||
|
||
public function request(string $method, string $url, array $options = []): ResponseInterface | ||
{ | ||
foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) { | ||
if (preg_match($regexp, $url)) { | ||
$options = self::mergeDefaultOptions($options, $defaultOptions, true); | ||
break; | ||
} | ||
} | ||
|
||
return $this->client->request($method, $url, $options); | ||
} | ||
|
||
public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface | ||
{ | ||
return $this->client->stream($responses, $timeout); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function reset() | ||
{ | ||
if ($this->client instanceof ResetInterface) { | ||
$this->client->reset(); | ||
} | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger): void | ||
{ | ||
if ($this->client instanceof LoggerAwareInterface) { | ||
$this->client->setLogger($logger); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
557409d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
easywechat – ./
easywechat.vercel.app
easywechat-overtrue.vercel.app
easywechat-git-6x-overtrue.vercel.app