Skip to content

Commit 53c431d

Browse files
author
Thomas Basche
committed
Add query methods
1 parent 8ccce80 commit 53c431d

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ name = "pypi"
99
requests = "*"
1010
arrow = "*"
1111
six = "*"
12+
pendulum = "*"

reposit/data/api.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55

66
import requests
7+
import pendulum
78

89
from reposit.data.exceptions import InvalidControllerException
910
from reposit.data.utils import is_valid_url, deepest_key, match_to_schema
@@ -64,11 +65,6 @@ def __init__(self, path, controller, schema, **kwargs):
6465

6566
# a lookup of the response schema
6667
self.schema = schema
67-
"""
68-
Various optional args here
69-
"""
70-
# if the response a list?
71-
self.is_list = kwargs.get('format_list', False)
7268

7369
def get(self):
7470
"""
@@ -91,6 +87,31 @@ def get(self):
9187
data = self._simple_format_for_fields(resp)
9288
return data
9389

90+
def query(self, start, end=None):
91+
"""
92+
Similar to get() but with specified query parameters.
93+
:param start: unix timestamp (start of query)
94+
:param end: unix timestamp (end of query). If not specified
95+
then this defaults to now()
96+
:return:
97+
"""
98+
if not end:
99+
end = pendulum.now().int_timestamp
100+
101+
resp = requests.get(
102+
'{}?start={}&end={}'.format(self.url, start, end),
103+
headers=self.controller.auth_headers
104+
)
105+
try:
106+
resp.raise_for_status()
107+
except Exception as ex:
108+
# Hijack the exception to log the exact issue.
109+
logger.exception('Error retrieving data: {}'.format(self))
110+
raise ex
111+
112+
data = self._simple_format_for_fields(resp)
113+
return {'data': data[0]} # because this is a list of lists
114+
94115
def _simple_format_for_fields(self, api_response):
95116
"""
96117
Based on the schema provided, format the data accordingly.

reposit/data/controller.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ def has_inverter(self):
8080
)
8181
return request.get()
8282

83+
def get_historical_generation(self, start, end):
84+
"""
85+
Given a start and end timestamp return the generation
86+
data.
87+
:param start: unix timestamp
88+
:param end: unix timestamp
89+
:return: list of lists of data
90+
"""
91+
request = ApiRequest(
92+
path='v2/deployments/{}/generation/historical/p'.format(self.user_key),
93+
controller=self,
94+
schema={
95+
'solarP': {}
96+
}
97+
)
98+
return request.query(start, end)
99+
83100
@property
84101
def latest_historical_generation(self):
85102
"""
@@ -95,6 +112,23 @@ def latest_historical_generation(self):
95112
)
96113
return request.get()
97114

115+
def get_historical_house(self, start, end):
116+
"""
117+
Given a start and end timestamp return the house
118+
data.
119+
:param start: unix timestamp
120+
:param end: unix timestamp
121+
:return: list of lists of data
122+
"""
123+
request = ApiRequest(
124+
path='v2/deployments/{}/house/historical'.format(self.user_key),
125+
controller=self,
126+
schema={
127+
'houseP': {}
128+
}
129+
)
130+
return request.query(start, end)
131+
98132
@property
99133
def latest_historical_house(self):
100134
"""
@@ -112,6 +146,23 @@ def latest_historical_house(self):
112146
)
113147
return request.get()
114148

149+
def get_historical_grid_credits(self, start, end):
150+
"""
151+
Given a start and end timestamp return the gridcredits
152+
data.
153+
:param start: unix timestamp
154+
:param end: unix timestamp
155+
:return: list of lists of data
156+
"""
157+
request = ApiRequest(
158+
path='v2/deployments/{}/gridcredits/historical'.format(self.user_key),
159+
controller=self,
160+
schema={
161+
'gridcredits': {}
162+
}
163+
)
164+
return request.query(start, end)
165+
115166
@property
116167
def latest_historical_grid_credits(self):
117168
"""
@@ -128,6 +179,23 @@ def latest_historical_grid_credits(self):
128179
)
129180
return request.get()
130181

182+
def get_historical_inverter(self, start, end):
183+
"""
184+
Given a start and end timestamp return the inverter
185+
data.
186+
:param start: unix timestamp
187+
:param end: unix timestamp
188+
:return: list of lists of data
189+
"""
190+
request = ApiRequest(
191+
path='v2/deployments/{}/inverter/historical/p'.format(self.user_key),
192+
controller=self,
193+
schema={
194+
'inverterP': {}
195+
}
196+
)
197+
return request.query(start, end)
198+
131199
@property
132200
def latest_historical_inverter(self):
133201
"""
@@ -143,6 +211,23 @@ def latest_historical_inverter(self):
143211
)
144212
return request.get()
145213

214+
def get_historical_meter(self, start, end):
215+
"""
216+
Given a start and end timestamp return the meter
217+
data.
218+
:param start: unix timestamp
219+
:param end: unix timestamp
220+
:return: list of lists of data
221+
"""
222+
request = ApiRequest(
223+
path='v2/deployments/{}/meter/historical/p'.format(self.user_key),
224+
controller=self,
225+
schema={
226+
'meterP': {}
227+
}
228+
)
229+
return request.query(start, end)
230+
146231
@property
147232
def latest_historical_meter(self):
148233
"""

0 commit comments

Comments
 (0)