-
Notifications
You must be signed in to change notification settings - Fork 6
/
activityinfo_client.py
executable file
·202 lines (157 loc) · 6.21 KB
/
activityinfo_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
# coding=utf-8
__author__ = 'jcranwellward'
import json
import os
import argparse
from urlparse import urljoin
from ConfigParser import SafeConfigParser
import requests
from requests.auth import HTTPBasicAuth
class ActivityInfoClient(object):
""" ActivityInfo python client to allow the following requests:
List all databases visible to the client: /databases
Retrieve the structure of database id 504: /database/504/schema
Retrieve all sites in activity 33: /sites?activity=33
Retrieve all sites in activity 33 in GeoJSON format: /sites/points?activity=33
List all countries: /countries
List all administrative levels in Lebanon (country code LB): /country/LB/adminLevels
List all administrative entities in level 1370: /adminLevel/1370/entities
List all administrative entities in level 1370 in GeoJSON format: /adminLevel/1370/entities/features
List all location types in Lebanon: /country/LB/locationTypes
List all locations of type 1370: /locations?type=1370
"""
def __init__(self,
username=None,
password=None,
base_url='https://www.activityinfo.org/'):
self.base_url = base_url
if username and password:
self.auth = HTTPBasicAuth(username, password)
def build_path(self, path=None):
""" Builds the full path to the service.
Args:
path (string): The part of the path you want to append
to the base url.
Returns:
A string containing the full path to the endpoint.
e.g if the base_url was "http://woo.com" and the path was
"databases" it would return "http://woo.com/databases/"
"""
if path is None:
return self.base_url
return urljoin(
self.base_url, os.path.normpath(path),
)
def make_request(self, path, **params):
response = requests.get(
self.build_path(path),
params=params,
auth=getattr(self, 'auth', ()),
)
return response
def call_command(self, type, **properties):
payload = json.dumps(
{
'type': type,
'command': {
'properties': properties
}
}
)
response = requests.post(
self.build_path('command'),
headers={'content-type': 'application/json'},
auth=getattr(self, 'auth', ()),
data=payload,
)
return response
def get_databases(self):
return self.make_request('resources/databases').json()
def get_database(self, db_id):
return self.make_request('resources/database/{}/schema'.format(db_id)).json()
def get_sites(self, database=None, partner=None, activity=None, indicator=None, attribute=None):
sites = self.make_request(
'resources/sites',
database=database,
partner=partner,
activity=activity,
indicator=indicator,
attribute=attribute).json()
return sites
def get_cube(self, form_ids, month=None):
return self.make_request(
'resources/sites/cube?'
'dimension=indicator'
'&dimension=site'
'&dimension=month'
'{}'
'&form={}'.format(
'&month='+month if month is not None else '',
'&form='.join([str(id) for id in form_ids])
)).json()
def get_monthly_reports_for_site(self, site_id):
return self.make_request('resources/sites/{}/monthlyReports'.format(site_id)).json()
def get_countries(self):
return self.make_request('resources/countries').json()
def get_admin_levels(self, country):
return self.make_request('resources/country/{}/adminLevels'.format(country)).json()
def get_location_types(self, country):
return self.make_request('resources/country/{}/locationTypes'.format(country)).json()
def get_entities(self, level_id):
return self.make_request('resources/adminLevel/{}/entities'.format(level_id)).json()
def get_locations(self, type_id):
return self.make_request('resources/locations', type=type_id).json()
def main():
"""
Main method for command line usage
"""
parser = argparse.ArgumentParser(
description='ActivityInfo API Python Client'
)
group = parser.add_mutually_exclusive_group()
group.add_argument('-d', '--database',
type=int,
help='Database to query')
group.add_argument('-a', '--activity',
type=int,
help='Filter results by activty')
parser.add_argument('-p', '--partner',
type=int,
default=None,
help='Filter results by partner')
parser.add_argument('-i', '--indicator',
type=int,
default=None,
help='Filter results by indicator')
parser.add_argument('-U', '--username',
type=str,
default='',
help='Optional username for authentication')
parser.add_argument('-P', '--password',
type=str,
default='',
help='Optional password for authentication')
args = parser.parse_args()
try:
parser = SafeConfigParser()
parser.read('settings.ini')
username = parser.get('auth', 'user')
password = parser.get('auth', 'pass')
client = ActivityInfoClient(
username=username or args.username,
password=password or args.password,
)
if args.database:
response = client.get_database(args.database)
elif args.activity:
response = client.get_sites(args.partner,
args.activity,
args.indicator)
else:
response = client.get_sites()
print response
except Exception as exp:
print str(exp)
if __name__ == '__main__':
main()