Currently, we can use a variation on the following code to pull data, but it could be easier to use and pull more data each pull.
Approach 1: work on the partially built {fossAPI}
R package:
It would be great to develop a clean, easy way to pull data from the FOSS data platform, like Alex Richardson's partially built FOSS API pull package.
API R packages that are already being used for other data streams that could be an example for our needs
Approach 2: loop through data content:
To loop through data from an API, you want to set up a loop that exits when the api call returns hasMore = false
. In the loop, you will adjust your offset (e.g. offset = offset + limit), make another api call, then append the returned items to an items variable.
If you look at the output from https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/ you'll see the following. hasMore
tells you there is more data. offset
tells the API where to start to retrieve the next page of data.
"hasMore": true,
"limit": 25,
"offset": 0,
"count": 25,
So to get the next page of data, set offset = 25
(e.g., https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/?offset=25). Also notice that there's a links section that tells you what the next page of data says offset = 50
. In your code, instead of having to keep track of what record you're on, you can just use the element 5 from the links array to request the next chunk of data.
"links": [
{
"rel": "self",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"
},
{
"rel": "edit",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"
},
{
"rel": "describedby",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/metadata-catalog/afsc_groundfish_survey/"
},
{
"rel": "first",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"
},
{
"rel": "next",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/?offset=50"
},
{
"rel": "prev",
"href": "https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"
}
]
cc'ing @Lewis-Barnett-NOAA and @MargaretSiple-NOAA, who would also be interested in this development.