This example returns the Location hierarchy from the 'MOBP' ('Mobile Platforms') node and below, and prints the locationCode and Name, indented for each tree level. If the locationCode parameter is excluded, the entire Oceans 3.0 Tree structure is returned. Parameters: import requests
import json
def getLocationChild(location,level):
locationCode = location['locationCode']
locationName = location['locationName']
children = location['children']
sTab = " ".join(["" for r in range(0,level)])
print('{0}{1} - {2}'.format(sTab,locationCode,locationName))
if (children):
for child in children:
getLocationChild(child,level+1)
url = 'https://data.oceannetworks.ca/api/locations'
parameters = {'method':'getTree',
'token':'YOUR_TOKEN_HERE', # replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at https://data.oceannetworks.can/Profile when logged in.
'locationCode':'MOBP'}
response = requests.get(url,params=parameters)
if (response.ok):
print('Locations')
locations = json.loads(str(response.content,'utf-8'))
for location in locations:
locationCode = location['locationCode']
locationName = location['locationName']
children = location['children']
print('{0} - {1}'.format(locationCode,locationName))
if (children):
for child in children:
getLocationChild(child,2)
else:
if(response.status_code == 400):
error = json.loads(str(response.content,'utf-8'))
print(error) # json response contains a list of errors, with an errorMessage and parameter
else:
print ('Error {} - {}'.format(response.status_code,response.reason))
|
getLocationsHierarchy()
function getLocationsHierarchy()
url = ['https://data.oceannetworks.ca/api/locations?' ...
'&method=getTree' ...
'&token=YOUR_TOKEN_HERE']; %replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at https://data.oceannetworks.can/Profile when logged in.
'&locationCode=MOBP'];
request = matlab.net.http.RequestMessage;
uri = matlab.net.URI(url);
options = matlab.net.http.HTTPOptions('ConnectTimeout',60);
response = send(request,uri,options);
if (response.StatusCode == 200) % HTTP Status - OK
locations = response.Body.Data;
for i=1:numel(locations)
location = locations(i);
disp(sprintf('%s - %s',location.locationCode,location.locationName));
if (numel(location.children) >= 1)
for c=1:numel(location.children)
getLocationChild(location.children(c),1);
end
end
end
elseif (response.StatusCode == 400) % HTTP Status - Bad Request
disp(response.Body.Data.errors);
else % all other HTTP Statuses
disp(char(response.StatusLine));
end
end
function getLocationChild(location,level)
sTab = '';
for t=1:level
sTab = sprintf('\t %s',sTab);
end
disp(sprintf('%s %s - %s',sTab, location.locationCode,location.locationName));
if (numel(location.children) >= 1)
for c=1:numel(location.children)
getLocationChild(location.children(c),level + 1);
end
end
end |
library(httr)
getChildLocation <- function(location,level){
sTab = ""
for (t in 1:level){
sTab = sprintf(" %s",sTab)
}
cat(sprintf("%s%s - %s\n",sTab, location$locationCode,location$locationName))
children=location$children
newLevel=level+1
for (child in children) {
getChildLocation(child,newLevel)
}
return()
}
r <- GET("https://data.oceannetworks.ca/api/locations",
query = list(method="getTree",
token="b6ede000-1865-4ac3-94ad-e87d8bdfd307", #>replace YOUR_TOKEN_HERE with your personal token obtained from the 'Web Services API' tab at https://data.oceannetworks.can/Profile when logged in.
locationCode="MOBP"))
if (http_error(r)) {
if (r$status_code == 400){
error = content(r)
str(error)
} else {
str(http_status(r)$message)
}
} else {
locations = content(r)
for (location in locations){
cat(sprintf("%s - %s\n",location$locationCode,location$locationName))
children = location$children
for (child in children) {
getChildLocation(child,1)
}
}
} |
|