Skip to content

Commit 4684e2e

Browse files
committed
Adding Mashape support
1 parent 1e2dce9 commit 4684e2e

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

imgurpython/client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .imgur.models.account_settings import AccountSettings
1818

1919
API_URL = 'https://api.imgur.com/'
20+
MASHAPE_URL = 'https://imgur-apiv3.p.mashape.com/'
2021

2122

2223
class AuthWrapper(object):
@@ -72,10 +73,11 @@ class ImgurClient(object):
7273
'album', 'name', 'title', 'description'
7374
}
7475

75-
def __init__(self, client_id, client_secret, access_token=None, refresh_token=None):
76+
def __init__(self, client_id, client_secret, access_token=None, refresh_token=None, mashape_key=None):
7677
self.client_id = client_id
7778
self.client_secret = client_secret
7879
self.auth = None
80+
self.mashape_key = mashape_key
7981

8082
if refresh_token is not None:
8183
self.auth = AuthWrapper(access_token, refresh_token, client_id, client_secret)
@@ -103,20 +105,27 @@ def authorize(self, response, grant_type='pin'):
103105
}, True)
104106

105107
def prepare_headers(self, force_anon=False):
108+
headers = {}
106109
if force_anon or self.auth is None:
107110
if self.client_id is None:
108111
raise ImgurClientError('Client credentials not found!')
109112
else:
110-
return {'Authorization': 'Client-ID %s' % self.get_client_id()}
113+
headers['Authorization'] = 'Client-ID %s' % self.get_client_id()
111114
else:
112-
return {'Authorization': 'Bearer %s' % self.auth.get_current_access_token()}
115+
headers['Authorization'] = 'Bearer %s' % self.auth.get_current_access_token()
116+
117+
if self.mashape_key is not None:
118+
headers['X-Mashape-Key'] = self.mashape_key
119+
120+
return headers
121+
113122

114123
def make_request(self, method, route, data=None, force_anon=False):
115124
method = method.lower()
116125
method_to_call = getattr(requests, method)
117126

118127
header = self.prepare_headers(force_anon)
119-
url = API_URL + ('3/%s' % route if 'oauth2' not in route else route)
128+
url = (MASHAPE_URL if self.mashape_key is not None else API_URL) + ('3/%s' % route if 'oauth2' not in route else route)
120129

121130
if method in ('delete', 'get'):
122131
response = method_to_call(url, headers=header, params=data, data=data)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Versions should comply with PEP440. For a discussion on single-sourcing
1212
# the version across setup.py and the project code, see
1313
# http://packaging.python.org/en/latest/tutorial.html#version
14-
version='1.1.5',
14+
version='1.1.6',
1515

1616
description='Official Imgur python library with OAuth2 and samples',
1717
long_description='',

0 commit comments

Comments
 (0)