Closed imneckro closed 8 months ago
There are a lot of things going on here. For example, you have repeatitive code and your code will be a lot simpler if you used a loop instead.
You can also replace this code block:
$myarrdet1 = $responsedet->attributes();
$myarrdet1 = $myarrdet1['invoicesDoc']->attributes();
$myarrdet1 = $myarrdet1['invoice'];
with this line:
$myarrdet1 = $responsedet->getInvoices();
And to answer you question, no there isn't actually a direct way to know the number of pages in advance. Your approach of getting the all the pages is correct, but you risk getting timed out for too many requests. You might want to consider caching the continuation tokens, or saving all the continuation tokens temporarily in a file.
@firebed thanks a lot once again for your help! Can you guide me with an exaxmple how to aproach a loop?
I tried below approach:
function getbookinfo(){
try {
$request = new RequestTransmittedDocs();
$response = $request->handle(
dateFrom: "01/01/2024",
dateTo: "03/03/2024"
);
$continuationToken = $response->getContinuationToken() ?? null;
$resp = $response->getInvoices();
$myarr = $resp;
if($continuationToken == null){
print_r($myarr);
}else{
while($continuationToken!=null){
$request = new RequestTransmittedDocs();
$response = $request->handle(
dateFrom: "01/01/2024",
dateTo: "31/01/2024",
nextPartitionKey: $continuationToken->getNextPartitionKey(),
nextRowKey: $continuationToken->getNextRowKey(),
);
$continuationToken = $response->getContinuationToken() ?? null;
$resp = $response->getInvoices();
$myarr = array_merge_recursive($myarr,$resp);
if($continuationToken==null){
print_r($myarr);
exit;
}
}
}
}catch (MyDataException $e) {;
echo "Σφάλμα επικοινωνίας: " . $e->getMessage();
}
}
but gives me bellow error:
Uncaught TypeError: array_merge_recursive(): Argument #1 must be of type array,
nevermind did it with :
function getbookinfoarray($datefrom,$dateto){
try {
$request = new RequestTransmittedDocs();
$response = $request->handle(
dateFrom: $datefrom,
dateTo: $dateto
);
$continuationToken = $response->getContinuationToken() ?? null;
$resp = $response->getInvoices();
$myarr = $resp;
if($continuationToken == null){
return $myarr;
}else{
while($continuationToken!=null){
$request = new RequestTransmittedDocs();
$response = $request->handle(
dateFrom: $datefrom,
dateTo: $dateto,
nextPartitionKey: $continuationToken->getNextPartitionKey(),
nextRowKey: $continuationToken->getNextRowKey(),
);
$continuationToken = $response->getContinuationToken() ?? null;
$resp = $response->getInvoices();
if($myarr==null){
$myarr = $resp;
}else{
$myarr = array_merge_recursive($myarr,$resp);
}
if($continuationToken==null){
return $myarr;
exit;
}
}
}
}catch (MyDataException $e) {;
echo "Σφάλμα επικοινωνίας: " . $e->getMessage();
}
}
I haven't been able to test this and see if it actually works. But this is where I would start from:
use Firebed\AadeMyData\Exceptions\MyDataException;
use Firebed\AadeMyData\Http\RequestTransmittedDocs;
function getTransmittedDocs($dateFrom, $dateTo): array
{
$invoices = [];
$request = new RequestTransmittedDocs();
try {
$continuationToken = null;
do {
$response = $request->handle(
dateFrom: $dateFrom,
dateTo: $dateTo,
nextPartitionKey: $continuationToken?->getNextPartitionKey(),
nextRowKey: $continuationToken?->getNextRowKey()
);
$invoices = array_merge($invoices, $response->getInvoices()->all());
$continuationToken = $response->getContinuationToken();
} while ($continuationToken);
} catch (MyDataException $e) {
die($e->getMessage());
}
return $invoices;
}
it's great and it works.
can I replace in RequestVatInfo $resp = $response->attributes(); $resp = $resp['VatInfo']; $myarr = $resp; with a one liner ?
it's great and it works.
can I replace in RequestVatInfo $resp = $response->attributes(); $resp = $resp['VatInfo']; $myarr = $resp; with a one liner ?
Yes, you can do:
$myarr = $response->attributes()['VatInfo'];
or even better:
$myarr = $response->getVatInfo();
You can always check the docs for more details.
1) Is there a way to get the length of continuation tokens?
2) Is there a way to keep requesting until all continuation tokens used?
3) Thanks in advance
Till now im using
which is not convenient...