Solved

Looping REST API calls in one RSD file

  • 1 December 2023
  • 6 replies
  • 143 views

Badge

Hi I have a REST API that uses an item index as part of the URI, in the format https://<URL>/LocationPerilGroup/{PerilGroupID}.

So an example call might be https://<URL>/LocationPerilGroup/12.

This is a small lookup table and I was hoping that i could use an enum to repeatedly call the API while iterating through the list (40 items) to return the table to the ODX.

My enum does iterate, but I only get output from the first record.

Can this be done? Is there something I have missed? Please help.

RSD file content below:

 

<api:script xmlns:api="http://apiscript.com/ns?v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- See Column Definitions to specify column behavior and use XPaths to extract column values from JSON. -->
  <api:info title="locationPerilSets" desc="Generated schema file." xmlns:other="http://apiscript.com/ns?v1">
    <!-- You can modify the name, type, and column size here. -->
    <attr name="LocationPerilSetsRecord_id"    xs:type="integer"  readonly="false"  key="true"  other:xPath="/json/locationPerilSets/_id"          />
    <attr name="locationPerilSetId"            xs:type="integer" readonly="false"              other:xPath="/json/locationPerilSets/id"           />
    <attr name="locations"                   xs:type="string"  readonly="false"              other:xPath="/json/locationPerilSets/locations"    />
    <attr name="name"                        xs:type="string"  readonly="false"              other:xPath="/json/locationPerilSets/name"         />
    <attr name="perils"                      xs:type="string"  readonly="false"              other:xPath="/json/locationPerilSets/perils"       />
  </api:info>

  <api:set attr="in.DataModel" value="RELATIONAL" />
  <api:set attr="in.JSONPath" value="$.locationPerilSets" />
  <api:set attr="in.URITemplate" value="https://someurl.net/api/REST/LocationPerilGroup/{perilset_id}"/>
  

  <!-- The GET method corresponds to SELECT. Here you can override the default processing of the SELECT statement. The results of processing are pushed to the schema's output. See SELECT Execution for more information. -->
<api:script method="GET">
    <api:set attr="method" value="GET"/>
    <api:enum range="6..10">
        <!-- [_value] -->
        <api:set attr="in.URI" value="[in.URITemplate| replace('{perilset_id}', [_value])]" />
        <!-- <api:call op="jsonproviderGet"> -->
        <api:call op="jsonproviderGet" in="in" out="PerilGroupOut">
            <api:set attr="out.LocationPerilSetsRecord_id"     value="[PerilGroupOut.LocationPerilSetsRecord_id | allownull()]"/>
            <api:set attr="out.locationPerilSetId"               value="[PerilGroupOut.locationPerilSetId | allownull()]"/>
            <api:set attr="out.locations"                     value="[PerilGroupOut.locations | allownull()]"/>
            <api:set attr="out.name"                         value="[PerilGroupOut.name | allownull()]"/>
            <api:set attr="out.perils"                         value="[PerilGroupOut.perils | allownull()]"/>

            <api:push item="out"/>
        </api:call>
    </api:enum>
</api:script>

</api:script>

 

icon

Best answer by Thomas Lind 4 December 2023, 08:22

View original

6 replies

Userlevel 5
Badge +7

Hi @Paul McLeod Bespoke Analytics ,

 

you should be able to use the tutorial here: 

You might need to take on board some of the other examples if you also have pagination or the like.

Userlevel 6
Badge +5

Hi @Paul McLeod Bespoke Analytics 
There is no URL you can call to get the perilset_id? It would be more dynamic then as you could skip using the enum and just make a array of ids from calling this.

What I think you are missing is this.

<api:set attr="EnablePaging" value="true"/>

Otherwise it looks OK.

Badge

Thanks Thomas,

I had missed the EnablePaging Attribute my enum is working as I hoped now, still figuring out the RSD files!

I hadconsidered using a linked call, but this is a simple lookup table that I want to build.

I now have a different problem. I want to set the upper enum value high enough to cover all future values. But I get errors now when i go beyond the end of the table and get a Null result. 

I have tried using the <api:try> and <api:catch> and the <api:check> keywords, but I cannot get the CDATA driver to stop throwing and error once I hit an invalid URI (i.e. fall of the end of the table). 

Any suggestions as to how I can test safely if an item has been returned and avoid the exception?

Badge

Never mind! managed to figure it out. I was placing the <api:try> inside the <api:call> instead of wrappings the <api:call> in the <api:try>. Working like a charm now! 

Thanks for your help

Userlevel 6
Badge +5

Hi @Paul McLeod Bespoke Analytics 

Good you found it. If you don’t mind, would you please share the file with TRY CATCH in it. So others can see how it can be done.

You can’t link to RSD files, but if you add it as code people can see it. Of course remove any private info such as URLs and IDs.

Badge

Hi Thomas, here is the resultant RSD file that I created to iterate over an Enum set to step through a REST API interface that only implemented single indexed lookups to retrieve the entire table set.

Note that I set the range of my Enum purposefully well beyond the expected range, so I had to handle empty call exceptions.

RSD file to enum step through an indexed API call​

 

I also created an RSD file to grab in a field the actual date that I queried the API interface so that I can surface that in audit fields for reporting purposes.

 

Reply