info:
  name: "[Runner] Get All Contracts"
  type: http
  seq: 1

http:
  method: GET
  url: "{{IrisApiUrl}}/contracts;full?page={{runnerCurrentPage}}&sort=pid&dir=asc"
  params:
    - name: page
      value: "{{runnerCurrentPage}}"
      type: query
    - name: sort
      value: pid
      type: query
      description: |-
        The usable fields for sorting are:
        - id
        - pid
        - year
        - name
        - startDate
        - lastModified
        - wfItemType.identifier (only for item with validation flow)
        - wfItemType.description (only for item with validation flow)

        The actual usable sorting filters for a specific resource type are specified in the REST Service Details
    - name: dir
      value: asc
      type: query
  auth: inherit

runtime:
  scripts:
    - type: after-response
      code: |-
        test("Status code is 200", function () {
          expect(res.getStatus()).to.equal(200);

          // Get pagination headers
          var currentPage = parseInt(res.getHeader("Page"));
          var totalPages = parseInt(res.getHeader("Page-Count"));

          // If first loop, reset results array
          if (currentPage == 1)
          {
            bru.setVar("runnerResultsArray", JSON.stringify([]));
          }

          // Update result array
          let resultsArray = JSON.parse(bru.getVar("runnerResultsArray"));
          var results = res.getBody();
          resultsArray = resultsArray.concat(results);
          bru.setVar("runnerResultsArray", JSON.stringify(resultsArray));
          
          // Loop decision
          if (currentPage == totalPages)
          {
            // Loop has finished, reset variables and stop
            bru.setVar("runnerCurrentPage", 1);
            bru.deleteVar("runnerTotalPages");
        //     //pm.collectionVariables.unset("runnerResultsArray"); // Do not unset, so you can find results in the env var!
          }
          else
          {
            // Still having pages, update pagination values
            bru.setVar("runnerCurrentPage", ++currentPage);
            bru.setVar("runnerTotalPages", totalPages);

            // Set next request and wait for 1 second before sending the next request. This is to avoid hitting the rate limit.
            bru.runner.setNextRequest(req.getName());
            setTimeout(function(){}, [1000]);
          }
        });

settings:
  encodeUrl: true
  timeout: 0
  followRedirects: true
  maxRedirects: 5
  forwardAuthorizationHeader: true
