Skip to content

Commit 3214974

Browse files
author
ityaozm@gmail.com
committed
refactor(clients): Replace FoundationSDK with AbstractClient in clients
- Replace instances of FoundationSDK with AbstractClient in client classes - Update usage of pending request methods to align with the new structure - Remove commented validation configurations from clients - Ensure all clients extend AbstractClient for better code consistency and clarity - Streamline function signatures for pending requests and configuration rules
1 parent 77db244 commit 3214974

9 files changed

Lines changed: 54 additions & 335 deletions

File tree

app/Clients/AbstractClient.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
abstract class AbstractClient
4545
{
46-
use Conditionable;
46+
// use Conditionable;
4747
use Dumpable;
4848
use ForwardsCalls;
4949
use Localizable;
@@ -147,7 +147,7 @@ protected function userAgentItems(): array
147147
{
148148
return [
149149
'ai-commit' => str(config('app.version'))->whenStartsWith('v', static fn (Stringable $version): Stringable => $version->replaceFirst('v', '')),
150-
'laravel' => InstalledVersions::getPrettyVersion('laravel/framework'),
150+
'laravel' => InstalledVersions::getPrettyVersion('illuminate/support'),
151151
'guzzle' => InstalledVersions::getPrettyVersion('guzzlehttp/guzzle'),
152152
'curl' => (curl_version() ?: ['version' => 'unknown'])['version'],
153153
'PHP' => \PHP_VERSION,
@@ -188,7 +188,13 @@ protected function fibonacciRetryIntervals(int $retries, int $baseIntervalMs = 1
188188

189189
private function defaultPendingRequest(): PendingRequest
190190
{
191-
return Http::baseUrl($this->configRepository->get('base_url'))
191+
return Http::when(
192+
$this->configRepository->get('base_url'),
193+
static fn (
194+
PendingRequest $pendingRequest,
195+
string $baseUrl
196+
) => $pendingRequest->baseUrl($baseUrl)
197+
)
192198
->when(
193199
$this->getUserAgent(),
194200
static fn (
@@ -214,7 +220,7 @@ private function defaultPendingRequest(): PendingRequest
214220
->withMiddleware(Middleware::mapRequest(
215221
static fn (RequestInterface $request): MessageInterface => $request->withHeader('X-Date-Time', now()->toDateTimeString('m'))
216222
))
217-
->withMiddleware($this->makeLoggerMiddleware($this->configRepository->get('logger')))
223+
// ->withMiddleware($this->makeLoggerMiddleware($this->configRepository->get('logger')))
218224
->withMiddleware(Middleware::mapResponse(
219225
static fn (ResponseInterface $response): MessageInterface => $response->withHeader('X-Date-Time', now()->toDateTimeString('m'))
220226
));
@@ -264,7 +270,7 @@ private function defaultConfig(): array
264270
private function defaultConfigRules(): array
265271
{
266272
return [
267-
'base_url' => 'required|string',
273+
'base_url' => 'string',
268274
'logger' => 'nullable|string',
269275
'http_options' => 'array',
270276
'retry' => 'array',

app/Clients/Ernie.php

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* @see https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu
2525
*/
26-
final class Ernie extends FoundationSDK
26+
final class Ernie extends AbstractClient
2727
{
2828
private static ?string $accessToken = null;
2929

@@ -82,72 +82,29 @@ public function ernieBotTurbo(array $parameters, ?callable $writer = null): Resp
8282
*/
8383
public function oauthToken(): Response
8484
{
85-
return $this->cloneDefaultPendingRequest()
85+
return $this
8686
->get(
8787
'oauth/2.0/token',
8888
[
8989
'grant_type' => 'client_credentials',
90-
'client_id' => $this->config['api_key'],
91-
'client_secret' => $this->config['secret_key'],
90+
'client_id' => $this->configRepository->get('api_key'),
91+
'client_secret' => $this->configRepository->get('secret_key'),
9292
]
9393
)
9494
->throw();
9595
}
9696

97-
/**
98-
* {@inheritDoc}
99-
*
100-
* @throws BindingResolutionException
101-
*/
102-
protected function validateConfig(array $config): array
97+
protected function configRules(): array
10398
{
104-
return array_replace_recursive(
105-
[
106-
'http_options' => [
107-
// \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 30,
108-
// \GuzzleHttp\RequestOptions::TIMEOUT => 180,
109-
],
110-
'retry' => [
111-
// 'times' => 1,
112-
// 'sleep' => 1000,
113-
// 'when' => static function (\Exception $exception): bool {
114-
// return $exception instanceof \Illuminate\Http\Client\ConnectionException;
115-
// },
116-
// // 'throw' => true,
117-
],
118-
'base_url' => 'https://aip.baidubce.com',
119-
],
120-
validate($config, [
121-
'http_options' => 'array',
122-
'retry' => 'array',
123-
'retry.times' => 'integer',
124-
'retry.sleep' => 'integer',
125-
'retry.when' => 'nullable',
126-
// 'retry.throw' => 'bool',
127-
'base_url' => 'string',
128-
'api_key' => 'required|string',
129-
'secret_key' => 'required|string',
130-
])
131-
);
99+
return [
100+
'api_key' => 'required|string',
101+
'secret_key' => 'required|string',
102+
];
132103
}
133104

134-
/**
135-
* {@inheritDoc}
136-
*/
137-
protected function buildDefaultPendingRequest(array $config): PendingRequest
105+
protected function extendPendingRequest(PendingRequest $pendingRequest): PendingRequest
138106
{
139-
return parent::buildDefaultPendingRequest($config)
140-
->baseUrl($config['base_url'])
141-
->asJson()
142-
// ->dump()
143-
// ->throw()
144-
// ->retry(
145-
// $config['retry']['times'],
146-
// $config['retry']['sleep'],
147-
// $config['retry']['when']
148-
// // $config['retry']['throw']
149-
// )
150-
->withOptions($config['http_options']);
107+
return $pendingRequest->baseUrl($this->configRepository->get('base_url', 'https://aip.baidubce.com'));
151108
}
152109

153110
/**
@@ -170,7 +127,6 @@ private function completion(
170127
array $customAttributes = []
171128
): Response {
172129
$response = $this
173-
->cloneDefaultPendingRequest()
174130
->when(
175131
($parameters['stream'] ?? false) && \is_callable($writer),
176132
static function (PendingRequest $pendingRequest) use (&$rowData, $writer): PendingRequest {

app/Clients/FoundationSDK.php

Lines changed: 0 additions & 157 deletions
This file was deleted.

app/Clients/Moonshot.php

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* @see https://platform.moonshot.cn/docs/api-reference
2525
*/
26-
final class Moonshot extends FoundationSDK
26+
final class Moonshot extends AbstractClient
2727
{
2828
/**
2929
* ```ok
@@ -64,7 +64,6 @@ final class Moonshot extends FoundationSDK
6464
public function chatCompletions(array $parameters, ?callable $writer = null): Response
6565
{
6666
$response = $this
67-
->cloneDefaultPendingRequest()
6867
->when(
6968
($parameters['stream'] ?? false) && \is_callable($writer),
7069
static function (PendingRequest $pendingRequest) use (&$rowData, $writer): PendingRequest {
@@ -118,62 +117,20 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
118117
*/
119118
public function models(): Response
120119
{
121-
return $this->cloneDefaultPendingRequest()->get('models')->throw();
120+
return $this->get('models')->throw();
122121
}
123122

124-
/**
125-
* {@inheritDoc}
126-
*
127-
* @throws BindingResolutionException
128-
*/
129-
protected function validateConfig(array $config): array
123+
protected function configRules(): array
130124
{
131-
return array_replace_recursive(
132-
[
133-
'http_options' => [
134-
// \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 30,
135-
// \GuzzleHttp\RequestOptions::TIMEOUT => 180,
136-
],
137-
'retry' => [
138-
// 'times' => 1,
139-
// 'sleep' => 1000,
140-
// 'when' => static function (\Exception $exception): bool {
141-
// return $exception instanceof \Illuminate\Http\Client\ConnectionException;
142-
// },
143-
// // 'throw' => true,
144-
],
145-
'base_url' => 'https://api.moonshot.cn/v1',
146-
],
147-
validate($config, [
148-
'http_options' => 'array',
149-
'retry' => 'array',
150-
'retry.times' => 'integer',
151-
'retry.sleep' => 'integer',
152-
'retry.when' => 'nullable',
153-
// 'retry.throw' => 'bool',
154-
'base_url' => 'string',
155-
'api_key' => 'required|string',
156-
])
157-
);
125+
return [
126+
'api_key' => 'required|string',
127+
];
158128
}
159129

160-
/**
161-
* {@inheritDoc}
162-
*/
163-
protected function buildDefaultPendingRequest(array $config): PendingRequest
130+
protected function extendPendingRequest(PendingRequest $pendingRequest): PendingRequest
164131
{
165-
return parent::buildDefaultPendingRequest($config)
166-
->baseUrl($config['base_url'])
167-
->asJson()
168-
->withToken($config['api_key'])
169-
// ->dump()
170-
// ->throw()
171-
// ->retry(
172-
// $config['retry']['times'],
173-
// $config['retry']['sleep'],
174-
// $config['retry']['when']
175-
// // $config['retry']['throw']
176-
// )
177-
->withOptions($config['http_options']);
132+
return $pendingRequest
133+
->baseUrl($this->configRepository->get('base_url', 'https://api.moonshot.cn/v1'))
134+
->withToken($this->configRepository->get('api_key'));
178135
}
179136
}

0 commit comments

Comments
 (0)