Jump to content
Joe Sansalone

REST Client gets STUCK (no response or timeout exception etc)

Recommended Posts

Hi,

 

(Delphi 11, also tried Delphi 10.4)

 

I'm running an application 24/7 that works for a few weeks and

then gets STUCK waiting for a response from executing a REST Client Request.

 

No exception.  No timeout.  Does not return from 

  FRESTRequest.Execute

 

The server that is being Requested, gets the request

AND does respond.


Below is the code.  

The ModifyCall procedure gets executed many times and works before hitting this problem.

Sometimes restarting the application is not enough to fix the problem. I need to restart windows!

 

1. Can I force a max timeout?  So that if it gets stuck, at least I can handle the timeout.

 

2.  Any idea why this would occur?

 

 

Joe

 

 

 

 

 

constructor TTwilioAPI.Create(aAccountSID, aPassword: string);
begin
  inherited Create;
  FTwiML := TTwilioML.Create;

  FAccountSID := aAccountSID;
  FPassword := aPassword;

  FRESTClient := TRESTClient.Create('https://api.twilio.com/2010-04-01');
  FRESTRequest := TRESTRequest.Create(FRESTClient);
  FRESTResponse := TRESTResponse.Create(FRESTClient);
  FHTTPBasicAuthenticator := THTTPBasicAuthenticator.Create(FAccountSID, FPassword);

  FRESTClient.Authenticator := FHTTPBasicAuthenticator;
  FRESTRequest.Client := FRESTClient;
  FRESTRequest.Response := FRESTResponse;
  FRESTClient.ContentType := 'application/x-www-form-urlencoded';


  FRESTClient.SecureProtocols := [THTTPSecureProtocol.TLS12, THTTPSecureProtocol.TLS11, THTTPSecureProtocol.TLS1];

  FBaseURL := cBaseURL;  // default
end;
 

 

 

 

function TTwilioAPI.ModifyCall(aTwiML, aCallSid: string; sync: boolean = false): boolean;
var statusCode: integer;
begin
  result := True;
  FRESTRequest.Params.Clear;
  FRestRequest.Resource :=  '/Accounts/{AccountSid}/Calls/{Sid}.json';

  FRestRequest.Params.AddItem('AccountSid', FAccountSID, TRestRequestParameterKind.pkURLSEGMENT);
  FRestRequest.Params.AddItem('Sid', aCallSid, TRestRequestParameterKind.pkURLSEGMENT);
  FRestRequest.Params.AddItem('Twiml', aTwiML, TRestRequestParameterKind.pkGETorPOST);

  FRESTRequest.Method := rmPOST;
  try
    FRESTRequest.Execute;     <=============== does not Return from this
  except on E:EHTTPProtocolException do  
    begin
      if Assigned(FLog) then FLog.WriteTimeStamp(lctHTTP, lptImportant, 'ModifyCall API: error executing request');
      result := false;
      raise ETwilioCallError.Create('HTTP protocol exception');
    end;

  on E:ERESTException do  
    begin
      if Assigned(FLog) then FLog.WriteTimeStamp(lctHTTP, lptImportant, 'ModifyCall API: ERESTException');
      result := false;
      raise ETwilioCallError.Create('HTTP protocol exception');
    end;

  end;

  statusCode := FRESTResponse.StatusCode;
  //TODO: IF 400 or 500 raise Exception ETwilioError

  FResponseData := TJSON.JsonToObject<TTwilioResponseObj>(TranslateJsonNames(FRESTResponse.Content));

  if Assigned(FLog)  then FLog.WriteTimeStamp(lctHTTP, lptNormal, 'ModifyCall API response: status: ' +
                                FResponseData.status + '  sid(call): ' + FResponseData.sid + '   statusCode: ' + InttoStr(statusCode)
                                + '    raw response:' + FRESTResponse.Content + '  message: ' + FResponseData.msg);

 

  if (statusCode = 400) then
  begin
    if Pos('Call is already in transferring', FResponseData.msg) > 0 then
    begin
      if Assigned(FLog) then FLog.WriteTimeStamp(lctHTTP, lptImportant, 'ModifyCall API - BUG');
    end;

    if Pos('Call is not in_progress', FResponseData.msg) > 0 then
      raise ETwilioHungup.Create('No call in progress')
    else
      raise ETwilioCallError.Create('status code 400');
  end;


end;
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×