@@ -243,7 +243,7 @@ TApiDeserializer = class(TApiHttpHandler)
243243 // / </remarks>
244244 TGenAIAPI = class (TApiDeserializer)
245245 private
246- function MockJsonResponse (const FieldName: string; Response: TStringStream): string;
246+ function MockJsonResponse (const FieldName: string; Response: TStringStream): string; overload;
247247 function MockJsonFile (const FieldName: string; Response: TStringStream): string;
248248 public
249249 // / <summary>
@@ -362,6 +362,31 @@ TGenAIAPI = class(TApiDeserializer)
362362 // / </exception>
363363 function Post <TParams: TJSONParam>(const Endpoint: string; ParamProc: TProc<TParams>; Response: TStringStream; Event: TReceiveDataCallback): Boolean; overload;
364364 // / <summary>
365+ // / Sends a POST request with parameters and streams the response.
366+ // / </summary>
367+ // / <typeparam name="TParams">
368+ // / The type of the parameters object for the request.
369+ // / </typeparam>
370+ // / <param name="Endpoint">
371+ // / The relative endpoint to send the POST request to.
372+ // / </param>
373+ // / <param name="ParamProc">
374+ // / A callback procedure to configure the request parameters.
375+ // / </param>
376+ // / <param name="Response">
377+ // / A string stream where the response will be written.
378+ // / </param>
379+ // / <param name="Event">
380+ // / A callback procedure for handling the received data during streaming.
381+ // / </param>
382+ // / <returns>
383+ // / A boolean value indicating whether the request was successful.
384+ // / </returns>
385+ // / <exception cref="GenAIInvalidResponseError">
386+ // / Raised if the response cannot be deserialized or is non-compliant.
387+ // / </exception>
388+ function Post <TParams: TJSONParam>(const Endpoint: string; ParamProc: TProc<TParams>; Response: TStream; Event: TReceiveDataCallback): Boolean; overload;
389+ // / <summary>
365390 // / Sends a POST request with parameters and returns a strongly typed object.
366391 // / </summary>
367392 // / <typeparam name="TResult">
@@ -488,6 +513,38 @@ constructor TGenAIAPI.Create(const AAPIKey: string);
488513 APIKey := AAPIKey;
489514end ;
490515
516+ function TGenAIAPI.Post <TParams>(const Endpoint: string;
517+ ParamProc: TProc<TParams>; Response: TStream;
518+ Event: TReceiveDataCallback): Boolean;
519+ var
520+ Params: TParams;
521+ begin
522+ Monitoring.Inc;
523+ Params := TParams.Create;
524+ try
525+ if Assigned(ParamProc) then
526+ ParamProc(Params);
527+ var Code := HttpClient.Post(BuildUrl(Endpoint), Params.JSON, Response, BuildJsonHeaders, Event);
528+ Result := (Code > 200 ) and (Code < 299 );
529+ case Code of
530+ 200 ..299 :
531+ Result := True;
532+ else
533+ begin
534+ Response.Position := 0 ;
535+ var ErrBytes: TBytes;
536+ SetLength(ErrBytes, Response.Size);
537+ Response.ReadBuffer(ErrBytes, Length(ErrBytes));
538+ DeserializeErrorData(Code, TEncoding.UTF8.GetString(ErrBytes));
539+ end ;
540+ end ;
541+ finally
542+ Params.Free;
543+ ResetCustomHeader;
544+ Monitoring.Dec;
545+ end ;
546+ end ;
547+
491548function TGenAIAPI.Post <TResult, TParams>(const Endpoint: string; ParamProc: TProc<TParams>;
492549 const RawByteFieldName: string): TResult;
493550begin
0 commit comments