diff --git a/empowering/__init__.py b/empowering/__init__.py index d2307eb..9b7d351 100644 --- a/empowering/__init__.py +++ b/empowering/__init__.py @@ -1,7 +1,7 @@ try: VERSION = __import__('pkg_resources') \ .get_distribution(__name__).version -except Exception, e: +except Exception as e: VERSION = 'unknown' from service import Empowering diff --git a/empowering/executors/urllib2_executor.py b/empowering/executors/urllib2_executor.py index 927e642..23c8f5c 100644 --- a/empowering/executors/urllib2_executor.py +++ b/empowering/executors/urllib2_executor.py @@ -7,8 +7,8 @@ logger = logging.getLogger('empowering.executors.urllib2_executor') -API_HOST = 'api.empowering.cimne.com' -DEBUG_API_HOST = '91.121.140.152' +API_HOST = 'api.beedataanalytics.com' +DEBUG_API_HOST = 'devapi.beedataanalytics.com' class HTTPSClientAuthHandler(urllib2.HTTPSHandler): diff --git a/empowering/resource.py b/empowering/resource.py index 72a5799..d5019e3 100644 --- a/empowering/resource.py +++ b/empowering/resource.py @@ -20,10 +20,13 @@ def delete(self, etag): return request, parsers.parse_json @base.apimethod - def get(self, where=None, sort=None): + def get(self, where=None, sort=None, rid=None): sort = sort and sort.replace(' ', '') params = base.get_params(('where', 'sort'), locals()) - request = http.Request('GET', self.get_url(), params) + url = self.get_url() + if rid: + url = '{}/{}'.format(url, rid) + request = http.Request('GET', url, params) return request, parsers.parse_json diff --git a/empowering/service.py b/empowering/service.py index 0b22be8..f2c938e 100644 --- a/empowering/service.py +++ b/empowering/service.py @@ -26,10 +26,6 @@ class Contracts(EmpoweringResource): path = 'contracts' - def wrap_object(self, obj): - serializer = models.Contract() - return serializer.dump(obj).data - class AmonMeasures(EmpoweringResource): path = 'amon_measures' @@ -47,6 +43,14 @@ def delete(self, obj, etag): raise base.MethodNotSupported +class ResidentialTimeofuseAmonMeasures(AmonMeasures): + path = 'residential_timeofuse_amon_measures' + + +class TertiaryAmonMeasures(AmonMeasures): + path = 'tertiary_amon_measures' + + class AmonMeasuresMeasurements(EmpoweringResource): path = 'amon_measures_measurements' @@ -72,6 +76,22 @@ def __init__(self, *args, **kwargs): DeprecationWarning) +class Tariffs(EmpoweringResource): + path = 'raw_tariffs' + + +class PriceIndexed(EmpoweringResource): + path = 'price_indexed' + + @base.apimethod + def update(self, obj, _id): + raise base.MethodNotSupported + + @base.apimethod + def delete(self, obj, _id): + raise base.MethodNotSupported + + class Empowering(base.Resource): """ Empowering Insight Engine Service API. @@ -123,6 +143,13 @@ def add_cookie_token(self, request): def get_url(self): return "{0}/{1}".format(self.apiroot, self.version) + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if self.login_handler and self.token: + self.logout() + def login(self, user, password): if self.login_handler and self.token: return {'success': True, 'token': self.token} @@ -152,6 +179,14 @@ def logout(self): def amon_measures(self): return AmonMeasures(self) + @base.resource(ResidentialTimeofuseAmonMeasures) + def residential_timeofuse_amon_measures(self): + return ResidentialTimeofuseAmonMeasures(self) + + @base.resource(TertiaryAmonMeasures) + def tertiary_amon_measures(self): + return TertiaryAmonMeasures(self) + @base.resource(Measures) def measures(self): warn('Deprecated. Use amon_measures_measurements', DeprecationWarning) @@ -169,6 +204,14 @@ def contract(self, contract_id): def contracts(self): return Contracts(self) + @base.resource(Tariffs) + def tariffs(self): + return Tariffs(self) + + @base.resource(PriceIndexed) + def price_indexed(self): + return PriceIndexed(self) + @base.resource(OT101Results) def ot101_result(self, result_id): return OT101Results(self, result_id) diff --git a/empowering/utils.py b/empowering/utils.py index 7ea4f55..16e6466 100644 --- a/empowering/utils.py +++ b/empowering/utils.py @@ -4,6 +4,7 @@ import times import ssl from arrow.parser import DateTimeParser +import os # Monkey patch parse function for times library (newer) @@ -89,8 +90,11 @@ def make_uuid(model, model_id): model = model.encode('utf-8') if isinstance(model_id, unicode): model_id = model_id.encode('utf-8') - token = '%s,%s' % (model, model_id) - return str(uuid.uuid5(uuid.NAMESPACE_OID, token)) + if int(os.getenv('ANONIMIZE', '1')): + token = '%s,%s' % (model, model_id) + return str(uuid.uuid5(uuid.NAMESPACE_OID, token)) + else: + return str(model_id) def make_utc_timestamp(timestamp, timezone='Europe/Madrid'):