Private
Public Access
1
0

removing some duplicate code

This commit is contained in:
Sander Roosendaal
2018-06-26 12:14:00 +02:00
parent 2350d5aedf
commit 0152c8d20b
14 changed files with 99 additions and 333 deletions

View File

@@ -7,6 +7,7 @@ from django.conf import settings
import uuid
import datetime
import requests
lbstoN = 4.44822
@@ -398,3 +399,47 @@ def ewmovingaverage(interval,window_size):
interval2 = interval
return interval2
# Exceptions
# Custom error class - to raise a NoTokenError
class NoTokenError(Exception):
def __init__(self,value):
self.value=value
def __str__(self):
return repr(self.value)
# Custom exception handler, returns a 401 HTTP message
# with exception details in the json data
def custom_exception_handler(exc,message):
response = {
"errors": [
{
"code": str(exc),
"detail": message,
}
]
}
res = HttpResponse(message)
res.status_code = 401
res.json = json.dumps(response)
return res
def get_strava_stream(r,metric,stravaid,series_type='time',fetchresolution='high'):
authorizationstring = str('Bearer ' + r.stravatoken)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
'Content-Type': 'application/json',
'resolution': 'medium',}
url = "https://www.strava.com/api/v3/activities/{stravaid}/streams/{metric}?resolution={fetchresolutions}&series_type={series_type}".format(
stravaid=stravid,
fetchresolution=fetchresolution,
series_type=series_type,
metric=metric
)
return requests.get(url,headers=headers)