calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
359 stars 262 forks source link

No response from request->send() when no content is returned from xero #868

Open arunmanivel opened 2 years ago

arunmanivel commented 2 years ago

I am using BankStatement API to sync bank statements from Xero for a given account ID. It works fine except when there is no content returned from Xero. Please see below my code. The program dies after $request->send().

function xero_get_bank_statements($fromDate,$toDate,$accountID) { $url = new URL($xero, 'Reports/BankStatement'); $request = new Request($xero, $url, Request::METHOD_GET); $request->setParameter('bankAccountID', $accountID); $request->setParameter('fromDate', $fromDate); $request->setParameter('toDate', $toDate); $request->send(); $elements = $request->getResponse()->getElements(); return $elements; }

I troubleshooted the issue and noticed that send() function first sends data to xero and then creates a response object using $guzzleResponse->getBody()->getContents(). For me, there is nothing returned from Xero. I am guessing there is no statements for that account ID, but I am not sure.

After creating response object, parse() function is called to parse the response. This is where the execution stops. There is no return value or response. I guess parse is failing as there is no content. There is no check for response from xero in parse function. I think we should not allow parsing if there is no content returned from Xero.

calcinai commented 2 years ago

Hi @arunmanivel is there a reason you're manually constructing this request rather than using the query builder (like in the readme?)

$xero->load(BankStatement::class)
    ->where('bankAccountID', $accountID)
    ->where('fromDate', $fromDate)
    ->where('toDate', $toDate)
    ->execute();

As far as I know, the above souls work for reports, too.

arunmanivel commented 2 years ago

@calcinai No specific reason. The code was written few years back by another developer. I have just inherited it. Thanks for pointing it out though. We will start using it for upcoming features. But will that solve the problem that I reported? Thank you.