Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.6.0+

Azure Key Vault: Cache Tokens and Retry a Read with Backoff

See more Azure Key Vault Examples

Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.

A single-caller example using client_id, client_secret, tenant_id, vault_name and cert_name. Reuses a valid private token cache only for the configured tenant/client and Key Vault resource, renews near expiry, and allows one token renewal after 401. The certificate GET gets at most five attempts. Honors numeric or HTTP-date Retry-After, adds jitter, and stops rather than shortening waits above 30 seconds. Retries only selected transient HTTP statuses; transport failures stop. Does not replay mutations or retry rejected token requests. Protect/cache tokens per identity and tenant; concurrent applications need synchronization and atomic cache replacement.

Microsoft reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
validate1: HCkStringBuilder;
alphabet1: HCkStringBuilder;
charIndex1: Integer;
vaultName: HCkStringBuilder;
letters: HCkStringBuilder;
origin: HCkStringBuilder;
stringMatch2: Boolean;
validate2: HCkStringBuilder;
alphabet2: HCkStringBuilder;
charIndex2: Integer;
stringMatch3: Boolean;
stringMatch4: Boolean;
stringMatch5: Boolean;
validate4: HCkStringBuilder;
alphabet4: HCkStringBuilder;
charIndex4: Integer;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
readUrl: HCkStringBuilder;
cache: HCkJsonObject;
expires: HCkDateTime;
now: HCkDateTime;
refresh: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
stringMatch9: Boolean;
authHttp: HCkHttp;
authRequest: HCkHttpRequest;
authResponse: HCkHttpResponse;
authResult: HCkJsonObject;
authUrl: HCkStringBuilder;
attempt: Integer;
delaySeconds: Integer;
retried401: Boolean;
complete: Boolean;
jitter: HCkPrng;
tokenExpires: HCkDateTime;
stringMatch10: Boolean;
stringMatch11: Boolean;
validate9: HCkStringBuilder;
alphabet9: HCkStringBuilder;
charIndex9: Integer;
tokenLifetime: Integer;
tokenNow: HCkDateTime;
saved: HCkJsonObject;
stringMatch12: Boolean;
stringMatch13: Boolean;
waitSeconds: Integer;
retryAfter: PWideChar;
stringMatch14: Boolean;
retryText: HCkStringBuilder;
numeric: Boolean;
digits: HCkStringBuilder;
index: Integer;
retryDate: HCkDateTime;
waitMs: Integer;

begin
success := False;

stringCompare := CkStringBuilder_Create();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load private Key Vault configuration.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'vault_name'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'vault_name') <> 1) or (stringMatch1 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid vault_name.');
    Exit;
  end;
validate1 := CkStringBuilder_Create();
alphabet1 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate1,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate vault_name.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet1,'abcdefghijklmnopqrstuvwxyz0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate1) < 3) or (CkStringBuilder_getLength(validate1) > 24)) then
  begin
    Memo1.Lines.Add('Invalid vault_name length.');
    Exit;
  end;
charIndex1 := 0;
while (charIndex1 < CkStringBuilder_getLength(validate1)) do
  begin
    if (CkStringBuilder_Contains(alphabet1,CkStringBuilder__getRange(validate1,charIndex1,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in vault_name.');
        Exit;
      end;
    charIndex1 := charIndex1 + 1;
  end;

vaultName := CkStringBuilder_Create();
CkStringBuilder_Append(vaultName,CkJsonObject__stringOf(config,'vault_name'));
letters := CkStringBuilder_Create();
CkStringBuilder_Append(letters,'abcdefghijklmnopqrstuvwxyz');
if ((CkStringBuilder_Contains(letters,CkStringBuilder__getRange(vaultName,0,1,False),True) <> True) or (CkStringBuilder_EndsWith(vaultName,'-',True) = True) or (CkStringBuilder_Contains(vaultName,'--',True) = True)) then
  begin
    Memo1.Lines.Add('Invalid vault_name.');
    Exit;
  end;
origin := CkStringBuilder_Create();
success := CkStringBuilder_Append(origin,'https://');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(origin,CkJsonObject__stringOf(config,'vault_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(origin,'.vault.azure.net');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'cert_name'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'cert_name') <> 1) or (stringMatch2 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid cert_name.');
    Exit;
  end;
validate2 := CkStringBuilder_Create();
alphabet2 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate2,CkJsonObject__stringOf(config,'cert_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate cert_name.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet2,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate2) < 1) or (CkStringBuilder_getLength(validate2) > 127)) then
  begin
    Memo1.Lines.Add('Invalid cert_name length.');
    Exit;
  end;
charIndex2 := 0;
while (charIndex2 < CkStringBuilder_getLength(validate2)) do
  begin
    if (CkStringBuilder_Contains(alphabet2,CkStringBuilder__getRange(validate2,charIndex2,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in cert_name.');
        Exit;
      end;
    charIndex2 := charIndex2 + 1;
  end;

CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'client_id'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'client_id') <> 1) or (stringMatch3 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid client_id.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'client_secret'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'client_secret') <> 1) or (stringMatch4 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid client_secret.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'tenant_id'));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'tenant_id') <> 1) or (stringMatch5 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid tenant_id.');
    Exit;
  end;
validate4 := CkStringBuilder_Create();
alphabet4 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate4,CkJsonObject__stringOf(config,'tenant_id'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate tenant GUID.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet4,'0123456789abcdefABCDEF-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate4) < 36) or (CkStringBuilder_getLength(validate4) > 36)) then
  begin
    Memo1.Lines.Add('Invalid tenant GUID length.');
    Exit;
  end;
charIndex4 := 0;
while (charIndex4 < CkStringBuilder_getLength(validate4)) do
  begin
    if (CkStringBuilder_Contains(alphabet4,CkStringBuilder__getRange(validate4,charIndex4,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in tenant GUID.');
        Exit;
      end;
    charIndex4 := charIndex4 + 1;
  end;

http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
readUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(readUrl,CkStringBuilder__getAsString(origin));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(readUrl,'/certificates/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(readUrl,CkJsonObject__stringOf(config,'cert_name'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(readUrl,'?api-version=2026-04-01');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
cache := CkJsonObject_Create();
CkJsonObject_putStrict(cache,True);
expires := CkDateTime_Create();
now := CkDateTime_Create();
refresh := True;
success := CkJsonObject_LoadFile(cache,'qa_data/tokens/azureKeyVaultToken.json');
if (success = True) then
  begin
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(cache,'tenant_id'));
    stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,CkJsonObject__stringOf(config,'tenant_id'),True);
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(cache,'client_id'));
    stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,CkJsonObject__stringOf(config,'client_id'),True);
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(cache,'resource'));
    stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'https://vault.azure.net',True);
    if ((CkJsonObject_JsonTypeOf(cache,'access_token') = 1) and (CkJsonObject_JsonTypeOf(cache,'expires_at') = 1) and (stringMatch6 = True) and (stringMatch7 = True) and (stringMatch8 = True)) then
      begin
        success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(cache,'expires_at'));
        if (success = True) then
          begin
            success := CkDateTime_SetFromCurrentSystemTime(now);
            if (success <> True) then
              begin
                Memo1.Lines.Add('Could not read time.');
                Exit;
              end;
            CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(cache,'access_token'));
            stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
            if ((CkDateTime_DiffSeconds(expires,now) > 60) and (stringMatch9 <> True)) then
              begin
                CkHttp_putAuthToken(http,CkJsonObject__stringOf(cache,'access_token'));
                refresh := False;
              end;
          end;
      end;
  end;
authHttp := CkHttp_Create();
CkHttp_putRequireSslCertVerify(authHttp,True);
CkHttp_putFollowRedirects(authHttp,False);
CkHttp_putConnectTimeout(authHttp,20);
CkHttp_putReadTimeout(authHttp,60);
authRequest := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(authRequest,'POST');
CkHttpRequest_putContentType(authRequest,'application/x-www-form-urlencoded');
CkHttpRequest_AddParam(authRequest,'client_id',CkJsonObject__stringOf(config,'client_id'));
CkHttpRequest_AddParam(authRequest,'client_secret',CkJsonObject__stringOf(config,'client_secret'));
CkHttpRequest_AddParam(authRequest,'scope','https://vault.azure.net/.default');
CkHttpRequest_AddParam(authRequest,'grant_type','client_credentials');
authResponse := CkHttpResponse_Create();
authResult := CkJsonObject_Create();
CkJsonObject_putStrict(authResult,True);
authUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(authUrl,'https://login.microsoftonline.com/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(authUrl,CkJsonObject__stringOf(config,'tenant_id'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(authUrl,'/oauth2/v2.0/token');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
attempt := 0;
delaySeconds := 1;
retried401 := False;
complete := False;
jitter := CkPrng_Create();
while ((attempt < 5) and (complete <> True)) do
  begin
    attempt := attempt + 1;
    success := CkDateTime_SetFromCurrentSystemTime(now);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not read time.');
        Exit;
      end;
    if (refresh <> True) then
      begin
        if (CkDateTime_DiffSeconds(expires,now) <= 60) then
          begin
            refresh := True;
          end;
      end;
    if (refresh = True) then
      begin
        tokenExpires := CkDateTime_Create();
        success := CkDateTime_SetFromCurrentSystemTime(tokenExpires);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not read time.');
            Exit;
          end;
        success := CkHttp_HttpReq(authHttp,CkStringBuilder__getAsString(authUrl),authRequest,authResponse);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Token request failed.');
            Exit;
          end;
        if (CkHttpResponse_getStatusCode(authResponse) <> 200) then
          begin
            Memo1.Lines.Add('Token request was rejected; no blind authentication retries.');
            Exit;
          end;
        success := CkJsonObject_Load(authResult,CkHttpResponse__bodyStr(authResponse));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Invalid token response.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(authResult,'access_token'));
        stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(authResult,'access_token') <> 1) or (stringMatch10 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid access_token.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(authResult,'token_type'));
        stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'Bearer',True);
        if (stringMatch11 <> True) then
          begin
            Memo1.Lines.Add('Expected Bearer token.');
            Exit;
          end;
        if ((CkJsonObject_JsonTypeOf(authResult,'expires_in') <> 2)) then
          begin
            Memo1.Lines.Add('Missing token lifetime.');
            Exit;
          end;
        validate9 := CkStringBuilder_Create();
        alphabet9 := CkStringBuilder_Create();
        success := CkStringBuilder_Append(validate9,CkJsonObject__stringOf(authResult,'expires_in'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not validate token lifetime.');
            Exit;
          end;
        success := CkStringBuilder_Append(alphabet9,'0123456789');
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not initialize validation.');
            Exit;
          end;
        if ((CkStringBuilder_getLength(validate9) < 1) or (CkStringBuilder_getLength(validate9) > 8)) then
          begin
            Memo1.Lines.Add('Invalid token lifetime length.');
            Exit;
          end;
        charIndex9 := 0;
        while (charIndex9 < CkStringBuilder_getLength(validate9)) do
          begin
            if (CkStringBuilder_Contains(alphabet9,CkStringBuilder__getRange(validate9,charIndex9,1,False),True) <> True) then
              begin
                Memo1.Lines.Add('Invalid character in token lifetime.');
                Exit;
              end;
            charIndex9 := charIndex9 + 1;
          end;

        tokenLifetime := CkJsonObject_IntOf(authResult,'expires_in');
        if ((tokenLifetime <= 60) or (tokenLifetime > 604800)) then
          begin
            Memo1.Lines.Add('Unexpected token lifetime.');
            Exit;
          end;
        success := CkDateTime_AddSeconds(tokenExpires,tokenLifetime);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not calculate expiration.');
            Exit;
          end;
        tokenNow := CkDateTime_Create();
        success := CkDateTime_SetFromCurrentSystemTime(tokenNow);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not read time.');
            Exit;
          end;
        if (CkDateTime_DiffSeconds(tokenExpires,tokenNow) <= 60) then
          begin
            Memo1.Lines.Add('Token expired before it could be saved.');
            Exit;
          end;
        saved := CkJsonObject_Create();
        success := CkJsonObject_UpdateString(saved,'access_token',CkJsonObject__stringOf(authResult,'access_token'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set access_token.');
            Exit;
          end;
        success := CkJsonObject_UpdateString(saved,'expires_at',CkDateTime__getAsTimestamp(tokenExpires,False));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set expires_at.');
            Exit;
          end;
        success := CkJsonObject_UpdateString(saved,'tenant_id',CkJsonObject__stringOf(config,'tenant_id'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set tenant_id.');
            Exit;
          end;
        success := CkJsonObject_UpdateString(saved,'client_id',CkJsonObject__stringOf(config,'client_id'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set client_id.');
            Exit;
          end;
        success := CkJsonObject_UpdateString(saved,'resource','https://vault.azure.net');
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set resource.');
            Exit;
          end;
        success := CkJsonObject_WriteFile(saved,'qa_data/tokens/azureKeyVaultToken.json');
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not save the private token file.');
            Exit;
          end;
        success := CkDateTime_SetFromTimestamp(expires,CkJsonObject__stringOf(saved,'expires_at'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not read cached expiry.');
            Exit;
          end;
        CkHttp_putAuthToken(http,CkJsonObject__stringOf(saved,'access_token'));
        refresh := False;
      end;
    success := CkHttp_HttpReq(http,CkStringBuilder__getAsString(readUrl),request,response);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Read transport failed; inspect diagnostics privately.');
        Exit;
      end;
    if (CkHttpResponse_getStatusCode(response) = 200) then
      begin
        success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Invalid certificate response.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'id'));
        stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(result,'id') <> 1) or (stringMatch12 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid id.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'cer'));
        stringMatch13 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(result,'cer') <> 1) or (stringMatch13 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid cer.');
            Exit;
          end;
        complete := True;
      end
    else
      begin
        if ((CkHttpResponse_getStatusCode(response) = 401) and (retried401 <> True)) then
          begin
            refresh := True;
            retried401 := True;
          end
        else
          begin
            if ((CkHttpResponse_getStatusCode(response) <> 429) and (CkHttpResponse_getStatusCode(response) <> 500) and (CkHttpResponse_getStatusCode(response) <> 502) and (CkHttpResponse_getStatusCode(response) <> 503) and (CkHttpResponse_getStatusCode(response) <> 504)) then
              begin
                Memo1.Lines.Add('Non-retryable status or repeated 401.');
                Exit;
              end;
            if (attempt = 5) then
              begin
                Memo1.Lines.Add('Read retry limit reached.');
                Exit;
              end;
            waitSeconds := delaySeconds;
            retryAfter := CkHttpResponse__getHeaderField(response,'Retry-After');
            CkStringBuilder_SetString(stringCompare,retryAfter);
            stringMatch14 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
            if (stringMatch14 <> True) then
              begin
                retryText := CkStringBuilder_Create();
                CkStringBuilder_Append(retryText,retryAfter);
                numeric := True;
                digits := CkStringBuilder_Create();
                CkStringBuilder_Append(digits,'0123456789');
                index := 0;
                while (index < CkStringBuilder_getLength(retryText)) do
                  begin
                    if (CkStringBuilder_Contains(digits,CkStringBuilder__getRange(retryText,index,1,False),True) <> True) then
                      begin
                        numeric := False;
                      end;
                    index := index + 1;
                  end;

                if (numeric = True) then
                  begin
                    if (CkStringBuilder_getLength(retryText) > 8) then
                      begin
                        Memo1.Lines.Add('Retry-After exceeds this example budget; retry later.');
                        Exit;
                      end;
                    waitSeconds := CkStringBuilder_getIntValue(retryText);
                  end
                else
                  begin
                    retryDate := CkDateTime_Create();
                    success := CkDateTime_SetFromRfc822(retryDate,retryAfter);
                    if (success <> True) then
                      begin
                        Memo1.Lines.Add('Invalid Retry-After date.');
                        Exit;
                      end;
                    success := CkDateTime_SetFromCurrentSystemTime(now);
                    if (success <> True) then
                      begin
                        Memo1.Lines.Add('Could not read time.');
                        Exit;
                      end;
                    waitSeconds := CkDateTime_DiffSeconds(retryDate,now);
                  end;
              end;
            if (waitSeconds < 0) then
              begin
                waitSeconds := 0;
              end;
            if (waitSeconds > 30) then
              begin
                Memo1.Lines.Add('Retry-After exceeds this example budget; retry later without shortening it.');
                Exit;
              end;
            waitMs := waitSeconds * 1000 + CkPrng_RandomInt(jitter,0,250);
            CkHttp_SleepMs(http,waitMs);
            delaySeconds := delaySeconds * 2;
          end;
      end;
  end;

if (complete <> True) then
  begin
    Memo1.Lines.Add('Read did not complete within the attempt budget.');
    Exit;
  end;
Memo1.Lines.Add('Example completed.');

CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate1);
CkStringBuilder_Dispose(alphabet1);
CkStringBuilder_Dispose(vaultName);
CkStringBuilder_Dispose(letters);
CkStringBuilder_Dispose(origin);
CkStringBuilder_Dispose(validate2);
CkStringBuilder_Dispose(alphabet2);
CkStringBuilder_Dispose(validate4);
CkStringBuilder_Dispose(alphabet4);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkStringBuilder_Dispose(readUrl);
CkJsonObject_Dispose(cache);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkHttp_Dispose(authHttp);
CkHttpRequest_Dispose(authRequest);
CkHttpResponse_Dispose(authResponse);
CkJsonObject_Dispose(authResult);
CkStringBuilder_Dispose(authUrl);
CkPrng_Dispose(jitter);
        CkDateTime_Dispose(tokenExpires);
        CkStringBuilder_Dispose(validate9);
        CkStringBuilder_Dispose(alphabet9);
        CkDateTime_Dispose(tokenNow);
        CkJsonObject_Dispose(saved);
                CkStringBuilder_Dispose(retryText);
                CkStringBuilder_Dispose(digits);
                    CkDateTime_Dispose(retryDate);