|
| 1 | +<?php |
| 2 | + |
| 3 | +/** @noinspection PhpUnusedAliasInspection */ |
| 4 | + |
| 5 | +declare(strict_types=1); |
| 6 | + |
| 7 | +/** |
| 8 | + * Copyright (c) 2023-2025 guanguans<ityaozm@gmail.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + * |
| 13 | + * @see https://github.com/guanguans/ai-commit |
| 14 | + */ |
| 15 | + |
| 16 | +namespace App\Clients; |
| 17 | + |
| 18 | +use App\Listeners\PrepareRequestListener; |
| 19 | +use Composer\InstalledVersions; |
| 20 | +use GuzzleHttp\Exception\ConnectException; |
| 21 | +use GuzzleHttp\MessageFormatter; |
| 22 | +use GuzzleHttp\Middleware; |
| 23 | +use GuzzleHttp\RequestOptions; |
| 24 | +use Illuminate\Config\Repository; |
| 25 | +use Illuminate\Http\Client\PendingRequest; |
| 26 | +use Illuminate\Support\Collection; |
| 27 | +use Illuminate\Support\Facades\Http; |
| 28 | +use Illuminate\Support\Facades\Log; |
| 29 | +use Illuminate\Support\Stringable; |
| 30 | +use Illuminate\Support\Traits\Conditionable; |
| 31 | +use Illuminate\Support\Traits\Dumpable; |
| 32 | +use Illuminate\Support\Traits\ForwardsCalls; |
| 33 | +use Illuminate\Support\Traits\Localizable; |
| 34 | +use Illuminate\Support\Traits\Tappable; |
| 35 | +use Psr\Http\Message\MessageInterface; |
| 36 | +use Psr\Http\Message\RequestInterface; |
| 37 | +use Psr\Http\Message\ResponseInterface; |
| 38 | + |
| 39 | +/** |
| 40 | + * @property \Illuminate\Support\Collection $stubCallbacks |
| 41 | + * |
| 42 | + * @mixin \Illuminate\Http\Client\PendingRequest |
| 43 | + */ |
| 44 | +abstract class AbstractClient |
| 45 | +{ |
| 46 | + use Conditionable; |
| 47 | + use Dumpable; |
| 48 | + use ForwardsCalls; |
| 49 | + use Localizable; |
| 50 | + use Tappable; |
| 51 | + protected readonly Repository $configRepository; |
| 52 | + private ?string $userAgent = null; |
| 53 | + private readonly PendingRequest $pendingRequest; |
| 54 | + |
| 55 | + public function __construct(array $config) |
| 56 | + { |
| 57 | + $this->configRepository = new Repository($this->validateConfig($config)); |
| 58 | + $this->pendingRequest = $this->extendPendingRequest($this->defaultPendingRequest()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @see \Illuminate\Http\Client\Factory::__call() |
| 63 | + * @see \Spatie\QueryBuilder\QueryBuilder::__call() |
| 64 | + * |
| 65 | + * @noinspection PhpMixedReturnTypeCanBeReducedInspection |
| 66 | + * |
| 67 | + * @return mixed|PendingRequest|static |
| 68 | + */ |
| 69 | + public function __call(string $name, array $arguments): mixed |
| 70 | + { |
| 71 | + $result = $this->forwardCallTo($this->pendingRequest(), $name, $arguments); |
| 72 | + |
| 73 | + if ($result === $this->pendingRequest) { |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + return $result; |
| 78 | + } |
| 79 | + |
| 80 | + public static function sanitizeData(string $data): string |
| 81 | + { |
| 82 | + return (string) str($data)->whenStartsWith( |
| 83 | + $prefix = 'data: ', |
| 84 | + static fn (Stringable $data): Stringable => $data->after($prefix) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function ddPendingRequest(mixed ...$args): static |
| 89 | + { |
| 90 | + $this->pendingRequest()->dd(...$args); |
| 91 | + |
| 92 | + return $this; |
| 93 | + } |
| 94 | + |
| 95 | + public function dumpPendingRequest(mixed ...$args): static |
| 96 | + { |
| 97 | + $this->pendingRequest()->dump(...$args); |
| 98 | + |
| 99 | + return $this; |
| 100 | + } |
| 101 | + |
| 102 | + public function clonePendingRequest(?callable $callback = null): PendingRequest |
| 103 | + { |
| 104 | + return $this->pendingRequest($callback, true); |
| 105 | + } |
| 106 | + |
| 107 | + public function pendingRequest(?callable $callback = null, bool $clone = false): PendingRequest |
| 108 | + { |
| 109 | + return tap( |
| 110 | + tap( |
| 111 | + $clone ? clone $this->pendingRequest : $this->pendingRequest, |
| 112 | + function (PendingRequest $pendingRequest): void { |
| 113 | + /** @see \Illuminate\Http\Client\Factory::createPendingRequest() */ |
| 114 | + $pendingRequest |
| 115 | + ->stub((fn (): Collection => $this->stubCallbacks)->call(Http::getFacadeRoot())) |
| 116 | + ->preventStrayRequests(Http::preventingStrayRequests()); |
| 117 | + } |
| 118 | + ), |
| 119 | + $callback ?? static fn (): null => null |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + abstract protected function configRules(): array; |
| 124 | + |
| 125 | + abstract protected function extendPendingRequest(PendingRequest $pendingRequest): PendingRequest; |
| 126 | + |
| 127 | + /** |
| 128 | + * @throws \Illuminate\Validation\ValidationException |
| 129 | + */ |
| 130 | + protected function validate(array $data, array $rules, array $messages = [], array $customAttributes = []): array |
| 131 | + { |
| 132 | + return validator($data, $rules, $messages, $customAttributes)->validate(); |
| 133 | + } |
| 134 | + |
| 135 | + protected function requestId(): ?string |
| 136 | + { |
| 137 | + /** @noinspection PhpUndefinedConstantInspection */ |
| 138 | + return \defined('TRACE_ID') ? TRACE_ID : null; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @noinspection OffsetOperationsInspection |
| 143 | + * |
| 144 | + * @return array<string, scalar> |
| 145 | + */ |
| 146 | + protected function userAgentItems(): array |
| 147 | + { |
| 148 | + return [ |
| 149 | + 'ai-commit' => str(config('app.version'))->whenStartsWith('v', static fn (Stringable $version): Stringable => $version->replaceFirst('v', '')), |
| 150 | + 'laravel' => InstalledVersions::getPrettyVersion('laravel/framework'), |
| 151 | + 'guzzle' => InstalledVersions::getPrettyVersion('guzzlehttp/guzzle'), |
| 152 | + 'curl' => (curl_version() ?: ['version' => 'unknown'])['version'], |
| 153 | + 'PHP' => \PHP_VERSION, |
| 154 | + \PHP_OS => php_uname('r'), |
| 155 | + ]; |
| 156 | + } |
| 157 | + |
| 158 | + protected function configMessages(): array |
| 159 | + { |
| 160 | + return []; |
| 161 | + } |
| 162 | + |
| 163 | + protected function configAttributes(): array |
| 164 | + { |
| 165 | + return []; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param int $retries 重试次数 |
| 170 | + * @param int $baseIntervalMs 基础间隔(毫秒) |
| 171 | + * |
| 172 | + * @see \GuzzleHttp\RetryMiddleware::exponentialDelay() |
| 173 | + * @see \retry() |
| 174 | + */ |
| 175 | + protected function fibonacciRetryIntervals(int $retries, int $baseIntervalMs = 1000): array |
| 176 | + { |
| 177 | + $intervals = []; |
| 178 | + $prev = 0; |
| 179 | + $curr = 1; |
| 180 | + |
| 181 | + for ($index = 0; $index < $retries; ++$index) { |
| 182 | + $intervals[] = $curr * $baseIntervalMs; |
| 183 | + [$prev, $curr] = [$curr, $prev + $curr]; |
| 184 | + } |
| 185 | + |
| 186 | + return $intervals; |
| 187 | + } |
| 188 | + |
| 189 | + private function defaultPendingRequest(): PendingRequest |
| 190 | + { |
| 191 | + return Http::baseUrl($this->configRepository->get('base_url')) |
| 192 | + ->when( |
| 193 | + $this->getUserAgent(), |
| 194 | + static fn ( |
| 195 | + PendingRequest $pendingRequest, |
| 196 | + string $userAgent |
| 197 | + ) => $pendingRequest->withUserAgent($userAgent) |
| 198 | + ) |
| 199 | + ->withOptions((array) config('ai-commit.http_options')) |
| 200 | + ->withOptions($this->configRepository->get('http_options')) |
| 201 | + ->retry( |
| 202 | + times: $this->configRepository->get('retry.times'), |
| 203 | + sleepMilliseconds: $this->configRepository->get('retry.sleep'), |
| 204 | + when: $this->configRepository->get('retry.when'), |
| 205 | + throw: $this->configRepository->get('retry.throw') |
| 206 | + ) |
| 207 | + // ->when( |
| 208 | + // $this->requestId(), |
| 209 | + // static fn ( |
| 210 | + // PendingRequest $pendingRequest, |
| 211 | + // string $requestId |
| 212 | + // ) => $pendingRequest->withHeader(PrepareRequestListener::X_REQUEST_ID, $requestId) |
| 213 | + // ) |
| 214 | + ->withMiddleware(Middleware::mapRequest( |
| 215 | + static fn (RequestInterface $request): MessageInterface => $request->withHeader('X-Date-Time', now()->toDateTimeString('m')) |
| 216 | + )) |
| 217 | + ->withMiddleware($this->makeLoggerMiddleware($this->configRepository->get('logger'))) |
| 218 | + ->withMiddleware(Middleware::mapResponse( |
| 219 | + static fn (ResponseInterface $response): MessageInterface => $response->withHeader('X-Date-Time', now()->toDateTimeString('m')) |
| 220 | + )); |
| 221 | + // ->when( |
| 222 | + // $this->requestId(), |
| 223 | + // static fn (PendingRequest $pendingRequest, string $requestId) => $pendingRequest->withMiddleware( |
| 224 | + // Middleware::mapResponse( |
| 225 | + // static fn (ResponseInterface $response) => $response->withHeader(PrepareRequestListener::X_REQUEST_ID, $requestId) |
| 226 | + // ) |
| 227 | + // ) |
| 228 | + // ) |
| 229 | + } |
| 230 | + |
| 231 | + private function validateConfig(array $config): array |
| 232 | + { |
| 233 | + return $this->validate( |
| 234 | + array_replace_recursive($this->defaultConfig(), $config), |
| 235 | + $this->configRules() + $this->defaultConfigRules(), |
| 236 | + $this->configMessages(), |
| 237 | + $this->configAttributes() |
| 238 | + ); |
| 239 | + } |
| 240 | + |
| 241 | + private function defaultConfig(): array |
| 242 | + { |
| 243 | + return [ |
| 244 | + // 'base_url' => null, |
| 245 | + 'logger' => 'null', |
| 246 | + 'http_options' => [ |
| 247 | + // RequestOptions::CONNECT_TIMEOUT => 10, |
| 248 | + // RequestOptions::TIMEOUT => 30, |
| 249 | + ], |
| 250 | + /** |
| 251 | + * @see PendingRequest::retry() |
| 252 | + * @see PendingRequest::$tries |
| 253 | + */ |
| 254 | + 'retry' => [ |
| 255 | + 'times' => $this->fibonacciRetryIntervals(1), |
| 256 | + 'sleep' => 1000, |
| 257 | + // 'when' => static fn (\Throwable $throwable): bool => $throwable instanceof ConnectException, |
| 258 | + 'when' => null, |
| 259 | + 'throw' => true, |
| 260 | + ], |
| 261 | + ]; |
| 262 | + } |
| 263 | + |
| 264 | + private function defaultConfigRules(): array |
| 265 | + { |
| 266 | + return [ |
| 267 | + 'base_url' => 'required|string', |
| 268 | + 'logger' => 'nullable|string', |
| 269 | + 'http_options' => 'array', |
| 270 | + 'retry' => 'array', |
| 271 | + ]; |
| 272 | + } |
| 273 | + |
| 274 | + private function getUserAgent(): string |
| 275 | + { |
| 276 | + return $this->userAgent ??= collect($this->userAgentItems()) |
| 277 | + ->map(static fn (mixed $value, string $name): string => "$name/$value") |
| 278 | + ->implode(' '); |
| 279 | + } |
| 280 | + |
| 281 | + private function makeLoggerMiddleware(?string $logger = null): callable |
| 282 | + { |
| 283 | + return Middleware::log(Log::channel($logger), new MessageFormatter(MessageFormatter::DEBUG)); |
| 284 | + } |
| 285 | +} |
0 commit comments