-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__py
133 lines (111 loc) · 4.83 KB
/
__init__py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
#
# Copyright 2011 HubSpot Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = "markitecht (Christopher O'Donnell)"
import re
import hmac
import base64
import urllib
import httplib
import logging
from xml.dom import minidom
HEALTHGRAPH_API_BASE = 'https://api.runkeeper.com'
HEALTHGRAPH_AUTH_URL = 'https://runkeeper.com/apps/authorize'
HEALTHGRAPG_TOKEN_URL = 'https://runkeeper.com/apps/token'
try:
import hashlib
except ImportError:
import md5 as hashlib
try:
import json as simplejson
simplejson.loads
except (ImportError, AttributeError):
try:
import simplejson
simplejson.loads
except (ImportError, AttributeError):
try:
from django.utils import simplejson
simplejson.loads
except (ImportError, AttributeError):
try:
import jsonlib as simplejson
simplejson.loads
except:
pass
class HealthGraphClient(object):
'''Client for interacting with the Health Graph APIs'''
def __init__(self, access_token):
self.access_token = access_token
def _create_path(self, method):
pass
def _http_error(self, code, message, url):
logging.error('Client request error. Code: %s - Reason: %s // %s - URL: %s' % (str(code), message['message'], message['detail'], url))
def _prepare_response(self, code, data):
msg = self._get_msg(code)
if data:
try:
data = simplejson.loads(data)
except ValueError:
pass
return {'status': code, 'body': data or {}, 'msg': msg}
def _get_msg(self, code):
messages = {
'200': {'message': 'OK', 'detail': 'Success!'},
'304': {'message': 'Not Modified', 'detail': 'The requested resource has not been modified since the time included in the If-Modified-Since request header.'},
'400': {'message': 'Bad Request', 'detail': 'The request could not be understood due to malformed syntax.'},
'401': {'message': 'Unauthorized', 'detail': 'Authentication credentials were missing or incorrect.'},
'403': {'message': 'Forbidden', 'detail': 'The request is understood but it has been refused, either due to insufficient permission or to a rate limit violation.'},
'404': {'message': 'Not Found', 'detail': 'The URI requested is invalid or the resource requested, such as a user, does not exist.'},
'405': {'message': 'Method Not Allowed', 'detail': 'The URI requested does not support the specific HTTP method used in the request.'},
'406': {'message': 'Not Acceptable', 'detail': 'The URI requested does not support any of the MIME types given in the Accept header.'},
'415': {'message': 'Unsupported Media Type', 'detail': 'The URI requested cannot accept the MIME type given in the Content-Type header.'},
'500': {'message': 'Internal Server Error', 'detail': 'Something is broken. Please contact support so the RunKeeper team can investigate.'},
'502': {'message': 'Bad Gateway', 'detail': 'RunKeeper is down or being upgraded.'},
'503': {'message': 'Service Unavailable', 'detail': 'The RunKeeper servers are up, but overloaded with requests. Try again later.'}
}
return messages[code]
def _deal_with_content_type(self, output):
if output == "atom" or output == "xml":
return "atom+xml"
return "json"
def _make_request(self, resource, params, content_type, media_type=None, data=None, request_method='GET', url=None):
client = httplib.HTTPSConnection(HEALTHGRAPH_API_BASE)
if data and not isinstance(data, str):
data = urllib.urlencode(data)
if url is None:
url = resource
headers = {
'Content-Type': content_type,
'Authorization': 'Bearer #%s' % self.access_token
}
if media_type:
headers['Accept'] = media_type
client.request(request_method, url, data, headers)
result = client.getresponse()
if result.status < 400:
body = result.read()
client.close()
if body:
return self._prepare_response(result.status, body)
else:
return self._prepare_response(result.status, None)
else:
client.close()
self._http_error(result.status, result.reason, url)
return self._prepare_response(result.status, {})
def get_user(self, user_id):
media_type = 'application/vnd.com.runkeeper.User+json'
return self._make_request('/user', media_type = media_type)