-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatapusher.py
190 lines (167 loc) · 5.77 KB
/
datapusher.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
import requests
import json
import datetime
class Datapusher:
"""Connection to ckan datastore"""
def __init__(self, global_settings, server="Staging"):
self.ckan_url = global_settings['URLs'][server]['CKAN']
self.dump_url = global_settings['URLs'][server]['Dump']
self.key = global_settings['API Keys'][server]
def resource_exists(self, packageid, resource_name):
"""
Searches for resource on ckan instance
:param packageid: id of resources parent dataset
:param resource_name: resources name
:return: true if found false otherwise
"""
check_resource = requests.post(
self.ckan_url + 'action/package_show',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'id': packageid
})
)
check = json.loads(check_resource.content)
# If no resources at all
if len(check['result']['resources']) == 0:
return False
# Check if this month's resource already exists
for resource in check['result']['resources']:
if resource['name'] == resource_name:
return True
return False
def create_resource(self, packageid, resource_name):
"""
Creates new resource in ckan instance
:param packageid: dataset under which to add new resource
:param resource_name: name of new resource
:return: id of newly created resource if successful
"""
# Make api call
create_resource = requests.post(
self.ckan_url + 'action/resource_create',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'package_id': packageid,
'url': '#',
'name': resource_name,
'url_type': 'datapusher',
'format': 'CSV'
})
)
resource = json.loads(create_resource.text)
if not resource['success']:
print(resource)
print("ERROR: " + resource['error']['name'][0])
return
print("SUCCESS: Resource #" + resource['result']['id'] + ' was created.')
return resource['result']['id']
def create_datastore(self, resource, fields, keys=None):
"""
Creates new datastore for specified resource
:param resource: resource id fo which new datastore is being made
:param fields: header fields for csv file
:return: resource id if successful
"""
# Make API call
datastore_creation = requests.post(
self.ckan_url + 'action/datastore_create',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'resource_id': resource,
'primary_key': keys,
'force': True,
'fields': fields
})
)
datastore = json.loads(datastore_creation.text)
if not datastore['success']:
print("ERROR: " + datastore['error']['name'][0])
return
print("SUCCESS: Datastore #" + datastore['result']['resource_id'] + ' was created.')
return datastore['result']
def delete_datastore(self, resource):
"""
Deletes datastore table for resource
:param resource: resource to remove table from
:return: request status
"""
delete = requests.post(
self.ckan_url + 'action/datastore_delete',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'resource_id': resource,
'force': True
})
)
return delete.status_code
def upsert(self, resource, data, method='insert'):
"""
Upsert data into datastore
:param resource: resource to which data will be inserted
:param data: data to be upserted
:return: request status
"""
insert = requests.post(
self.ckan_url + 'action/datastore_upsert',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'resource_id': resource,
'method': method,
'force': True,
'records': data
})
)
return insert
def update_meta_data(self, resource):
"""
TODO: Make this versatile
:param resource: resource whose metadata willbe modified
:return: request status
"""
update = requests.post(
self.ckan_url + 'action/resource_patch',
headers={
'content-type': 'application/json',
'authorization': self.key
},
data=json.dumps({
'id': resource,
'url': self.dump_url + resource,
'url_type': 'datapusher',
'last_modified': datetime.datetime.now().isoformat(),
})
)
return update.status_code
def resource_search(self, name):
"""
:param name:
:return:
"""
search = requests.post(
self.ckan_url + 'action/datastore_search',
headers={
'content-type': 'application/json',
'authorization':self.key
},
data=json.dumps({
"resource_id": name,
"plain": False
})
)
return search