Skip to content

Commit

Permalink
Merge pull request #11 from gisce/refactor_amon_class
Browse files Browse the repository at this point in the history
Refactor amon class
  • Loading branch information
ecarreras committed Oct 13, 2014
2 parents c60ff77 + a1f37cf commit 1ad6478
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 454 deletions.
21 changes: 12 additions & 9 deletions empowering/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def delete(self, obj, etag):
class AmonMeasuresMeasurements(EmpoweringResource):
path = 'amon_measures_measurements'

@base.apimethod
def get(self):
raise base.MethodNotSupported

@base.apimethod
def create(self, obj):
raise base.MethodNotSupported
Expand All @@ -73,19 +69,26 @@ class Empowering(base.Resource):
"""
Empowering Insight Engine Service API.
"""
def __init__(self, company_id, key_file, cert_file, version='v1'):
self.company_id = company_id
def __init__(self, company_id, key_file=None, cert_file=None, version='v1',
debug=False):
self.company_id = str(company_id)
self.key_file = key_file
self.cert_file = cert_file
endpoint = "https://api.empowering.cimne.com"
if debug:
endpoint = "http://91.121.140.152:5111"
self.apiroot = '%s/%s' % (endpoint, version)
self.add_filter(self.use_json)
self.add_filter(self.add_company_id)

extra_handlers = ()
# We have to use SSL Client
https_handler = HTTPSClientAuthHandler(self.key_file, self.cert_file)
filter_handler = HTTPEmpoweringFilterHandler()
urllib2_executor.use(extra_handlers=(https_handler, filter_handler))
if self.key_file and self.cert_file:
extra_handlers += (
HTTPSClientAuthHandler(self.key_file, self.cert_file),
)
extra_handlers += (HTTPEmpoweringFilterHandler(), )
urllib2_executor.use(extra_handlers=extra_handlers)

def use_json(self, request):
if request.method.upper() not in http.URLENCODE_METHODS:
Expand Down
27 changes: 26 additions & 1 deletion empowering/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from datetime import datetime
import uuid
import times
from arrow.parser import DateTimeParser


# Monkey patch parse function for times library (newer)
parser = DateTimeParser()
times.parse = parser.parse_iso


def remove_none(struct, context=None):
Expand Down Expand Up @@ -59,4 +66,22 @@ def make_uuid(model, model_id):
def make_utc_timestamp(timestamp, timezone='Europe/Madrid'):
if not timestamp:
return None
return times.to_universal(timestamp, timezone).isoformat('T') + 'Z'
return times.to_universal(timestamp, timezone).isoformat('T') + 'Z'


def make_local_timestamp(timestamp, timezone='Europe/Madrid'):
if not timestamp:
return None
if isinstance(timestamp, basestring):
timestamp = times.parse(timestamp.replace('Z', ''))
return times.to_local(timestamp, timezone).strftime('%Y-%m-%d %H:%M:%S')


def datestring_to_epoch(date_string):
if not date_string:
return None
if not isinstance(date_string, datetime):
dt = datetime.strptime(date_string, '%Y-%m-%d')
else:
dt = date_string
return dt.strftime('%s')
Loading

0 comments on commit 1ad6478

Please sign in to comment.