Project

General

Profile

API » History » Version 34

Maarten Plieger, 10/23/2015 11:22 AM

1 1 Maarten Plieger
h1. API
2 1 Maarten Plieger
3 28 Maarten Plieger
h2. Access token API - service "account"
4 1 Maarten Plieger
5 28 Maarten Plieger
*1). Generate a token for the current user which can be used to access C4I services by using "generatetoken":*
6 33 Maarten Plieger
* https://dev.climate4impact.eu/impactportal/tokenapi?service=account&request=generatetoken
7 1 Maarten Plieger
8 17 Maarten Plieger
The token has the following structure:
9 17 Maarten Plieger
<pre><code class="json">
10 18 Maarten Plieger
{ 
11 18 Maarten Plieger
  "creationdate" : "2015-10-23T09:50:05Z",
12 17 Maarten Plieger
  "notafter" : "2015-10-30T09:50:05Z",
13 17 Maarten Plieger
  "notbefore" : "2015-10-23T09:50:05Z",
14 22 Maarten Plieger
  "openid" : "https://esg-dn1.nsc.liu.se/esgf-idp/openid/<identifier>",
15 24 Maarten Plieger
  "token" : "daad2944-1def-4bee-b032-b08048f88302", 
16 22 Maarten Plieger
  "userid" : "esg-dn1.nsc.liu.se.esgf-idp.openid.<identifier>"
17 17 Maarten Plieger
}
18 17 Maarten Plieger
</code></pre>
19 17 Maarten Plieger
20 28 Maarten Plieger
*2). List all tokens available for the current user by using "listtokens":*
21 33 Maarten Plieger
* https://dev.climate4impact.eu/impactportal/tokenapi?service=account&request=listtokens
22 1 Maarten Plieger
23 29 Maarten Plieger
*Service endpoints which can be used with a token:*
24 33 Maarten Plieger
* WMS can be used via <pre>https://dev.climate4impact.eu/impactportal/adagucserver/<accesstoken>/?</pre>
25 33 Maarten Plieger
* WCS can be used via <pre>https://dev.climate4impact.eu/impactportal/adagucserver/<accesstoken>/?</pre>
26 33 Maarten Plieger
* WPS can be used via <pre>https://dev.climate4impact.eu/impactportal/WPS/<accesstoken>/?</pre>
27 33 Maarten Plieger
* File download requests and OpenDAP requests from the basket can be done via <pre>https://dev.climate4impact.eu/impactportal/DAP/<accesstoken>/<userid>/<file>"</pre>
28 33 Maarten Plieger
* Basket requests can be done via <pre>https://dev.climate4impact.eu/impactportal/basket/<accesstoken>?&service=basket&request=getoverview&</pre>
29 1 Maarten Plieger
30 5 Maarten Plieger
h2. Basket requests
31 1 Maarten Plieger
32 2 Maarten Plieger
* Get overview of all files in the users basket: service=basket&request=getoverview, 
33 33 Maarten Plieger
** e.g. <pre>https://dev.climate4impact.eu/impactportal/basket?&service=basket&request=getoverview&</pre>
34 33 Maarten Plieger
** For commandline access with an access token: <pre>https://dev.climate4impact.eu/impactportal/basket/<accesstoken>/?&service=basket&request=getoverview&</pre>
35 7 Maarten Plieger
36 14 Maarten Plieger
The result is a json file with nested childs representing te directory structure at C4I
37 10 Maarten Plieger
<pre><code class="json">
38 12 Maarten Plieger
{ "browser" : "/impactportal/data/catalogbrowser.jsp?",
39 12 Maarten Plieger
  "children" : [ { "children" : [  ],
40 12 Maarten Plieger
        "date" : "",
41 12 Maarten Plieger
        "expanded" : true,
42 12 Maarten Plieger
        "iconCls" : "typeFolder",
43 12 Maarten Plieger
        "leaf" : false,
44 12 Maarten Plieger
        "text" : "Remote data"
45 1 Maarten Plieger
      },
46 13 Maarten Plieger
      { "children" : [ { "dapurl" : "https://localhost/impactportal/DAP/<userid>/INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
47 12 Maarten Plieger
              "date" : "2014-06-06 14:31:23Z",
48 12 Maarten Plieger
              "filesize" : "611.573K",
49 1 Maarten Plieger
              "hasdap" : true,
50 12 Maarten Plieger
              "hashttp" : true,
51 13 Maarten Plieger
              "httpurl" : "https://localhost/impactportal/DAP/<userid>/INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
52 12 Maarten Plieger
              "iconCls" : "typeOF",
53 12 Maarten Plieger
              "id" : "INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
54 12 Maarten Plieger
              "index" : 1,
55 12 Maarten Plieger
              "leaf" : true,
56 1 Maarten Plieger
              "text" : "INTER_OPER_R___RD3_____L3__20100101T000000_20100201T000000_0004.nc",
57 12 Maarten Plieger
              "type" : "file"
58 12 Maarten Plieger
            },
59 13 Maarten Plieger
            { "dapurl" : "https://localhost/impactportal/DAP/<userid>/tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
60 1 Maarten Plieger
              "date" : "2015-10-23 09:37:33Z",
61 12 Maarten Plieger
              "filesize" : "47.867M",
62 12 Maarten Plieger
              "hasdap" : true,
63 12 Maarten Plieger
              "hashttp" : true,
64 13 Maarten Plieger
              "httpurl" : "https://localhost/impactportal/DAP/<userid>/tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
65 12 Maarten Plieger
              "iconCls" : "typeOF",
66 12 Maarten Plieger
              "id" : "tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
67 12 Maarten Plieger
              "index" : 2,
68 12 Maarten Plieger
              "leaf" : true,
69 12 Maarten Plieger
              "text" : "tasmax_day_CNRM-CM5_historical_r1i1p1_1890.nc",
70 12 Maarten Plieger
              "type" : "file"
71 12 Maarten Plieger
            }
72 12 Maarten Plieger
          ],
73 12 Maarten Plieger
        "date" : "",
74 12 Maarten Plieger
        "expanded" : true,
75 12 Maarten Plieger
        "iconCls" : "typeFolder",
76 1 Maarten Plieger
        "leaf" : false,
77 12 Maarten Plieger
        "text" : "My data"
78 12 Maarten Plieger
      }
79 12 Maarten Plieger
    ],
80 12 Maarten Plieger
  "leaf" : false,
81 13 Maarten Plieger
  "text" : "https://esg-dn1.nsc.liu.se/esgf-idp/openid/<openid>",
82 11 Maarten Plieger
  "viewer" : "/impactportal/data/datasetviewer.jsp?"
83 11 Maarten Plieger
}
84 10 Maarten Plieger
</code>
85 9 Maarten Plieger
</pre>
86 30 Maarten Plieger
87 30 Maarten Plieger
h2. WPS Requests
88 30 Maarten Plieger
89 34 Maarten Plieger
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:
90 34 Maarten Plieger
# inputa - a float number
91 34 Maarten Plieger
# inputb - a float number
92 34 Maarten Plieger
# operation -  Can be "add", "substract", "divide" or "multiply"
93 34 Maarten Plieger
94 34 Maarten Plieger
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]] 
95 34 Maarten Plieger
96 30 Maarten Plieger
<pre><code class="python">
97 30 Maarten Plieger
#!/bin/python
98 30 Maarten Plieger
99 30 Maarten Plieger
"""
100 30 Maarten Plieger
Obtain an access token:
101 33 Maarten Plieger
- Login with a google account at https://dev.climate4impact.eu/impactportal/account/OAuth2.jsp 
102 30 Maarten Plieger
- Generate via https://dev.climate4impact.eu/impactportal/tokenapi?service=account&request=generatetoken
103 32 Maarten Plieger
- Copy paste the accesstoken value in the "token" variable below.
104 31 Maarten Plieger
"""
105 30 Maarten Plieger
106 30 Maarten Plieger
token="<access_token>" 
107 30 Maarten Plieger
 
108 30 Maarten Plieger
109 33 Maarten Plieger
wpsendpoint="https://dev.climate4impact.eu/impactportal/WPS/"+token+"/?"
110 30 Maarten Plieger
111 30 Maarten Plieger
inputa=11;
112 30 Maarten Plieger
inputb=22;
113 30 Maarten Plieger
operator="divide" # Any of add, substract, multiply, divide
114 30 Maarten Plieger
115 30 Maarten Plieger
print "Calculating ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"]"
116 30 Maarten Plieger
117 30 Maarten Plieger
import xml.etree.ElementTree as et
118 30 Maarten Plieger
import urllib2
119 30 Maarten Plieger
import time
120 30 Maarten Plieger
121 30 Maarten Plieger
""" Parses URL to XML tree """
122 30 Maarten Plieger
def getandparsexml(url):
123 30 Maarten Plieger
  response = urllib2.urlopen(url)
124 30 Maarten Plieger
  html = response.read()
125 30 Maarten Plieger
  #print html
126 30 Maarten Plieger
  return et.fromstring(html)
127 30 Maarten Plieger
  
128 30 Maarten Plieger
# Execute the ultimatequestionprocess aynchronousely and obtain the statusLocation for progress
129 30 Maarten Plieger
datainputs = "inputa="+str(inputa)+";inputb="+str(inputb)+";operator="+str(operator)
130 30 Maarten Plieger
root = getandparsexml(wpsendpoint+"service=WPS&version=1.0.0&request=execute&storeExecuteResponse=true&status=true&identifier=binaryoperatorfornumbers&datainputs="+datainputs)    
131 30 Maarten Plieger
monitorURL = root.attrib['statusLocation']
132 30 Maarten Plieger
133 30 Maarten Plieger
print "Process executed checking " +monitorURL
134 30 Maarten Plieger
135 30 Maarten Plieger
# Get starttime of process, sometimes the monitorURL is not immediately available. Iterate until a valid creationtime is available would be good practice.
136 30 Maarten Plieger
numberOfTries=10
137 30 Maarten Plieger
while numberOfTries>0:
138 30 Maarten Plieger
  numberOfTries = numberOfTries-1 
139 30 Maarten Plieger
  try:
140 30 Maarten Plieger
    root = getandparsexml(monitorURL)
141 30 Maarten Plieger
    numberOfTries = -1
142 30 Maarten Plieger
    print "Process started at "+str(root[1].attrib['creationTime'])
143 30 Maarten Plieger
  except:
144 30 Maarten Plieger
    time.sleep(1)
145 30 Maarten Plieger
    pass
146 30 Maarten Plieger
147 30 Maarten Plieger
# Monitor progress, polling the statuslocation frequently
148 30 Maarten Plieger
percentage=0
149 30 Maarten Plieger
while percentage<100:
150 30 Maarten Plieger
  root = getandparsexml(monitorURL)
151 30 Maarten Plieger
  try:
152 30 Maarten Plieger
    percentage = float(root[1][0].attrib['percentCompleted'])
153 30 Maarten Plieger
  except:
154 30 Maarten Plieger
    percentage=100.0
155 30 Maarten Plieger
    pass
156 30 Maarten Plieger
  print "Progress: "+str(percentage)+ " complete."
157 30 Maarten Plieger
  time.sleep(1)
158 30 Maarten Plieger
159 30 Maarten Plieger
# Obtain the answer  
160 30 Maarten Plieger
root = getandparsexml(monitorURL)
161 30 Maarten Plieger
try:
162 30 Maarten Plieger
  print "The answer for ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"] is "+(root[2][0][2][0].text)
163 30 Maarten Plieger
except:
164 30 Maarten Plieger
  print "Processing failed: "+root[1][0][0][0][0].text
165 30 Maarten Plieger
  pass
166 30 Maarten Plieger
</code>
167 30 Maarten Plieger
</pre>