FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 525412ac authored by J.W. Smith's avatar J.W. Smith
Browse files

feat: Example survey download from WRSIS

The method in this example may become the official method of downoading Ethiopia wheat rust surveys. Based on proposed API provided by CSM on 23 September 2020.
parent 5b7486a6
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ scratch*
logs/
*.log
ODK-Briefcase-v*.jar
Cred-WRSIS-API.json
Cred_gmail.json
MO_FTP_Cred.json
ssh_key_willow
......
#WRSIS-API-test.py
'''Test retrieval following the CSM example API document. '''
# requests is a module to interact with http servers
import requests
# json is to read the restricted credentials
import json
gis_url = 'http://197.156.117.171/WRSIS/gisapi/getRustSurveyDataForGlobalRust'
cred_fn = 'Cred-WRSIS-API.json'
# load credentials from restricted file
with open(cred_fn,'r') as cred_file:
cred_dict = json.load(cred_file)
# The API document says these parameters are mandatory
# example dates as provided in the document
date_params = {
'fromDate':'10-06-2020',
'toDate':'22-06-2020'}
# set up http session
session = requests.Session()
# provide authorisation
# Surprisingly, this script seems to behave the same if session.auth is not provided
#session.auth = (cred_dict['Username'], cred_dict['Password'])
# post is a type of HTTP interaction command
# mandatory parameters are passed as the 'json' argument
r = session.post(gis_url,json=date_params)
# possible HTTP responses as provided in the API document
# (I've seen some other responses though, e.g. 415)
# It seems there is another layer of status codes
status_codes = {
200 : 'OK',
201 : 'Created',
202 : 'Accepted (Request accepted, and queued for execution)',
400 : 'Bad request',
401 : 'Authentication failure',
403 : 'Forbidden',
404 : 'Resource not found',
405 : 'Method Not Allowed',
409 : 'Conflict',
412 : 'Precondition Failed',
413 : 'Request Entity Too Large',
500 : 'Internal Server Error',
501 : 'Not Implemented',
503 : 'Service Unavailable'}
# checking the HTTP status code (not the code in the repsonse)
if r.status_code == 200:
print('HTTP request succeeded OK')
print('Response is')
# extract the dict from the responses
# r.content is 'bytes' type. Extraction depends on versions of python modules
response = json.loads(str(r.content)[2:-1])
print(json.dumps(response,indent=2))
else:
print("HTTP response did not succeed OK, code is {:s}: {:s} ".format(r.status_code,status_codes[r.status_code]))
print('Finished script')
#!/bin/bash
# provide custom python packages so they can be imported
export PYTHONPATH=$PYTHONPATH:/storage/app/EWS/General/EWS-python/custom_modules/flagdir/
# activate conda environment of python modules so they can be imported
source /storage/app/miniconda3/bin/activate /storage/app/EWS/General/EWS-python/py3EWSepi
python /storage/app/EWS/General/EWS-Coordinator-Dev/WRSIS-API-test.py
# deactivate conda environment
source /storage/app/miniconda3/bin/deactivate /storage/app/EWS/General/EWS-python/py3EWSepi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment