Skip to content

Commit

Permalink
新增http.options_by_regexp配置 (#2761)
Browse files Browse the repository at this point in the history
我们的服务器目前没有固定IP(函数计算), 所以获取AccessTokens需要使用有固定IP的代理,  但是我又不想所有的请求都使用这个代理, 所以需要ScopingHttpClient的能力
  • Loading branch information
hughcube committed Nov 21, 2023
1 parent 2fd9ea7 commit 82dc365
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Kernel/Traits/InteractWithHttpClient.php
Expand Up @@ -5,10 +5,11 @@
namespace EasyWeChat\Kernel\Traits;

use EasyWeChat\Kernel\HttpClient\RequestUtil;
use EasyWeChat\Kernel\Support\Arr;
use Psr\Log\LoggerAwareInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

use function property_exists;

trait InteractWithHttpClient
Expand Down Expand Up @@ -39,7 +40,18 @@ public function setHttpClient(HttpClientInterface $httpClient): static

protected function createHttpClient(): HttpClientInterface
{
return HttpClient::create(RequestUtil::formatDefaultOptions($this->getHttpClientDefaultOptions()));
$options = $this->getHttpClientDefaultOptions();

$optionsByRegexp = Arr::get($options, 'options_by_regexp', []);
unset($options['options_by_regexp']);

$client = HttpClient::create(RequestUtil::formatDefaultOptions($options));

if (! empty($optionsByRegexp)) {
$client = new ScopingHttpClient($client, $optionsByRegexp);
}

return $client;
}

/**
Expand Down

1 comment on commit 82dc365

@vercel
Copy link

@vercel vercel bot commented on 82dc365 Nov 21, 2023

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-git-6x-overtrue.vercel.app
easywechat-overtrue.vercel.app
easywechat.vercel.app

Please sign in to comment.