From 8d8b5e458062262b6cb9c8f35a79b930176edb34 Mon Sep 17 00:00:00 2001 From: "MaxiDonkey@hotmail.com" Date: Wed, 8 Jan 2025 10:33:56 +0100 Subject: [PATCH] Httpx update --- source/Anthropic.Chat.pas | 1 - source/Anthropic.Httpx.pas | 99 +++++++++++++++++++++++-- source/Anthropic.NetEncoding.Base64.pas | 32 ++------ 3 files changed, 99 insertions(+), 33 deletions(-) diff --git a/source/Anthropic.Chat.pas b/source/Anthropic.Chat.pas index 07c5b06..c7b65b8 100644 --- a/source/Anthropic.Chat.pas +++ b/source/Anthropic.Chat.pas @@ -1902,7 +1902,6 @@ class function TContentImageSource.New( if Value.ToLower.StartsWith('http') then Base64 := THttpx.LoadDataToBase64(Value, MimeType) else base64 := FileToBase64(Value, MimeType); - CheckMimeType(MimeType); Result := TContentImageSource.Create.&Type('base64').MediaType(MimeType).Data(Base64); end; diff --git a/source/Anthropic.Httpx.pas b/source/Anthropic.Httpx.pas index 4cbc67c..5187d16 100644 --- a/source/Anthropic.Httpx.pas +++ b/source/Anthropic.Httpx.pas @@ -1,5 +1,12 @@ unit Anthropic.Httpx; +{------------------------------------------------------------------------------- + + Github repository : https://github.com/MaxiDonkey/DelphiAnthropic + Visit the Github repository for the documentation and use examples + + ------------------------------------------------------------------------------} + interface uses @@ -7,10 +14,72 @@ interface System.Net.HttpClientComponent, System.NetEncoding; type + /// + /// THttpx provides utility methods for handling HTTP-related tasks such as + /// downloading data, encoding it in Base64, and retrieving MIME types. + /// THttpx = class + /// + /// Converts the content of a stream into a byte array. + /// + /// + /// The input stream to convert. + /// + /// + /// A byte array containing the data from the input stream. + /// + /// + /// Raises an exception if the input stream is null. + /// class function StreamToBytes(AStream: TStream): TBytes; - class function LoadDataToBase64(const Url: string; var MimeType: string): string; + /// + /// Loads data from the specified URL, encodes it in Base64, and retrieves its MIME type. + /// + /// + /// The URL to fetch the data from. + /// + /// + /// Outputs the MIME type of the data retrieved from the URL. + /// + /// + /// A Base64-encoded string representing the data fetched from the URL. + /// + class function LoadDataToBase64(const Url: string; var MimeType: string): string; overload; + /// + /// Loads data from the specified URL, encodes it in Base64. + /// + /// + /// The URL to fetch the data from. + /// + /// + /// A Base64-encoded string representing the data fetched from the URL. + /// + class function LoadDataToBase64(const Url: string): string; overload; + /// + /// Retrieves the MIME type of the content at the specified URL. + /// + /// + /// The URL of the content to inspect. + /// + /// + /// A string representing the MIME type of the content at the URL. + /// class function GetMimeType(const Url: string): string; + /// + /// Validates the accessibility of a specified URL by performing an HTTP HEAD request. + /// + /// + /// The URL to validate. + /// + /// + /// Raises an exception if the URL is not accessible or the server responds with a non-success status code. + /// + /// + /// This method checks the HTTP status code returned by the server for the given URL. + /// If the status code indicates an error (e.g., 4xx or 5xx), an exception is raised. + /// If the status code indicates success (e.g., 200-299), no exception is thrown. + /// + class procedure UrlCheck(const Url: string); end; implementation @@ -28,17 +97,22 @@ class function THttpx.GetMimeType(const Url: string): string; end; class function THttpx.LoadDataToBase64(const Url: string; var MimeType: string): string; +begin + MimeType := GetMimeType(Url); + Result := LoadDataToBase64(Url); +end; + +class function THttpx.LoadDataToBase64(const Url: string): string; begin var HttpClient := THTTPClient.Create; try var Response: IHTTPResponse := HttpClient.Get(Url); - var ImageBytes := StreamToBytes(Response.ContentStream); + var DataBytes := StreamToBytes(Response.ContentStream); {$IF RTLVersion >= 35.0} - Result := TNetEncoding.Base64String.EncodeBytesToString(ImageBytes); + Result := TNetEncoding.Base64String.EncodeBytesToString(DataBytes); {$ELSE} Result := TNetEncoding.Base64.EncodeBytesToString(ImageBytes); {$ENDIF} - MimeType := GetMimeType(Url); finally HttpClient.Free; end; @@ -49,7 +123,8 @@ class function THttpx.StreamToBytes(AStream: TStream): TBytes; LBytesStream: TBytesStream; begin if not Assigned(AStream) then - Exit(nil); + raise Exception.Create('StreamToBytes error: stream is null'); + LBytesStream := TBytesStream.Create; try AStream.Position := 0; @@ -61,4 +136,18 @@ class function THttpx.StreamToBytes(AStream: TStream): TBytes; end; end; +class procedure THttpx.UrlCheck(const Url: string); +begin + var HttpClient := THTTPClient.Create; + try + case (HttpClient.Head(Url) as IHTTPResponse).StatusCode of + 200..299: ; + else + raise Exception.CreateFmt('Address not found or inaccessible : %s', [Url]); + end; + finally + HttpClient.Free; + end; +end; + end. diff --git a/source/Anthropic.NetEncoding.Base64.pas b/source/Anthropic.NetEncoding.Base64.pas index f7f82ca..1fdf50d 100644 --- a/source/Anthropic.NetEncoding.Base64.pas +++ b/source/Anthropic.NetEncoding.Base64.pas @@ -44,28 +44,6 @@ interface /// function ResolveMimeType(const FileLocation: string): string; - /// - /// Validates the MIME type of the specified file to ensure it is supported. - /// - /// - /// The full path to the file whose MIME type is to be checked. - /// - /// - /// Thrown if the specified file cannot be found at the provided location, or if the MIME type - /// of the file is not supported. - /// - /// - /// This procedure verifies if the file exists at the given path and checks its MIME type - /// using the function. - /// - /// Supported MIME types include: image/png, image/jpeg, image/gif, image/webp, and application/pdf. - /// - /// - /// If the file's MIME type is not in this list, an exception is raised indicating an unsupported format. - /// - /// - procedure CheckMimeType(const MimeType: string); - /// /// Retrieves the MIME type of the specified file and returns its content as a Base64-encoded string. /// @@ -120,11 +98,11 @@ function ResolveMimeType(const FileLocation: string): string; TMimeTypes.Default.GetFileInfo(FileLocation, Result, LKind); end; -procedure CheckMimeType(const MimeType: string); -begin - if IndexStr(MimeType.ToLower, ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'application/pdf']) = -1 then - raise Exception.Create('Unsupported document format'); -end; +//procedure CheckMimeType(const MimeType: string); +//begin +//// if IndexStr(MimeType.ToLower, ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'application/pdf']) = -1 then +//// raise Exception.Create('Unsupported document format'); +//end; function FileToBase64(FileLocation : string; var MimeType: string): string; begin