Skip to content

Commit 5519774

Browse files
Merge pull request #368 from algolia/fix/key-management
Deprecate key management via the index class
2 parents 30d22fe + fa6aadb commit 5519774

File tree

2 files changed

+47
-92
lines changed

2 files changed

+47
-92
lines changed

algoliasearch/client.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -364,31 +364,36 @@ def list_api_keys(self, request_options=None):
364364
return self._req(True, '/1/keys', 'GET', request_options)
365365

366366
@deprecated
367-
def getUserKeyACL(self, key):
368-
return self.get_user_key_acl(key)
367+
def getUserKeyACL(self, api_key):
368+
return self.get_user_key_acl(api_key)
369369

370370
@deprecated
371-
def get_user_key_acl(self, key):
371+
def get_user_key_acl(self, api_key):
372372
"""Use `get_api_key_acl`"""
373-
return self.get_api_key_acl(key)
373+
return self.get_api_key_acl(api_key)
374374

375-
def get_api_key_acl(self, key, request_options=None):
375+
@deprecated
376+
def get_api_key_acl(self, api_key, request_options=None):
377+
"""Use `get_api_key`"""
378+
return self.get_api_key(api_key, request_options)
379+
380+
def get_api_key(self, api_key, request_options=None):
376381
"""'Get ACL of an api key."""
377-
path = '/1/keys/%s' % key
382+
path = '/1/keys/%s' % api_key
378383
return self._req(True, path, 'GET', request_options)
379384

380385
@deprecated
381-
def deleteUserKey(self, key):
382-
return self.delete_user_key(key)
386+
def deleteUserKey(self, api_key):
387+
return self.delete_user_key(api_key)
383388

384389
@deprecated
385-
def delete_user_key(self, key):
390+
def delete_user_key(self, api_key):
386391
"""Use `delete_api_key`"""
387-
return self.delete_api_key(key)
392+
return self.delete_api_key(api_key)
388393

389-
def delete_api_key(self, key, request_options=None):
394+
def delete_api_key(self, api_key, request_options=None):
390395
"""Delete an existing api key."""
391-
path = '/1/keys/%s' % key
396+
path = '/1/keys/%s' % api_key
392397
return self._req(False, path, 'DELETE', request_options)
393398

394399
@deprecated
@@ -451,31 +456,33 @@ def add_api_key(self, obj,
451456
if not isinstance(obj, dict):
452457
obj = {'acl': obj}
453458

454-
obj.update({
455-
'validity': validity,
456-
'maxQueriesPerIPPerHour': max_queries_per_ip_per_hour,
457-
'maxHitsPerQuery': max_hits_per_query
458-
})
459+
# Check with `is not None`, because 0 is evaluated to False
460+
if validity is not None:
461+
obj['validity'] = validity
462+
if max_queries_per_ip_per_hour is not None:
463+
obj['maxQueriesPerIPPerHour'] = max_queries_per_ip_per_hour
464+
if max_hits_per_query is not None:
465+
obj['maxHitsPerQuery'] = max_hits_per_query
459466

460467
if indexes:
461468
obj['indexes'] = indexes
462469

463470
return self._req(False, '/1/keys', 'POST', request_options, data=obj)
464471

465472
@deprecated
466-
def update_user_key(self, key, obj,
473+
def update_user_key(self, api_key, obj,
467474
validity=None,
468475
max_queries_per_ip_per_hour=None,
469476
max_hits_per_query=None,
470477
indexes=None):
471478
"""Use `update_api_key`"""
472479
return self.update_api_key(
473-
key, obj, validity, max_queries_per_ip_per_hour,
480+
api_key, obj, validity, max_queries_per_ip_per_hour,
474481
max_hits_per_query, indexes
475482
)
476483

477484

478-
def update_api_key(self, key, obj,
485+
def update_api_key(self, api_key, obj,
479486
validity=None,
480487
max_queries_per_ip_per_hour=None,
481488
max_hits_per_query=None,
@@ -525,7 +532,7 @@ def update_api_key(self, key, obj,
525532
if indexes:
526533
obj['indexes'] = indexes
527534

528-
path = '/1/keys/%s' % key
535+
path = '/1/keys/%s' % api_key
529536
return self._req(False, path, 'PUT', request_options, data=obj)
530537

531538

algoliasearch/index.py

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -859,95 +859,71 @@ def set_settings(self, settings, forward_to_slaves=True,
859859

860860
@deprecated
861861
def listUserKeys(self):
862+
"""Use `list_api_keys` on the client instead."""
862863
return self.list_user_keys()
863864

864865
@deprecated
865866
def list_user_keys(self):
866-
"""Use `list_api_keys`"""
867+
"""Use `list_api_keys` on the client instead."""
867868
return self.list_api_keys()
868869

870+
@deprecated
869871
def list_api_keys(self, request_options=None):
870-
"""
871-
List all existing api keys of this index with their associated ACLs.
872-
"""
872+
"""Use `list_api_keys` on the client instead."""
873873
return self._req(True, '/keys', 'GET', request_options)
874874

875875
@deprecated
876876
def getUserKeyACL(self, key):
877+
"""Use `get_api_key` on the client instead."""
877878
return self.get_user_key_acl(key)
878879

879880
@deprecated
880881
def get_user_key_acl(self, key):
881-
"""Use `get_api_key_acl`"""
882+
"""Use `get_api_key` on the client instead."""
882883
return self.get_api_key_acl(key)
883884

885+
@deprecated
884886
def get_api_key_acl(self, key, request_options=None):
885-
"""Get ACL of a api key associated to this index."""
887+
"""Use `get_api_key` on the client instead."""
886888
path = '/keys/%s' % key
887889
return self._req(True, path, 'GET', request_options)
888890

889891
@deprecated
890892
def deleteUserKey(self, key):
893+
"""Use `delete_api_key` on the client instead."""
891894
return self.delete_user_key(key)
892895

893896
@deprecated
894897
def delete_user_key(self, key):
895-
"""Use `delete_api_key`"""
898+
"""Use `delete_api_key` on the client instead."""
896899
return self.delete_api_key(key)
897900

901+
@deprecated
898902
def delete_api_key(self, key, request_options=None):
899-
"""Delete an existing api key associated to this index."""
903+
"""Use `delete_api_key` on the client instead."""
900904
path = '/keys/%s' % key
901905
return self._req(False, path, 'DELETE', request_options)
902906

903907
@deprecated
904908
def addUserKey(self, obj, validity=0, max_queries_per_ip_per_hour=0,
905909
max_hits_per_query=0):
910+
"""Use `add_api_key` on the client instead."""
906911
return self.add_user_key(obj, validity, max_queries_per_ip_per_hour,
907912
max_hits_per_query)
908913

909914
@deprecated
910915
def add_user_key(self, obj, validity=0, max_queries_per_ip_per_hour=0,
911916
max_hits_per_query=0):
912-
"""Use `add_api_key`"""
917+
"""Use `add_api_key` on the client instead."""
913918
return self.add_api_key(
914919
obj, validity, max_queries_per_ip_per_hour,
915920
max_hits_per_query
916921
)
917922

923+
@deprecated
918924
def add_api_key(self, obj, validity=0, max_queries_per_ip_per_hour=0,
919925
max_hits_per_query=0, request_options=None):
920-
"""
921-
Create a new api key associated to this index (can only access to
922-
this index).
923-
924-
@param obj can be two different parameters:
925-
The list of parameters for this key. Defined by a dictionary that
926-
can contains the following values:
927-
- acl: array of string
928-
- indices: array of string
929-
- validity: int
930-
- referers: array of string
931-
- description: string
932-
- maxHitsPerQuery: integer
933-
- queryParameters: string
934-
- maxQueriesPerIPPerHour: integer
935-
Or the list of ACL for this key. Defined by an array of string that
936-
can contains the following values:
937-
- search: allow to search (https and http)
938-
- addObject: allows to add/update an object in the index (https only)
939-
- deleteObject : allows to delete an existing object (https only)
940-
- deleteIndex : allows to delete index content (https only)
941-
- settings : allows to get index settings (https only)
942-
- editSettings : allows to change index settings (https only)
943-
@param validity the number of seconds after which the key will be
944-
automatically removed (0 means no time limit for this key)
945-
@param max_queries_per_ip_per_hour Specify the maximum number of API
946-
calls allowed from an IP address per hour. Defaults to 0 (no
947-
rate limit)
948-
@param max_hits_per_query Specify the maximum number of hits this
949-
API key can retrieve in one call. Defaults to 0 (unlimited)
950-
"""
926+
"""Use `add_api_key` on the client instead."""
951927
if not isinstance(obj, dict):
952928
obj = {'acl': obj}
953929

@@ -963,45 +939,17 @@ def add_api_key(self, obj, validity=0, max_queries_per_ip_per_hour=0,
963939
def update_user_key(self, key, obj, validity=None,
964940
max_queries_per_ip_per_hour=None,
965941
max_hits_per_query=None):
966-
"""Use `update_api_key`"""
942+
"""Use `update_api_key` on the client instead."""
967943
return self.update_api_key(
968944
key, obj, validity, max_queries_per_ip_per_hour,
969945
max_hits_per_query
970946
)
971947

948+
@deprecated
972949
def update_api_key(self, key, obj, validity=None,
973950
max_queries_per_ip_per_hour=None,
974951
max_hits_per_query=None, request_options=None):
975-
"""
976-
Update a api key associated to this index (can only access to this index).
977-
978-
@param obj can be two different parameters:
979-
The list of parameters for this key. Defined by a dictionary that
980-
can contains the following values:
981-
- acl: array of string
982-
- indices: array of string
983-
- validity: int
984-
- referers: array of string
985-
- description: string
986-
- maxHitsPerQuery: integer
987-
- queryParameters: string
988-
- maxQueriesPerIPPerHour: integer
989-
Or the list of ACL for this key. Defined by an array of string that
990-
can contains the following values:
991-
- search: allow to search (https and http)
992-
- addObject: allows to add/update an object in the index (https only)
993-
- deleteObject : allows to delete an existing object (https only)
994-
- deleteIndex : allows to delete index content (https only)
995-
- settings : allows to get index settings (https only)
996-
- editSettings : allows to change index settings (https only)
997-
@param validity the number of seconds after which the key will be
998-
automatically removed (0 means no time limit for this key)
999-
@param max_queries_per_ip_per_hour Specify the maximum number of API
1000-
calls allowed from an IP address per hour. Defaults to 0 (no rate
1001-
limit).
1002-
@param max_hits_per_query Specify the maximum number of hits this API
1003-
key can retrieve in one call. Defaults to 0 (unlimited)
1004-
"""
952+
"""Use `update_api_key` on the client instead."""
1005953
if not isinstance(obj, dict):
1006954
obj = {'acl': obj}
1007955

0 commit comments

Comments
 (0)