XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
91 stars 65 forks source link

Retrieve Headers of Response #238

Closed willhemming closed 3 years ago

willhemming commented 3 years ago

Xero developer pages tell us

Each API response you receive will include the X-DayLimit-Remaining, X-MinLimit-Remaining and X-AppMinLimit-Remaining headers telling you the number of remaining against each limit.

https://developer.xero.com/documentation/guides/oauth2/limits/#uncertified-app-limits

Can these headers be retrieved from the response so as to track how close we are to the API limits? We get the 429 / actual error message using

try { $apiResponse = $apiInstance->createInvoices($xeroTenantId,array("Invoices"=>$arr_invoices)); } catch (Exception $e) { echo 'Exception when calling AccountingApi->createInvoices: '.$e->getMessage(); }

But it would be handy to get those 'remaining' headers to understand which limit we are hitting. Any suggestions please?

SerKnight commented 3 years ago

Hey @willhemming - a reasonable request. I am taking over maintenance for this repo.

I figured out that you can pass the debug option to the guzzle http client setup and that will echo out all the headers (plus a ton of other crud) - I can work on a way to more easily do something like the following though, as this is a reasonable request that I am very surprised is not yet supported.

Ideally I think you should be able to do something like:

$apiResponse->getHeaders()

With debug option

  $apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
    new GuzzleHttp\Client(['debug' => true]),
    $config
  );

< HTTP/1.1 200 OK < Content-Type: application/json; charset=utf-8 < Content-Length: 2116 < Server: nginx < Xero-Correlation-Id: 3496287c-1fb9-407f-8363-07b61c2f8046 < X-AppMinLimit-Remaining: 9999 < X-MinLimit-Remaining: 59 < X-DayLimit-Remaining: 4956 < Expires: Thu, 22 Jul 2021 20:18:04 GMT < Cache-Control: max-age=0, no-cache, no-store < Pragma: no-cache < Date: Thu, 22 Jul 2021 20:18:04 GMT < Connection: keep-alive < X-Client-TLS-ver: tls1.3 < * Connection #0 to host api.xero.com left intact

willhemming commented 3 years ago

hey @SerKnight - thanks for the response

$apiResponse->getHeaders()

^ this does not work for me, throws a fatal error.

Adding ['debug' => true] into the Guzzle command does mean I get all the detail echo'd out, will see if I can find a way to use it.

A clean way to get it would be greatly appreciated though :)

SerKnight commented 3 years ago

Yes I am saying it would be ideal to have $apiResponse->getHeaders()

For now you can do the debug, but I will scope some work to get header access into this project asap

willhemming commented 3 years ago

Oh right! Thank you, really appreciate the help. (& great to see this repo getting some attention!)

SerKnight commented 3 years ago

Hi @willhemming - I found a good solution in the existing code without introducing a breaking change.

https://github.com/XeroAPI/xero-php-oauth2#accessing-http-headers

Basically, you can use the <functionName>WithHttpInfo declaration of any function to get an array back with the HTTP headers as the 3rd element (accessed by $apiResponse[2])

Let me know if you have any improvements to the docs if you can get this working.