Skip to content

Commit

Permalink
Fix parsing of QueryResult responses after recent API changes
Browse files Browse the repository at this point in the history
Starting some days ago Alma added a namespace to the "rowset" tag in
the QueryResult response. Adjust our xpath to ignore namespaces there
to work with the old and the new format, and move the parsing in
a central place.

This fixes all APIs using the analytics API internally to return empty
responses.
  • Loading branch information
lazka committed Nov 4, 2024
1 parent 416ab6e commit cb7a139
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Service/AlmaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public function addAllBookLoansByLibraryToCollection(Sublibrary $library, ArrayC
$resumptionData['token'] ??= (string) $xml->ResumptionToken;

$isFinished = ((string) $xml->IsFinished) !== 'false';
$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

/** @var \SimpleXMLElement $row */
foreach ($rows as $row) {
Expand Down Expand Up @@ -1425,7 +1425,7 @@ public function getBudgetMonetaryAmountsByLibrary(Sublibrary $library): array
$mapping = AlmaUtils::getColumnMapping($xml);
$libraryCode = $library->getCode();
$fundLedgerCode = $libraryCode.'MON';
$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

if (count($rows) === 0) {
return [];
Expand Down Expand Up @@ -1549,7 +1549,7 @@ public function addAllBookOffersByLibraryToCollection(Sublibrary $library, Array
$resumptionData['token'] ??= (string) $xml->ResumptionToken;

$isFinished = ((string) $xml->IsFinished) !== 'false';
$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

/** @var \SimpleXMLElement $row */
foreach ($rows as $row) {
Expand Down Expand Up @@ -1691,7 +1691,7 @@ public function addAllBookOrdersByLibraryToCollection(Sublibrary $library, Array
$resumptionData['token'] ??= (string) $xml->ResumptionToken;

$isFinished = ((string) $xml->IsFinished) !== 'false';
$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

// FIXME: We get duplicated entries where Invoice Line-Currency/Invoice-Currency/Invoice Line Total Price
// are missing. Since we don't use them right now just ignore those duplicates.
Expand Down Expand Up @@ -1802,7 +1802,7 @@ private function getAnalyticsUpdatesHash()
return $this->getFallbackAnalyticsUpdatesHash();
}

$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

if (count($rows) === 0) {
return $this->getFallbackAnalyticsUpdatesHash();
Expand All @@ -1827,7 +1827,7 @@ public function getAnalyticsUpdatesData()
return null;
}

$rows = $xml->xpath('ResultXml/rowset/Row');
$rows = AlmaUtils::getRows($xml);

if (count($rows) === 0) {
return null;
Expand Down
11 changes: 11 additions & 0 deletions src/Service/AlmaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ public static function mapRowColumns(\SimpleXMLElement $row, array $mapping): ar

return $values;
}

public static function getRows(\SimpleXMLElement $queryResult): array
{
// Around 2024-11 they added namespaces to rowset, so match by tag name only to support both versions
$res = $queryResult->xpath('ResultXml//*[local-name()="Row"]');
if ($res === null || $res === false) {
throw new \RuntimeException('Broken Alma response');
}

return $res;
}
}
28 changes: 28 additions & 0 deletions tests/AlmaUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,32 @@ public function testGetColumnMapping()
$this->assertEquals($mapped['::0'], '');
$this->assertEquals($mapped['Bibliographic Details::Author'], 'Zwerger, Klaus');
}

public function testGetRows()
{
$data = '<QueryResult>
<ResumptionToken>foobar</ResumptionToken>
<IsFinished>true</IsFinished>
<ResultXml>
<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<Row>
<Column0>0</Column0>
<Column1>Something</Column1>
<Column2>F00042FOR</Column2>
<Column3>Test</Column3>
<Column4>1</Column4>
<Column5>0</Column5>
<Column6>1</Column6>
<Column7>1</Column7>
<Column8>0</Column8>
</Row>
</rowset>
</ResultXml>
</QueryResult>';

$xml = simplexml_load_string($data);
$rows = AlmaUtils::getRows($xml);
$this->assertCount(1, $rows);
$this->assertSame('Test', (string) $rows[0]->Column3);
}
}

0 comments on commit cb7a139

Please sign in to comment.