-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
164 lines (137 loc) · 6.05 KB
/
api.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
import requests
class MetadataServiceException(Exception):
'''Indicates a request to uFrame's Metadata Service API failed.'''
def __init__(self, status_code):
message = 'Metadata Service request failed with status: {0}'.format(status_code)
super(MetadataServiceException, self).__init__(message)
self.status_code = status_code
class MetadataServiceAPI(object):
'''Class used to communicate with uFrame's Metadata Service API.'''
def __init__(self, stream_url, partition_url):
self.__stream_url = stream_url
self.__partition_url = partition_url
##################
# Common Methods #
##################
@staticmethod
def __get_json(url):
response = requests.get(url)
if (response.status_code == requests.codes.ok):
return response.json()
else:
raise MetadataServiceException(response.status_code)
@staticmethod
def __post_json(url, json):
response = requests.post(url, json=json)
if (response.status_code == requests.codes.created):
return response.json()
else:
raise MetadataServiceException(response.status_code)
@staticmethod
def __put_json(url, json):
response = requests.put(url, json=json)
if (response.status_code == requests.codes.ok):
return response.json()
else:
raise MetadataServiceException(response.status_code)
@staticmethod
def __delete_json(url):
response = requests.delete(url)
if (response.status_code == requests.codes.ok):
return response.json()
else:
raise MetadataServiceException(response.status_code)
@staticmethod
def build_stream_metadata_record(subsite, node, sensor, method, stream, first, last, count):
return {
'@class': '.StreamMetadataRecord',
'referenceDesignator': {
'subsite': str(subsite),
'node': str(node),
'sensor': str(sensor)
},
'method': str(method),
'stream': str(stream),
'first': float(first),
'last': float(last),
'count': long(count)
}
@staticmethod
def build_partition_metadata_record(subsite, node, sensor, method, stream, bin, store, first, last, count):
return {
'@class': '.PartitionMetadataRecord',
'referenceDesignator': {
'subsite': str(subsite),
'node': str(node),
'sensor': str(sensor)
},
'method': str(method),
'stream': str(stream),
'bin': long(bin),
'store': str(store),
'first': float(first),
'last': float(last),
'count': long(count)
}
##################################
# Stream Metadata Record Methods #
##################################
def get_stream_metadata_records(self):
return self.__get_json(self.__stream_url)
def get_stream_metadata_record(self, subsite, node, sensor, method, stream):
url = '/'.join((self.__stream_url, 'inv', subsite, node, sensor, method, stream))
try:
return self.__get_json(url)
except MetadataServiceException, e:
if (e.status_code == requests.codes.not_found):
return None
raise e
def create_stream_metadata_record(self, stream_metadata_record):
return self.__post_json(self.__stream_url, stream_metadata_record)
def index_stream_metadata_record(self, stream_metadata_record):
url = '/'.join((self.__stream_url, 'index'))
return self.__put_json(url, stream_metadata_record)
def delete_stream_metadata_records(self, subsite, node, sensor):
url = '/'.join((self.__stream_url, 'inv', subsite, node, sensor))
return self.__delete_json(url)
def delete_stream_metadata_record(self, subsite, node, sensor, method, stream):
url = '/'.join((self.__stream_url, 'inv', subsite, node, sensor, method, stream))
try:
return self.__delete_json(url)
except MetadataServiceException, e:
if (e.status_code == requests.codes.not_found):
return None
raise e
#####################################
# Partition Metadata Record Methods #
#####################################
def get_partition_metadata_records(self, subsite, node, sensor, method=None, stream=None):
if method and stream:
url = '/'.join((self.__partition_url, 'inv', subsite, node, sensor, method, stream))
else:
url = '/'.join((self.__partition_url, 'inv', subsite, node, sensor))
return self.__get_json(url)
def get_partition_metadata_record(self, subsite, node, sensor, method, stream, bin, store):
url = '/'.join((self.__partition_url, 'inv', subsite, node, sensor, method, stream, str(bin), store))
try:
return self.__get_json(url)
except MetadataServiceException, e:
if (e.status_code == requests.codes.not_found):
return None
raise e
def create_partition_metadata_record(self, partition_metadata_record):
return self.__post_json(self.__partition_url, partition_metadata_record)
def index_partition_metadata_record(self, partition_metadata_record):
url = '/'.join((self.__partition_url, 'index'))
return self.__put_json(url, partition_metadata_record)
def delete_partition_metadata_records(self, subsite, node, sensor):
url = '/'.join((self.__partition_url, 'inv', subsite, node, sensor))
return self.__delete_json(url)
def delete_partition_metadata_record(self, subsite, node, sensor, method, stream, bin, store):
url = '/'.join((self.__partition_url, 'inv', subsite, node, sensor, method, stream, str(bin), store))
try:
return self.__delete_json(url)
except MetadataServiceException, e:
if (e.status_code == requests.codes.not_found):
return None
raise e