Project

General

Profile

API » History » Version 34

« Previous - Version 34/72 (diff) - Next » - Current version
Maarten Plieger, 10/23/2015 11:22 AM


API

Access token API - service "account"

1). Generate a token for the current user which can be used to access C4I services by using "generatetoken":

The token has the following structure:

{ 
  "creationdate" : "2015-10-23T09:50:05Z",
  "notafter" : "2015-10-30T09:50:05Z",
  "notbefore" : "2015-10-23T09:50:05Z",
  "openid" : "https://esg-dn1.nsc.liu.se/esgf-idp/openid/<identifier>",
  "token" : "daad2944-1def-4bee-b032-b08048f88302", 
  "userid" : "esg-dn1.nsc.liu.se.esgf-idp.openid.<identifier>" 
}

2). List all tokens available for the current user by using "listtokens": Service endpoints which can be used with a token:
  • WMS can be used via
    https://dev.climate4impact.eu/impactportal/adagucserver/<accesstoken>/?
  • WCS can be used via
    https://dev.climate4impact.eu/impactportal/adagucserver/<accesstoken>/?
  • WPS can be used via
    https://dev.climate4impact.eu/impactportal/WPS/<accesstoken>/?
  • File download requests and OpenDAP requests from the basket can be done via
    https://dev.climate4impact.eu/impactportal/DAP/<accesstoken>/<userid>/<file>"
  • Basket requests can be done via
    https://dev.climate4impact.eu/impactportal/basket/<accesstoken>?&service=basket&request=getoverview&

Basket requests

  • Get overview of all files in the users basket: service=basket&request=getoverview,
    • e.g.
      https://dev.climate4impact.eu/impactportal/basket?&service=basket&request=getoverview&
    • For commandline access with an access token:
      https://dev.climate4impact.eu/impactportal/basket/<accesstoken>/?&service=basket&request=getoverview&

The result is a json file with nested childs representing te directory structure at C4I

{ "browser" : "/impactportal/data/catalogbrowser.jsp?",
  "children" : [ { "children" : [  ],
        "date" : "",
        "expanded" : true,
        "iconCls" : "typeFolder",
        "leaf" : false,
        "text" : "Remote data" 
      },
      { "children" : [ { "dapurl" : "https://localhost/impactportal/DAP/<userid>/INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
              "date" : "2014-06-06 14:31:23Z",
              "filesize" : "611.573K",
              "hasdap" : true,
              "hashttp" : true,
              "httpurl" : "https://localhost/impactportal/DAP/<userid>/INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
              "iconCls" : "typeOF",
              "id" : "INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
              "index" : 1,
              "leaf" : true,
              "text" : "INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
              "type" : "file" 
            },
            { "dapurl" : "https://localhost/impactportal/DAP/<userid>/tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
              "date" : "2015-10-23 09:37:33Z",
              "filesize" : "47.867M",
              "hasdap" : true,
              "hashttp" : true,
              "httpurl" : "https://localhost/impactportal/DAP/<userid>/tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
              "iconCls" : "typeOF",
              "id" : "tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
              "index" : 2,
              "leaf" : true,
              "text" : "tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
              "type" : "file" 
            }
          ],
        "date" : "",
        "expanded" : true,
        "iconCls" : "typeFolder",
        "leaf" : false,
        "text" : "My data" 
      }
    ],
  "leaf" : false,
  "text" : "https://esg-dn1.nsc.liu.se/esgf-idp/openid/<openid>",
  "viewer" : "/impactportal/data/datasetviewer.jsp?" 
}

WPS Requests

The following Python example behaves like a minimal WPS client, using C4I services to perform analysis. The example below makes use of the binaryoperatorfornumbers process, which enables operations on two numbers. The process has three inputs:
  1. inputa - a float number
  2. inputb - a float number
  3. operation - Can be "add", "substract", "divide" or "multiply"

The process lists progress for five seconds, this is done to enable progress monitoring. The process is executed asynchronously and is executed with the GET method. PyWPS also supports POST, for posting bigger inputs. This is described here: WPS

#!/bin/python

""" 
Obtain an access token:
- Login with a google account at https://dev.climate4impact.eu/impactportal/account/OAuth2.jsp 
- Generate via https://dev.climate4impact.eu/impactportal/tokenapi?service=account&request=generatetoken
- Copy paste the accesstoken value in the "token" variable below.
""" 

token="<access_token>" 

wpsendpoint="https://dev.climate4impact.eu/impactportal/WPS/"+token+"/?" 

inputa=11;
inputb=22;
operator="divide" # Any of add, substract, multiply, divide

print "Calculating ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"]" 

import xml.etree.ElementTree as et
import urllib2
import time

""" Parses URL to XML tree """ 
def getandparsexml(url):
  response = urllib2.urlopen(url)
  html = response.read()
  #print html
  return et.fromstring(html)

# Execute the ultimatequestionprocess aynchronousely and obtain the statusLocation for progress
datainputs = "inputa="+str(inputa)+";inputb="+str(inputb)+";operator="+str(operator)
root = getandparsexml(wpsendpoint+"service=WPS&version=1.0.0&request=execute&storeExecuteResponse=true&status=true&identifier=binaryoperatorfornumbers&datainputs="+datainputs)    
monitorURL = root.attrib['statusLocation']

print "Process executed checking " +monitorURL

# Get starttime of process, sometimes the monitorURL is not immediately available. Iterate until a valid creationtime is available would be good practice.
numberOfTries=10
while numberOfTries>0:
  numberOfTries = numberOfTries-1 
  try:
    root = getandparsexml(monitorURL)
    numberOfTries = -1
    print "Process started at "+str(root[1].attrib['creationTime'])
  except:
    time.sleep(1)
    pass

# Monitor progress, polling the statuslocation frequently
percentage=0
while percentage<100:
  root = getandparsexml(monitorURL)
  try:
    percentage = float(root[1][0].attrib['percentCompleted'])
  except:
    percentage=100.0
    pass
  print "Progress: "+str(percentage)+ " complete." 
  time.sleep(1)

# Obtain the answer  
root = getandparsexml(monitorURL)
try:
  print "The answer for ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"] is "+(root[2][0][2][0].text)
except:
  print "Processing failed: "+root[1][0][0][0][0].text
  pass

run_wps_from_client.py View - Runs a very simple WPS client on one of the services at climate4impact (2.86 KB) Maarten Plieger, 10/31/2016 03:14 PM