Skip to content

Commit

Permalink
Httpx update
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxiDonkey@hotmail.com authored and MaxiDonkey@hotmail.com committed Jan 8, 2025
1 parent 92c652b commit 8d8b5e4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 33 deletions.
1 change: 0 additions & 1 deletion source/Anthropic.Chat.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
99 changes: 94 additions & 5 deletions source/Anthropic.Httpx.pas
Original file line number Diff line number Diff line change
@@ -1,16 +1,85 @@
unit Anthropic.Httpx;

{-------------------------------------------------------------------------------
Github repository : https://github.com/MaxiDonkey/DelphiAnthropic
Visit the Github repository for the documentation and use examples
------------------------------------------------------------------------------}

interface

uses
System.SysUtils, System.Classes, System.Net.URLClient, System.Net.HttpClient,
System.Net.HttpClientComponent, System.NetEncoding;

type
/// <summary>
/// THttpx provides utility methods for handling HTTP-related tasks such as
/// downloading data, encoding it in Base64, and retrieving MIME types.
/// </summary>
THttpx = class
/// <summary>
/// Converts the content of a stream into a byte array.
/// </summary>
/// <param name="AStream">
/// The input stream to convert.
/// </param>
/// <returns>
/// A byte array containing the data from the input stream.
/// </returns>
/// <exception cref="Exception">
/// Raises an exception if the input stream is null.
/// </exception>
class function StreamToBytes(AStream: TStream): TBytes;
class function LoadDataToBase64(const Url: string; var MimeType: string): string;
/// <summary>
/// Loads data from the specified URL, encodes it in Base64, and retrieves its MIME type.
/// </summary>
/// <param name="Url">
/// The URL to fetch the data from.
/// </param>
/// <param name="MimeType">
/// Outputs the MIME type of the data retrieved from the URL.
/// </param>
/// <returns>
/// A Base64-encoded string representing the data fetched from the URL.
/// </returns>
class function LoadDataToBase64(const Url: string; var MimeType: string): string; overload;
/// <summary>
/// Loads data from the specified URL, encodes it in Base64.
/// </summary>
/// <param name="Url">
/// The URL to fetch the data from.
/// </param>
/// <returns>
/// A Base64-encoded string representing the data fetched from the URL.
/// </returns>
class function LoadDataToBase64(const Url: string): string; overload;
/// <summary>
/// Retrieves the MIME type of the content at the specified URL.
/// </summary>
/// <param name="Url">
/// The URL of the content to inspect.
/// </param>
/// <returns>
/// A string representing the MIME type of the content at the URL.
/// </returns>
class function GetMimeType(const Url: string): string;
/// <summary>
/// Validates the accessibility of a specified URL by performing an HTTP HEAD request.
/// </summary>
/// <param name="Url">
/// The URL to validate.
/// </param>
/// <exception cref="Exception">
/// Raises an exception if the URL is not accessible or the server responds with a non-success status code.
/// </exception>
/// <remarks>
/// 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.
/// </remarks>
class procedure UrlCheck(const Url: string);
end;

implementation
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
32 changes: 5 additions & 27 deletions source/Anthropic.NetEncoding.Base64.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,6 @@ interface
/// </remarks>
function ResolveMimeType(const FileLocation: string): string;

/// <summary>
/// Validates the MIME type of the specified file to ensure it is supported.
/// </summary>
/// <param name="FileLocation">
/// The full path to the file whose MIME type is to be checked.
/// </param>
/// <exception cref="Exception">
/// Thrown if the specified file cannot be found at the provided location, or if the MIME type
/// of the file is not supported.
/// </exception>
/// <remarks>
/// This procedure verifies if the file exists at the given path and checks its MIME type
/// using the <see cref="ResolveMimeType"/> function.
/// <para>
/// Supported MIME types include: image/png, image/jpeg, image/gif, image/webp, and application/pdf.
/// </para>
/// <para>
/// If the file's MIME type is not in this list, an exception is raised indicating an unsupported format.
/// </para>
/// </remarks>
procedure CheckMimeType(const MimeType: string);

/// <summary>
/// Retrieves the MIME type of the specified file and returns its content as a Base64-encoded string.
/// </summary>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d8b5e4

Please sign in to comment.