|
42 | 42 | from .helpers import urlify |
43 | 43 |
|
44 | 44 |
|
| 45 | +MAX_API_KEY_LENGTH = 500 |
| 46 | + |
| 47 | + |
45 | 48 | class Client(object): |
46 | 49 | """ |
47 | 50 | Entry point in the Python Client API. |
@@ -76,15 +79,14 @@ def __init__(self, app_id, api_key, hosts=None, _transport=None): |
76 | 79 | self._transport.write_hosts = hosts |
77 | 80 |
|
78 | 81 | self._transport.headers = { |
79 | | - 'X-Algolia-API-Key': api_key, |
80 | 82 | 'X-Algolia-Application-Id': app_id, |
81 | 83 | 'Content-Type': 'gzip', |
82 | 84 | 'Accept-Encoding': 'gzip', |
83 | 85 | 'User-Agent': 'Algolia for Python (%s)' % VERSION |
84 | 86 | } |
85 | 87 |
|
86 | 88 | self._app_id = app_id |
87 | | - self._api_key = api_key |
| 89 | + self.api_key = api_key |
88 | 90 |
|
89 | 91 | # Fix for AppEngine bug when using urlfetch_stub |
90 | 92 | if 'google.appengine.api.apiproxy_stub_map' in sys.modules.keys(): |
@@ -117,7 +119,11 @@ def api_key(self): |
117 | 119 | @api_key.setter |
118 | 120 | def api_key(self, value): |
119 | 121 | self._api_key = value |
120 | | - self.set_extra_headers(**{'X-Algolia-API-Key': value}) |
| 122 | + if len(value) > MAX_API_KEY_LENGTH: |
| 123 | + # If it was previously set, remove the header |
| 124 | + self.headers.pop('X-Algolia-API-Key', None) |
| 125 | + else: |
| 126 | + self.set_extra_headers(**{'X-Algolia-API-Key': value}) |
121 | 127 |
|
122 | 128 | @deprecated |
123 | 129 | def enableRateLimitForward(self, admin_api_key, end_user_ip, |
@@ -489,4 +495,8 @@ def generate_secured_api_key(self, private_api_key, queryParameters, |
489 | 495 | return str(base64.b64encode(("%s%s" % (securedKey, queryParameters)).encode('utf-8')).decode('utf-8')) |
490 | 496 |
|
491 | 497 | def _req(self, is_search, path, meth, params=None, data=None): |
| 498 | + if len(self.api_key) > MAX_API_KEY_LENGTH: |
| 499 | + if data is None: |
| 500 | + data = {} |
| 501 | + data['apiKey'] = self.api_key |
492 | 502 | return self._transport.req(is_search, path, meth, params, data) |
0 commit comments