|
17 | 17 | from .imgur.models.account_settings import AccountSettings |
18 | 18 |
|
19 | 19 | API_URL = 'https://api.imgur.com/' |
| 20 | +MASHAPE_URL = 'https://imgur-apiv3.p.mashape.com/' |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class AuthWrapper(object): |
@@ -72,10 +73,11 @@ class ImgurClient(object): |
72 | 73 | 'album', 'name', 'title', 'description' |
73 | 74 | } |
74 | 75 |
|
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): |
76 | 77 | self.client_id = client_id |
77 | 78 | self.client_secret = client_secret |
78 | 79 | self.auth = None |
| 80 | + self.mashape_key = mashape_key |
79 | 81 |
|
80 | 82 | if refresh_token is not None: |
81 | 83 | self.auth = AuthWrapper(access_token, refresh_token, client_id, client_secret) |
@@ -103,20 +105,27 @@ def authorize(self, response, grant_type='pin'): |
103 | 105 | }, True) |
104 | 106 |
|
105 | 107 | def prepare_headers(self, force_anon=False): |
| 108 | + headers = {} |
106 | 109 | if force_anon or self.auth is None: |
107 | 110 | if self.client_id is None: |
108 | 111 | raise ImgurClientError('Client credentials not found!') |
109 | 112 | else: |
110 | | - return {'Authorization': 'Client-ID %s' % self.get_client_id()} |
| 113 | + headers['Authorization'] = 'Client-ID %s' % self.get_client_id() |
111 | 114 | 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 | + |
113 | 122 |
|
114 | 123 | def make_request(self, method, route, data=None, force_anon=False): |
115 | 124 | method = method.lower() |
116 | 125 | method_to_call = getattr(requests, method) |
117 | 126 |
|
118 | 127 | 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) |
120 | 129 |
|
121 | 130 | if method in ('delete', 'get'): |
122 | 131 | response = method_to_call(url, headers=header, params=data, data=data) |
|
0 commit comments