Jump to content

Anxich

Members
  • Content Count

    6
  • Joined

  • Last visited

Everything posted by Anxich

  1. Hello, I am writing an application that is using an third party API. Now, this API has limits of requests that can be made within a minute. So I would like to add a timeout and then for my application to retry the request again. I've wrote a piece of code like that: unit cSslHttpRestASync; interface uses OverbyteIcsSslHttpRest, Classes, OverbyteIcsHttpProt; const MaxRetryAmount = 10; type TSslHttpRestASync = class(TSslHttpRest) private procedure OnRequestDoneAsync(Sender : TObject; Request : THttpRequest; ErrorCode : Word); protected FDone : Boolean; FError : Boolean; FRetryCount : Integer; public property Done : Boolean read FDone; property Error : Boolean read FError; constructor Create(AOwner: TComponent); override; end; implementation uses cUtils, SysUtils, DateUtils; { TSslHttpRestAsync } constructor TSslHttpRestAsync.Create(AOwner: TComponent); begin inherited Create(AOwner); OnRequestDone := OnRequestDoneAsync; FRetryCount := 0; end; procedure TSslHttpRestAsync.OnRequestDoneAsync(Sender: TObject; Request: THttpRequest; ErrorCode: Word); var SslHttpRestASync : TSslHttpRestASync; I : Integer; RetryAfter : Integer; begin SslHttpRestASync := Sender as TSslHttpRestASync; if ((SslHttpRestASync.StatusCode = 200) or (SslHttpRestASync.StatusCode = 201)) then begin FDone := True; end else begin if FRetryCount = MaxRetryAmount then begin FError := True; end; if SslHttpRestASync.StatusCode = 429 then begin SaveToLog('FRetryCount: ' + IntToStr(FRetryCount) + sLineBreak + 'URL: ' + SslHttpRestASync.URL + sLineBreak + SslHttpRestASync.RcvdHeader.Text); for I := 0 to SslHttpRestASync.RcvdHeader.Count - 1 do begin if Pos('Retry-After: ', SslHttpRestASync.RcvdHeader[I]) <> 0 then begin RetryAfter := StrToInt(Copy(SslHttpRestASync.RcvdHeader[I], Pos(' ', SslHttpRestASync.RcvdHeader[I]) + 1, Length(SslHttpRestASync.RcvdHeader[I]))); end; end; Sleep(RetryAfter * 1000); GetASync; end else begin Inc(FRetryCount); Sleep(500); GetASync; end; end; end; end. Now, the problem is, I am getting same header over and over in my logs. The date matches the date of first request - like the request isn't sent again and I am just being served same information. What am I doing wrong here? I'm completely lost.
  2. I've modified my timer like that after I looked through ICS source code. procedure TSslHttpRestASync.OnTimer(Sender: TObject); begin if IcsTestTrgTick64(FResendTrigger) then begin FResendTrigger := Trigger64Disabled; if FRequestType = httpPOST then FSendStream.Seek(0, soFromBeginning); DoRequestASync(FRequestType); end; end; It is now working all good, thank you all for help 🙂
  3. Yes, the params are gone when reissuing a request the way I did it. "Content-Length: 0"
  4. Hey, I've been using Postman all along, but the API I am working with is very badly done (government job). Official docs say 6000 requests per minute, in reality it's 60 requests per minute on search methods, 1000 on getters etc. So I am working a way here to make it work universally. Retry-After is implemented in my code as well, just I didn't share it here. Also - said API - does not always send that in header...
  5. Thank you, that shed some light on this for me. I am having a problem with this approach though as well, probably I am missing something again. It seems that POST methods do not have the params I've given them in first place when I am resending the query. procedure TSslHttpRestASync.OnTimer(Sender: TObject); begin if IcsTestTrgTick64(FResendTrigger) then begin FResendTrigger := Trigger64Disabled; DoRequestASync(FRequestType); end; end; GET request seem to be working just fine, however I am adding parameters directly to URL with these requests. When executing POST requests, I am using code like this. FSslHttpRest[I].RestRequest(httpPOST, FSslHttpRest[I].URL, True, FJson[I].AsJSon()); Any ideas? Thanks for your help so far 🙂
  6. Thanks, that worked fine 🙂 Can you explain to me why this way works, but mine didn't? Or let me know where to find answer to that? I'd really appreciate that. I want to understand what I've done.
×