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[]>
@@ -133,7 +134,7 @@ public function generateContentStream(
133134 }
134135 }
135136
136- curl_setopt ($ ch , CURLOPT_URL , "{ $ this ->baseUrl } /v1/ { $ request-> getOperation ()}" );
137+ curl_setopt ($ ch , CURLOPT_URL , $ this ->getRequestUrl ( $ request) );
137138 curl_setopt ($ ch , CURLOPT_POST , true );
138139 curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ request ));
139140 curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headerLines );
@@ -184,6 +185,19 @@ public function withBaseUrl(string $baseUrl): self
184185 return $ clone ;
185186 }
186187
188+ public function withV1BetaVersion (): self
189+ {
190+ return $ this ->withVersion (GeminiClientInterface::API_VERSION_V1_BETA );
191+ }
192+
193+ public function withVersion (string $ version ): self
194+ {
195+ $ clone = clone $ this ;
196+ $ clone ->version = $ version ;
197+
198+ return $ clone ;
199+ }
200+
187201 /**
188202 * @param array<string, string|string[]> $headers
189203 * @return self
@@ -211,6 +225,16 @@ private function getRequestHeaders(): array
211225 ];
212226 }
213227
228+ private function getRequestUrl (RequestInterface $ request ): string
229+ {
230+ return sprintf (
231+ '%s/%s/%s ' ,
232+ $ this ->baseUrl ,
233+ $ this ->version ,
234+ $ request ->getOperation (),
235+ );
236+ }
237+
214238 /**
215239 * @throws ClientExceptionInterface
216240 */
@@ -220,9 +244,11 @@ private function doRequest(RequestInterface $request): string
220244 throw new RuntimeException ('Missing client or factory for Gemini API operation ' );
221245 }
222246
223- $ uri = "{$ this ->baseUrl }/v1/ {$ request ->getOperation ()}" ;
224247 $ httpRequest = $ this ->requestFactory
225- ->createRequest ($ request ->getHttpMethod (), $ uri );
248+ ->createRequest (
249+ $ request ->getHttpMethod (),
250+ $ this ->getRequestUrl ($ request ),
251+ );
226252
227253 foreach ($ this ->getRequestHeaders () as $ name => $ value ) {
228254 $ httpRequest = $ httpRequest ->withAddedHeader ($ name , $ value );
0 commit comments