Title: | Oceans 2.0 API Client Library |
Description: | Allows users to discover and retrieve Ocean Networks Canada's oceanographic data in raw, text, image, audio, video or any other format available. Provides a class that wraps web service calls and business logic so that users can download data with a single line of code. |
Version: | 2.0.1.0 |
URL: | https://wiki.oceannetworks.ca/display/O2A/Oceans+2.0+API+Home |
License: | MIT + file LICENSE |
Imports: | anytime, httr, methods, humanize, lubridate, stringi, tictoc, crayon, utils, testthat |
Encoding: | UTF-8 |
RoxygenNote: | 7.1.1 |
NeedsCompilation: | no |
Packaged: | 2021-08-20 15:34:53 UTC; blm |
Author: | Dany Cabrera [aut], Allan Rempel [ctb], Ryan Ross [ctb], Bennit Mueller [ctb, cre] |
Maintainer: | Bennit Mueller <data@oceannetworks.ca> |
Repository: | CRAN |
Date/Publication: | 2021-08-21 09:40:07 UTC |
Helper that appends a list item to a list of lists
Description
Helper that appends a list item to a list of lists
Usage
.appendList(li = list(), item = list())
Arguments
li |
The container list |
item |
The list to append to li as a new element |
Value
The modified list
Given a runId, polls the "download" method to count the number of files available Uses HTTP HEAD to avoid downloading the files
Description
Given a runId, polls the "download" method to count the number of files available Uses HTTP HEAD to avoid downloading the files
Usage
.countFilesInProduct(self, runId = 0)
Arguments
self |
Caling object |
runId |
Run id returned by .runDataProduct() |
Value
(numeric) Number of files available for download
Discovery Request encapsulates a request to a discovery service
Description
Discovery Request encapsulates a request to a discovery service
Usage
.discoveryRequest(self, filters = list(), service = "", method = "get")
Arguments
self |
Calling object |
filters |
Named list of filters |
service |
One of "locations", "deployments", "deviceCategories", "devices", "properties", "dataProducts" |
method |
One of "get", "getTree" |
Value
(named list) Parsed response obtained
Generic request wrapper for making simple web service requests Will return parsed results, even if they are an error description sent by the API Will stop() if the request fails into an error different from 400
Description
Generic request wrapper for making simple web service requests Will return parsed results, even if they are an error description sent by the API Will stop() if the request fails into an error different from 400
Usage
.doRequest(
self,
url = "",
filters = list(),
getInfo = FALSE,
rawResponse = FALSE
)
Arguments
self |
Calling object |
url |
Full URL to make the request, without GET parameters |
filters |
GET parameters |
getInfo |
When TRUE, will return a list with ("response", "duration", "status"), where "duration" is the time the request took and "status" is the HTTP response status |
rawResponse |
When TRUE, the response returned is the unparsed object returned by httr::GET, otherwise a parsed named list is returned |
Value
(list) The parsed response
Public wrapper that lets a user download data products manually with a runId Can optionally return just the download links
Description
Public wrapper that lets a user download data products manually with a runId Can optionally return just the download links
Usage
.downloadDataProduct(
self,
runId = 0,
maxRetries = 0,
downloadResultsOnly = FALSE,
includeMetadataFile = TRUE,
overwrite = FALSE
)
Arguments
self |
Calling object |
runId |
(numeric) Run ID as provided by runDataProduct() |
maxRetries |
(numeric) Maximum number of API requests allowed, 0 for no limit |
downloadResultsOnly |
(logical) When TRUE, files are not downloaded By default (FALSE) generated files are downloaded |
includeMetadataFile |
(logical) When TRUE, a metadata file is downloaded, otherwise it is skipped |
overwrite |
(logical) When TRUE downloaded files will overwrite any file with the same filename, otherwise they will be skipped |
Value
(list) A list of results (one named list for each file) with information on the operation outcome
Download all data product files for provided run id
Description
Download all data product files for provided run id
Usage
.downloadProductFiles(
self,
runId = 0,
getMetadata = TRUE,
maxRetries = 0,
overwrite = TRUE,
fileCount = 0
)
Arguments
self |
Caling object |
runId |
Run id returned by .runDataProduct() |
getMetadata |
When TRUE, the metadata file will be downloaded |
maxRetries |
(numeric) Maximum number of API requests allowed, 0 for no limit |
overwrite |
(logical) When TRUE downloaded files will overwrite any file with the same filename, otherwise they will be skipped |
fileCount |
The number of files to download, or 0 if unknown |
Value
a list of named lists, with the results for each file
Sets a poll period adequate to the estimated processing time Longer processing times require longer poll periods to avoid going over maxRetries
Description
Sets a poll period adequate to the estimated processing time Longer processing times require longer poll periods to avoid going over maxRetries
Usage
.estimatePollPeriod(self, response)
Arguments
self |
Calling object |
response |
Response obtained in .requestDataProduct() for the DP request |
Value
(numeric) suggested time between server polls (seconds)
Filter file list results to only those where the filename ends with the extension If extension is empty no change will be made
Description
Filter file list results to only those where the filename ends with the extension If extension is empty no change will be made
Usage
.filterByExtension(self, results = list(), extension = "")
Arguments
self |
Calling object |
results |
Results as otained by getListByLocation() or getListByDevice() |
extension |
(character) Extension to search for (i.e. "txt") |
Value
Filtered list
Returns a string URL for a base url and a named list of parameters
Description
Returns a string URL for a base url and a named list of parameters
Usage
.filters2url(url, params)
Arguments
url |
Base url string |
params |
Named list of parameters |
Value
Full URL
Returns a formatted time duration string representation of a duration in seconds
Description
Returns a formatted time duration string representation of a duration in seconds
Usage
.formatDuration(secs = 0)
Arguments
secs |
(double) Number of seconds |
Value
(character) A readable string for this time duration
Aggregates individual download results obtained in .orderDataProduct() into a list of formatted results to return, and a named list with general stats of the operation
Description
Aggregates individual download results obtained in .orderDataProduct() into a list of formatted results to return, and a named list with general stats of the operation
Usage
.formatResult(self, fileList = list(), runInfo = list())
Arguments
self |
Calling object |
fileList |
List of individual download results |
runInfo |
As returned by .runDataProduct() |
Value
A named list with "downloadResults" (list of download results) and "stats" (general stats for the full operation)
Returns a formatted file size string representation
Description
Returns a formatted file size string representation
Usage
.formatSize(size = 0)
Arguments
size |
(float) Size in bytes |
Value
(character) a readable string for this file size
Format date as ISO8601 UTC
Description
Helper that returns an ISO8601 string for the provided date string Most date formats are supported, as explained in: https://github.com/eddelbuettel/anytime A value of "now" returns the current UTC date & time Depends on the local system clock
Usage
.formatUtc(self, dateString = "now")
Arguments
self |
Calling object |
dateString |
A string that describes a date. Can also be "now" |
Value
(character) Date string
Request a list of data products that matches the filters provided
Description
Request a list of data products that matches the filters provided
Usage
.getDataProducts(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of data products found, or an error description if any
Request a list of deployments that matches the filters provided
Description
Request a list of deployments that matches the filters provided
Usage
.getDeployments(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of deployments found, or an error description if any
Request a list of device categories that matches the filters provided
Description
Request a list of device categories that matches the filters provided
Usage
.getDeviceCategories(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of device categories found, or an error description if any
Request a list of devices that matches the filters provided
Description
Request a list of devices that matches the filters provided
Usage
.getDevices(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of devices found, or an error description if any
Generic method to download and concatenate all pages of data Keeps downloading all scalar or raw data pages until finished Automatically translates sensorCategoryCodes to a string if a list is provided
Description
Generic method to download and concatenate all pages of data Keeps downloading all scalar or raw data pages until finished Automatically translates sensorCategoryCodes to a string if a list is provided
Usage
.getDirectAllPages(
self,
filters = list(),
service = "",
method = "",
allPages = FALSE
)
Arguments
self |
Calling object |
filters |
(named list) Filters that describe the data origin |
service |
One of: "scalardata", "rawdata" |
method |
One of: "getByDevice", "getByLocation" |
allPages |
When TRUE, if the data requested is too large to fit a single API resquest, keep downloading data pages until we gather all data |
Value
A single response in the expected format, with all data pages concatenated
Obtains scalar data from a device, as described by the filters
Description
Obtains scalar data from a device, as described by the filters
Usage
.getDirectByDevice(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) Filters that describe the data origin |
allPages |
When TRUE, if the data requested is too large to fit a single API resquest, keep downloading data pages until we gather all data |
Value
(named list) Scalar data obtained for all sensors found
Obtains scalar data from a location, from the source described by the filters
Description
Obtains scalar data from a location, from the source described by the filters
Usage
.getDirectByLocation(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) Filters that describe the data origin |
allPages |
When TRUE, if the data requested is too large to fit a single API resquest, keep downloading data pages until we gather all data |
Value
(named list) Scalar data obtained for all sensors found
Downloads all archive files that match the filters Internally will use geListByDevice or getListByLocation and getFile all files
Description
Downloads all archive files that match the filters Internally will use geListByDevice or getListByLocation and getFile all files
Usage
.getDirectFiles(self, filters = list(), overwrite = FALSE, allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) describe the data origin |
overwrite |
When TRUE, downloaded files will overwrite any file with the same filename, otherwise file will be skipped |
allPages |
When TRUE, if the data is too long to fit a single request, multiple pages will be requested until all data is obatined |
Value
(named list) Information on the results of the operation, with "downloadResults" for each file downloaded and general "stats"
Obtains raw data from a device, as described by the filters
Description
Obtains raw data from a device, as described by the filters
Usage
.getDirectRawByDevice(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) Filters that describe the data origin |
allPages |
When TRUE, if the data requested is too large to fit a single API resquest, keep downloading data pages until we gather all data |
Value
(named list) Raw data obtained for all sensors found
Obtains raw data from a location, from the source described by the filters
Description
Obtains raw data from a location, from the source described by the filters
Usage
.getDirectRawByLocation(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) Filters that describe the data origin |
allPages |
When TRUE, if the data requested is too large to fit a single API resquest, keep downloading data pages until we gather all data |
Value
(named list) Raw data obtained for all sensors found
Given a filename, returns an archivefile absolute download URL
Description
Given a filename, returns an archivefile absolute download URL
Usage
.getDownloadUrl(self, filename = "")
Arguments
self |
Calling object |
filename |
(character) archive file name |
Value
(character) download URL
Download the archive file with filename
Description
Download the archive file with filename
Usage
.getFile(self, filename = "", overwrite = FALSE)
Arguments
self |
Calling object |
filename |
Archive file filename |
overwrite |
When TRUE, downloaded files will overwrite any file with the same filename, otherwise file will be skipped |
Value
(named list) Information on the download result
A generic wrapper for getListByLocation() and getListByDevice()
Description
A generic wrapper for getListByLocation() and getListByDevice()
Usage
.getList(self, filters = list(), by = "location", allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) describe the data origin |
by |
One of: "location", "device" |
allPages |
When TRUE, if the data is too long to fit a single request, multiple pages will be requested until all data is obatined |
Value
(named list) Information on the list of files obtained
Get a list of files available in Oceans 2.0 Archiving System for a given device code. The list of filenames can be filtered by time range. filtered by others optional parameters.
Description
Get a list of files available in Oceans 2.0 Archiving System for a given device code. The list of filenames can be filtered by time range. filtered by others optional parameters.
Usage
.getListByDevice(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) describe the data origin |
allPages |
When TRUE, if the data is too long to fit a single request, multiple pages will be requested until all data is obatined |
Value
(named list) file list obtained
Get a list of files for a given location code and device category code, and filtered by others optional parameters.
Description
Get a list of files for a given location code and device category code, and filtered by others optional parameters.
Usage
.getListByLocation(self, filters = list(), allPages = FALSE)
Arguments
self |
Calling object |
filters |
(named list) describe the data origin |
allPages |
When TRUE, if the data is too long to fit a single request, multiple pages will be requested until all data is obatined |
Value
(named list) file list obtained
Request a hierarchical tree list of locations that matches the filters provided
Description
Request a hierarchical tree list of locations that matches the filters provided
Usage
.getLocationHierarchy(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of locations found, or an error description if any Locations include lists of children locations recursively
Request a list of locations that matches the filters provided
Description
Request a list of locations that matches the filters provided
Usage
.getLocations(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of locations found, or an error description if any
Request a list of properties that matches the filters provided
Description
Request a list of properties that matches the filters provided
Usage
.getProperties(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) search filters |
Value
(named list) A list of properties found, or an error description if any
Returns a list of information lists for each file available for download Returned rows will have the same structure as those returned by DataProductFile$getInfo()
Description
Returns a list of information lists for each file available for download Returned rows will have the same structure as those returned by DataProductFile$getInfo()
Usage
.infoForProductFiles(self, dpRunId = 0, fileCount = 0, getMetadata = FALSE)
Arguments
self |
Caling object |
dpRunId |
Run id returned by .runDataProduct() |
fileCount |
The number of files to download, or 0 if unknown |
getMetadata |
When TRUE, the metadata file will be included |
Value
a list of named lists, with the results for each file
R helper: Returns the last element of a list
Description
R helper: Returns the last element of a list
Usage
.lastItem(collection)
Arguments
collection |
The list |
Value
The last element of the list
Prints message to console only when showInfo is true
Description
Prints message to console only when showInfo is true
Usage
.log(self, msg = "")
Arguments
self |
Calling object |
msg |
(character) The message to print |
Request, run and download a data product as described by the filters
Description
Request, run and download a data product as described by the filters
Usage
.orderDataProduct(
self,
filters = list(),
maxRetries = 0,
downloadResultsOnly = FALSE,
metadata = TRUE,
overwrite = FALSE
)
Arguments
self |
Caling object |
filters |
(named list) Filters that describe this data product |
maxRetries |
(numeric) Total maximum number of request calls allowed, 0 for no limit |
downloadResultsOnly |
(logical) When TRUE, files are not downloaded By default (FALSE) generated files are downloaded |
metadata |
(logical) When TRUE, a metadata file is downloaded, otherwise it is skipped |
overwrite |
(logical) When TRUE downloaded files will overwrite any file with the same filename, otherwise they will be skipped |
Value
(list) A list of results (one named list for each file) with information on the operation outcome
Creates directory if it does not exist Supports directory paths with or without filenames
Description
Creates directory if it does not exist Supports directory paths with or without filenames
Usage
.prepareDirectory(filepath = "")
Arguments
filepath |
(character) Directory path |
Value
(logical) TRUE if the directory was created or already exists
Pretty prints a complex hierarchy of lists
Description
Pretty prints a complex hierarchy of lists
Usage
.prettyPrint(item, name = "", level = 0, comma = FALSE, file = "")
Arguments
item |
Hierarchy item (can be a list or an atomic element) |
name |
(character) Item name if its an element in a named list |
level |
(numeric) Depth level in the hierarchy, starting at 0 |
comma |
(logical) If TRUE, a comma is printed after this element |
file |
A file connection to an open file if we should print to it, or "" (no file, print to console) |
Helper for printing a JSON named list to the console Can alternatively print the output to a text file at filePath
Description
Helper for printing a JSON named list to the console Can alternatively print the output to a text file at filePath
Usage
.print(self, obj, filePath = "")
Arguments
self |
self |
obj |
named list to print (usually the result of another function) |
filePath |
(string) if present, creates the file and writes the output in it |
Print Error Response Message
Description
Prints the information from a API error response to the console as a formatted error message
Usage
.printErrorMessage(response)
Arguments
response |
Parsed response returned by httr::GET |
Prints a formatted representation of the total time and size downloaded after the product order finishes
Description
Prints a formatted representation of the total time and size downloaded after the product order finishes
Usage
.printProductOrderStats(self, fileList = list(), runInfo = list())
Arguments
self |
Calling object |
fileList |
(list) As returned by .downloadProductFiles() |
runInfo |
(list) As returned by .runDataProduct() |
Prints the response after a data product request The request response format might differ depending on the product origin as it can be "assembled" on the fly, or reused from existing products
Description
Prints the response after a data product request The request response format might differ depending on the product origin as it can be "assembled" on the fly, or reused from existing products
Usage
.printProductRequest(self, response)
Arguments
self |
Calling object |
response |
Parsed httr response |
Request a data product generation described by the filters
Description
Request a data product generation described by the filters
Usage
.requestDataProduct(self, filters = list())
Arguments
self |
Calling object |
filters |
(named list) Filters that describe this data product |
Value
Parsed httr response
Response has failed
Description
Returns TRUE if the response is a 401 JSON error description provided by the API
Usage
.respFailed(response)
Arguments
response |
(named list) Parsed httr response |
Value
(logical)
Run a data product generation request
Description
Run a data product generation request
Usage
.runDataProduct(self, dpRequestId = 0, waitComplete = FALSE)
Arguments
self |
Calling object |
dpRequestId |
(numeric) Request id obtained by requestDataProduct() |
waitComplete |
(logical) When true, will keep polling the server until the data product is ready to download |
Value
(named list) information of the run process, or error object
For all rows in data, enforce that fields expected to have bool values are logical values (fixes API issues with booleans returned as strings)
Description
For all rows in data, enforce that fields expected to have bool values are logical values (fixes API issues with booleans returned as strings)
Usage
.sanitizeBooleans(self, data = list())
Arguments
self |
Calling object |
data |
(list) Parsed response |
Value
(list) The modified data
Saves the file downloaded in the response object, in the outPath, with filename
Description
Saves the file downloaded in the response object, in the outPath, with filename
Usage
.saveAsFile(response, filePath = "", fileName = "", overwrite = FALSE)
Arguments
response |
An http raw response as returned by httr::GET |
filePath |
Path where the file will be saved |
fileName |
Name of the file to save |
overwrite |
If TRUE will overwrite files with the same name |
Value
(numeric) Result code from 0: done, -1: error, -2: fileExists
Returns the absolute url for a given ONC API service
Description
Returns the absolute url for a given ONC API service
Usage
.serviceUrl(self, service = "")
Arguments
self |
Calling object |
service |
One of ONC's API services |
Value
(character) Service's URL
DataProductFile class
Description
Encapsulates the download of a single data product file, and the handling of the server polling and error codes.
Arguments
dpRunId |
Run Id of a data product request that was run |
index |
Index of this object's file |
baseUrl |
From the Onc class |
token |
From the Onc class Downloads this data product file Can poll, wait and retry if the file is not ready to download |
overwrite |
When TRUE, existing files will be overwritten, otherwise they are skipped |
timeout , pollPeriod , outPath , maxRetries |
Same as in the Onc class |
response |
The successful (200) httr response obtained from a download request |
Value
(integer) the final response's HTTP status code Return the file name from the response
(string) The filename as obtained from the headers Return information on this download's outcome
(list) A named list with information on the download result Sets this object's status to 200 (complete) Used by onc_delivery methods
Fields
.filters
The list of filters to pass to the download API method
.retries
Total count of HTTP requests made by this object
.status
Last request's HTTP status code
.downloaded
TRUE if the file was downloaded from the API
.baseUrl
Same as in the Onc class
.downloadUrl
URL that downloads this file
.fileName
Filename of the file if downloaded & saved
.fileSize
Size in bytes of the file if downloaded & saved
.runningTime
Total time spent running (sec)
.downloadingTime
Total time spend downloading (sec) Class initializer
MultiPage
Description
API multiple page helper. Handles the download of data in multiple pages Used by real-time and archivefile services
Arguments
extension: |
Only provide for archivefiles filtering by filename extension |
nextResponse |
(named list) Response with the next data page to add |
responseTime: |
(double) Request duration in seconds |
response |
(named list) Response with the first page |
service |
(character) One of: "scalardata", "rawdata", "archivefiles" |
Value
: Response with concatenated data for all pages obtained Do a page request
Wraps the _doRequest method Performs additional processing of the response for certain services
(list) A named list with: "response" (httr response) and "duration" (secs) Concatenate data
Concatenates the data results from nextResponse into response Compatible with the row structure of scalardata, rowdata and archivefiles
(named list) Modified original response Estimate pages
Estimates the number of pages this request will require to download, from the first page's response and its duration
(int) Estimated number of pages Response Time Span
Determines the timespan covered by the data in the response
(int) page duration in seconds
Fields
showInfo
As provided by the Onc class
timeout
As provided by the Onc class Class initializer Get all pages
Obtains all data pages for a query with the filters Multiple pages will be downloaded in sequence until completed Each new page gets concatenated to the previous one (limited to RAM)
Onc Class
Description
Provides convenient & easy access to Ocean Networks Canada's data. For detailed information and usage examples, visit our official Documentation.
Arguments
token |
User token |
production |
whether the ONC Production server URL is used for service requests |
showInfo |
Whether verbose debug messages are displayed |
outPath |
Output path for downloaded files |
timeout |
Number of seconds before a request to the API is canceled |
Fields
token
character. User token
showInfo
logical. Print verbose debug comments
timeout
numeric. Number of seconds before a request to the API is canceled
baseUrl
character. Base URL for API requests
outPath
character. Output path for downloaded files
Class initializer
Methods
downloadDataProduct( runId = 0, maxRetries = 0, downloadResultsOnly = FALSE, includeMetadataFile = TRUE, overwrite = FALSE )
Manually download a data product after it was requested and run
formatUtc(dateString = "now")
Formats the provided date string to meet ISO8601
getDataProducts(filters = list())
Returns a filtered list of data products
getDeployments(filters = list())
Returns a filtered list of deployments
getDeviceCategories(filters = list())
Returns a filtered list of device categories
getDevices(filters = list())
Returns a filtered list of devices
getDirectByDevice(filters = list(), allPages = FALSE)
Obtain scalar data readings from a device
getDirectByLocation(filters = list(), allPages = FALSE)
Obtain scalar data readings from a device category in a location
getDirectFiles(filters = list(), overwrite = FALSE, allPages = FALSE)
Download a list of archived files that match the filters provided
getDirectRawByDevice(filters = list(), allPages = FALSE)
Obtain raw data readings from a device
getDirectRawByLocation(filters = list(), allPages = FALSE)
Obtain raw data readings from a device category in a location
getFile(filename = "", overwrite = FALSE)
Download a file with the given filename
getListByDevice(filters = list(), allPages = FALSE)
Get a list of archived files for a device
getListByLocation(filters = list(), allPages = FALSE)
Get a list of archived files for a device category in a location
getLocationHierarchy(filters = list())
Returns a filtered locations tree
getLocations(filters = list())
Returns a filtered list of locations
getProperties(filters = list())
Returns a filtered list of properties
orderDataProduct( filters = list(), maxRetries = 0, downloadResultsOnly = FALSE, includeMetadataFile = TRUE, overwrite = FALSE )
Request, run and download a data product
print(data, filename = "")
Prints a named list in a format easier to read
requestDataProduct(filters = list())
Manually Request a data product
runDataProduct(dpRequestId = 0, waitComplete = FALSE)
Manually run a data product request