Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

RSAP Union API - Get Members Status

See more _Miscellaneous_ Examples

Demonstrates how to use an OAuth2 access token for the RSAP Union API. Calls the endpoint to get the statuses of all union members.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.StringBuilder,
  Chilkat.PrivateKey,
  Chilkat.Cert,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  jToken: TJsonObject;
  cert: TCert;
  privKey: TPrivateKey;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  Load the access token previously obtained by this example:  RSAP Union OAuth2
  jToken := TJsonObject.Create;
  success := jToken.LoadFile('qa_data/tokens/rsapToken.json');
  if (success = False) then
    begin
      WriteLn('Failed to load access token JSON.');
      Exit;
    end;

  //  Adds the "Authorization: Bearer ACCESS_TOKEN" header.
  http.AuthToken := jToken.StringOf('access_token');

  //  For authentication, assuming both the client cert and access token are needed???
  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/certs_and_keys/union_client_certificate.crt');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  privKey := TPrivateKey.Create;
  success := privKey.LoadAnyFormatFile('qa_data/certs_and_keys/union_client_certificate.nopass.key','');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  Associate the private key with the cert.
  //  This will fail if the private key is not actually the correct one that corresponds to the public key stored within the cert.
  success := cert.SetPrivateKey(privKey);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Tell HTTP to use the cert for client TLS certificate authentication.
  success := http.SetSslClientCert(cert);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  success := http.QuickGetSb('https://api-test.rsap.ca/members/status',sbResponseBody);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := http.LastStatus;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(http.LastHeader);
      WriteLn('Failed.');
      Exit;
    end;


  http.Free;
  jToken.Free;
  cert.Free;
  privKey.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.