RESOStandards / RESO-WebAPI-Client-PHP

RESO Web API Open Source Reference Client written in PHP
Other
4 stars 2 forks source link

Support sending odata.maxpagesize header #17

Open jaybeaton opened 3 weeks ago

jaybeaton commented 3 weeks ago

Description

Bridge Interactive is changing how their Bridge API handles the $top operator:

https://www.bridgeinteractive.com/bridge-reso-web-api-changes-coming-uat-now-available-for-testing/

The $top operator will be modified to match the OData specification.

  • Bridge API previously treated $top as the page size, whereas OData specifies that $top is meant to be the total collection size. In other words, $top is meant to be the total number of records returned once you have consumed every page.
  • Clients may specify a given page size by utilizing the maxpagesize header. The current default is 10 with a maximum value of 200.

The current RESO-WebAPI-Client-PHP code does not support setting the maxpagesize header, so you won't be able to set the number of items per page after they make this change.

jaybeaton commented 3 weeks ago

With this change, the RESO-WebAPI-Client-PHP code can support setting the maxpagesize header:

diff --git a/lib/Request.php b/lib/Request.php
index 3442c53da..a56fc85ff 100644
--- a/lib/Request.php
+++ b/lib/Request.php
@@ -16,11 +16,11 @@ abstract class Request
      * @param string $request
      * @param string $output_format
      * @param string $decode_json
-     * @param string $accept_format
+     * @param int $max_page_size
      *
      * @return mixed API Request response in requested data format.
      */
-    public static function request($request, $output_format = "xml", $decode_json = false)
+    public static function request($request, $output_format = "xml", $decode_json = false, $max_page_size = 0)
     {
         \RESO\RESO::logMessage("Sending request '".$request."' to RESO API.");

@@ -52,6 +52,9 @@ public static function request($request, $output_format = "xml", $decode_json =
             "Accept: ".$accept,
             "Authorization: Bearer ".$token
         );
+        if ($max_page_size) {
+          $headers[] = "prefer: odata.maxpagesize=".$max_page_size;
+        }

         // Send request
         $response = $curl->request("get", $url, $headers, null, false);

You could then get 2000 total items with pages of 200 items with a call like this:

<?php
$query = 'Property?$filter=ModificationTimestamp ge 2024-09-13T00:00:00Z&$top=2000';
$max_page_size = 200;
$results = Request::request($query, 'json', TRUE, $max_page_size);