4040class Client implements GeminiClientInterface
4141{
4242 private string $ baseUrl = 'https://generativelanguage.googleapis.com ' ;
43+ private string $ version = GeminiClientInterface::API_VERSION_V1 ;
4344
4445 /**
4546 * @var array<string, string|string[]>
@@ -163,7 +164,7 @@ public function generateContentStream(
163164 }
164165 }
165166
166- curl_setopt ($ ch , CURLOPT_URL , "{ $ this ->baseUrl } /v1/ { $ request-> getOperation ()}" );
167+ curl_setopt ($ ch , CURLOPT_URL , $ this ->getRequestUrl ( $ request) );
167168 curl_setopt ($ ch , CURLOPT_POST , true );
168169 curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ request ));
169170 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headerLines );
@@ -214,6 +215,19 @@ public function withBaseUrl(string $baseUrl): self
214215 return $ clone ;
215216 }
216217
218+ public function withV1BetaVersion (): self
219+ {
220+ return $ this ->withVersion (GeminiClientInterface::API_VERSION_V1_BETA );
221+ }
222+
223+ public function withVersion (string $ version ): self
224+ {
225+ $ clone = clone $ this ;
226+ $ clone ->version = $ version ;
227+
228+ return $ clone ;
229+ }
230+
217231 /**
218232 * @param array<string, string|string[]> $headers
219233 * @return self
@@ -241,6 +255,16 @@ private function getRequestHeaders(): array
241255 ];
242256 }
243257
258+ private function getRequestUrl (RequestInterface $ request ): string
259+ {
260+ return sprintf (
261+ '%s/%s/%s ' ,
262+ $ this ->baseUrl ,
263+ $ this ->version ,
264+ $ request ->getOperation (),
265+ );
266+ }
267+
244268 /**
245269 * @throws ClientExceptionInterface
246270 */
@@ -250,9 +274,11 @@ private function doRequest(RequestInterface $request): string
250274 throw new RuntimeException ('Missing client or factory for Gemini API operation ' );
251275 }
252276
253- $ uri = "{$ this ->baseUrl }/v1/ {$ request ->getOperation ()}" ;
254277 $ httpRequest = $ this ->requestFactory
255- ->createRequest ($ request ->getHttpMethod (), $ uri );
278+ ->createRequest (
279+ $ request ->getHttpMethod (),
280+ $ this ->getRequestUrl ($ request ),
281+ );
256282
257283 foreach ($ this ->getRequestHeaders () as $ name => $ value ) {
258284 $ httpRequest = $ httpRequest ->withAddedHeader ($ name , $ value );
0 commit comments