Skip to content

Commit 7e44175

Browse files
Merge pull request #13 from tfeseker/create_single_observation
Create single observation
2 parents 962285c + 6025b85 commit 7e44175

5 files changed

Lines changed: 27 additions & 20 deletions

File tree

frost_sta_client/dao/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def create(self, entity):
110110
e.response.status_code,
111111
error_message))
112112
raise e
113-
entity.id = int(frost_sta_client.utils.extract_value(response.headers['location']))
113+
entity.id = frost_sta_client.utils.extract_value(response.headers['location'])
114114
entity.service = self.service
115115
logging.debug('Received response: ' + str(response.status_code))
116116

frost_sta_client/dao/observation.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@ def __init__(self, service):
3434
"""
3535
base.BaseDao.__init__(self, service, EntityTypes["Observation"])
3636

37-
def create(self, data_array):
38-
url = self.service.url.copy()
39-
url.path.add(self.CREATE_OBSERVATIONS)
40-
logging.debug('Posting to ' + str(url.url))
41-
json_dict = transform_entity_to_json_dict(data_array.value)
42-
try:
43-
response = self.service.execute('post', url, json=json_dict)
44-
except requests.exceptions.HTTPError as e:
45-
error_json = e.response.json()
46-
error_message = error_json['message']
47-
logging.error("Creating {} failed with status-code {}, {}".format("Data Array",
37+
def create(self, entity):
38+
if isinstance(entity, frost_sta_client.model.observation.Observation):
39+
super().create(entity)
40+
else:
41+
# entity is probably a data array
42+
url = self.service.url.copy()
43+
url.path.add(self.CREATE_OBSERVATIONS)
44+
logging.debug('Posting to ' + str(url.url))
45+
json_dict = transform_entity_to_json_dict(entity.value)
46+
try:
47+
response = self.service.execute('post', url, json=json_dict)
48+
except requests.exceptions.HTTPError as e:
49+
error_json = e.response.json()
50+
error_message = error_json['message']
51+
logging.error("Creating {} failed with status-code {}, {}".format("Data Array",
4852
e.response.status_code,
4953
error_message))
50-
response_text_as_list = json.loads(response.text)
51-
result = [frost_sta_client.model.observation.Observation(self_link=link) for link in response_text_as_list]
52-
return result
54+
response_text_as_list = json.loads(response.text)
55+
result = [frost_sta_client.model.observation.Observation(self_link=link) for link in response_text_as_list]
56+
return result

frost_sta_client/model/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def id(self, value):
4242
if isinstance(value, int) or isinstance(value, str):
4343
self._id = value
4444
return
45-
raise ValueError('id of entity should be of type int!')
45+
raise ValueError('id of entity should be of type int or str!')
4646

4747
@property
4848
def self_link(self):

frost_sta_client/model/location.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def things(self):
141141
@things.setter
142142
def things(self, values):
143143
if values is None:
144-
self._thing = None
144+
self._things = None
145145
return
146146
if isinstance(values, list) and all(isinstance(th, thing.Thing) for th in values):
147147
entity_class = entity_type.EntityTypes['Thing']['class']
148-
self._thing = entity_list.EntityList(entity_class=entity_class, entities=values)
148+
self._things = entity_list.EntityList(entity_class=entity_class, entities=values)
149149
return
150150
if not isinstance(values, entity_list.EntityList) or \
151151
any((not isinstance(th, thing.Thing)) for th in values.entities):

frost_sta_client/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525

2626
def extract_value(location):
27-
return location[location.find('(')+1: location.find(')')]
28-
27+
try:
28+
value = int(location[location.find('(')+1: location.find(')')])
29+
except ValueError:
30+
value = str(location[location.find('(')+2: location.find(')')-1])
31+
return value
2932

3033
def transform_entity_to_json_dict(entity):
3134
json_str = jsonpickle.encode(entity, unpicklable=False)

0 commit comments

Comments
 (0)