unit podman;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpjson, jsonparser, fphttpclientnext;
type
TResponse = record
Content: string;
Code: Integer;
end;
TRequestOptions = record
Method: string;
BaseURL: string;
Path: string;
UnixSocketPath: string;
end;
TRequestResultHandler = procedure (Sender: TObject; response: TResponse) of object;
TRequestThread = Class(TObject)
FResponse: TResponse;
FOptions: TRequestOptions;
FOnResult: TRequestResultHandler;
FOnError : TRequestResultHandler;
constructor Create(options: TRequestOptions; OnResult: TRequestResultHandler; OnError : TRequestResultHandler);
Procedure DoneThread(Sender : TObject);
Procedure DoThread;
Procedure Run;
end;
{TFPPodmanAPISystemHostInfo}
TFPPodmanAPISystemHostInfo = record
arch : string;
buildahVersion : string;
cgroupManager: string;
cgroupVersion: string;
cgroupControllers: array of string;
end;
{TFPPodmanAPISystemInfo}
TFPPodmanAPISystemInfo = record
host : TFPPodmanAPISystemHostInfo;
end;
{ TFPPodmanAPIClient }
TFPPodmanAPIClient = class(TComponent)
public
const Version: string = '1.0.0';
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetSystemInfo(OnResult: TRequestResultHandler; OnError : TRequestResultHandler; Scope: TObject);
end;
TFPPodmanAPIDomain = class(TObject)
public
class function CoerceSystemInfo(response: TResponse): TFPPodmanAPISystemInfo; static;
end;
implementation
class Function TFPPodmanAPIDomain.CoerceSystemInfo(response: TResponse): TFPPodmanAPISystemInfo;
var ResponseObject : TJSONObject;
Coerced: TFPPodmanAPISystemInfo;
begin
try
ResponseObject := GetJSON(response.Content) as TJSONObject;
Coerced.Host.arch := ResponseObject.FindPath('host.arch').AsString;
Coerced.Host.buildahVersion := ResponseObject.FindPath('host.buildahVersion').AsString;
Coerced.Host.cgroupManager := ResponseObject.FindPath('host.cgroupManager').AsString;
Coerced.Host.cgroupVersion := ResponseObject.FindPath('host.cgroupVersion').AsString;
finally
end;
Result := Coerced;
end;
constructor TRequestThread.Create(Options: TRequestOptions; OnResult: TRequestResultHandler; OnError : TRequestResultHandler);
begin
Self.FOptions := Options;
Self.FOptions.UnixSocketPath := '/tmp/podman.sock';
Self.FOptions.BaseURL := 'http://d/v3.0.0/libpod';
Self.FOnResult := OnResult;
Self.FOnError := OnError;
end;
procedure TRequestThread.DoneThread(Sender : TObject);
begin
Self.FOnResult(Sender, Self.FResponse);
end;
procedure TRequestThread.DoThread;
var client : TFPHTTPClient;
RequestURL: string;
begin
RequestURL := Self.FOptions.BaseURL + Self.FOptions.Path;
client := TFPHTTPClient.Create(nil);
client.UnixSocketPath := Self.FOptions.UnixSocketPath;
case uppercase(Self.FOptions.Method) of
'GET': Self.FResponse.Content := client.Get(RequestURL);
end;
Self.FResponse.Code := client.ResponseStatusCode;
end;
procedure TRequestThread.Run;
begin
TThread.ExecuteInThread(@DoThread, @DoneThread);
end;
constructor TFPPodmanAPIClient.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TFPPodmanAPIClient.Destroy;
begin
inherited Destroy;
end;
procedure TFPPodmanAPIClient.GetSystemInfo(OnResult: TRequestResultHandler; OnError: TRequestResultHandler; Scope: TObject);
var
request: TRequestThread;
options: TRequestOptions;
begin
options.Method := 'GET';
options.Path := '/info';
request := TRequestThread.Create(
options,
OnResult,
OnError
);
request.Run;
end;
end.