honzajavorek / fiobank

Fio Bank API in Python
ISC License
38 stars 21 forks source link

x-IntervalUseToken #19

Open PSLLSP opened 6 years ago

PSLLSP commented 6 years ago

There is an extra header "x-IntervalUseToken" returned by server, it indicates required delay before next query with the same token. Some calls, like setting pointer doesn't require delay.

Example : x-IntervalUseToken: 20000

x-IntervalUseToken: 0

Unfortunately, when reply "409 Conflict" is returned, header x-IntervalUseToken is not in the server reply :-(

honzajavorek commented 6 years ago

I don't completely get your point. Is the header something the API returns and we could use it to make the fiobank library better?

PSLLSP commented 6 years ago

I believe, you can use return value of x-IntervalUseToken to speed up your API. Store returned value of x-IntervalUseToken (find timestamp of next allowed access) and store it; add a function to return value to user application that can query if it can send another query. And use it to wait in the case that user application tries to get data too early.

Your python code assumes, that it has to wait 30 seconds between querires (it is information from official documenatation). But server signals how long you have to wait. Try to use that information...

Example of query and reply, note header x-IntervalUseToken in the server reply:

$ curl -v -H 'Accept: text/plain' https://www.fio.cz/ib_api/rest/periods/{TOKEN}/2018-03-04/2018-05-04/transactions.gpc

> GET /ib_api/rest/periods/{TOKEN}/2018-03-04/2018-05-04/transactions.gpc HTTP/1.1
> Host: www.fio.cz
> User-Agent: curl/7.47.0
> Accept: text/plain
> 
< HTTP/1.1 200 OK
< Date: Fri, 04 May 2018 13:28:33 GMT
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Referrer-Policy: strict-origin-when-cross-origin
< x-IntervalUseToken: 20000
< Content-Type: text/gpc;charset=windows-1250
< Content-Length: 390
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=31536000
< 
074000000xxxxxxxxxxnnnnnnnnnnnnnnnnnnnxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx000040518FIO   
$ curl -v -H 'Accept: text/plain' https://www.fio.cz/ib_api/rest/set-last-date/{TOKEN}/2017-01-01/

> GET /ib_api/rest/set-last-date/{TOKEN}/2017-01-01/ HTTP/1.1
> Host: www.fio.cz
> User-Agent: curl/7.47.0
> Accept: text/plain
> 
< HTTP/1.1 200 OK
< Date: Fri, 04 May 2018 13:44:25 GMT
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Referrer-Policy: strict-origin-when-cross-origin
< x-IntervalUseToken: 0
< Content-Length: 0
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=31536000
< 
$ curl -v -H 'Accept: text/plain' 'https://www.fio.cz/ib_api/rest/lastStatement/{TOKEN}/statement?year=2017'

> GET /ib_api/rest/lastStatement/{TOKEN}/statement?year=2017 HTTP/1.1
> Host: www.fio.cz
> User-Agent: curl/7.47.0
> Accept: text/plain
> 
< HTTP/1.1 200 OK
< Date: Fri, 04 May 2018 13:47:06 GMT
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Referrer-Policy: strict-origin-when-cross-origin
< x-IntervalUseToken: 0
< Content-Type: text/plain;charset=UTF-8
< Vary: Accept-Encoding
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=31536000
< Transfer-Encoding: chunked
< 
* Connection #0 to host www.fio.cz left intact
2017,4

I was too fast, error 409 was returned and there was no header x-IntervalUseToken in the reply :-(. It means, value x-IntervalUseToken has to be saved when it is returned with valid reply - 200 OK).

$ curl -v -H 'Accept: text/plain' https://www.fio.cz/ib_api/rest/periods/{TOKEN}/2018-03-04/2018-05-04/transactions.gpc

> GET /ib_api/rest/periods/{TOKEN}/2018-03-04/2018-05-04/transactions.gpc HTTP/1.1
> Host: www.fio.cz
> User-Agent: curl/7.47.0
> Accept: text/plain
> 
< HTTP/1.1 409 Conflict
< Date: Fri, 04 May 2018 14:05:19 GMT
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Referrer-Policy: strict-origin-when-cross-origin
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 0
< X-Frame-Options: SAMEORIGIN
< Strict-Transport-Security: max-age=31536000
<