Skip to content

Commit 92512cb

Browse files
Merge pull request #132 from keenlabs/issues-109
Issues 109
2 parents 6d6b023 + bd88aee commit 92512cb

File tree

11 files changed

+663
-23
lines changed

11 files changed

+663
-23
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
language: python
22
python:
3-
- "2.6"
43
- "2.7"
5-
- "3.2"
6-
- "3.3"
74
- "3.4"
85
- "3.5"
96
- "3.6"
107
# command to install dependencies
118
install: |
12-
if [ "$TRAVIS_PYTHON_VERSION" == 3.2 ]
13-
then
14-
pip install "setuptools<30"
15-
fi
16-
179
pip install -r requirements.txt
1810
1911
# command to run tests

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
---------
33

4+
0.5.0
5+
``````
6+
+ Added support for Access Keys.
7+
+ Added support for order_by and limit, group_by options.
8+
+ Deprecated python 2.6, 3.2 and 3.3.
9+
+ Scoped Keys are now deprecated in favor of Access Keys.
10+
+ Now permits more versions of the requests library. (issue #133)
11+
12+
413
0.4.0
514
``````
615

README.rst

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Use pip to install!
1717

1818
pip install keen
1919

20-
This client is known to work on Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5 and 3.6.
20+
This client is known to work on Python 2.7, 3.4, 3.5 and 3.6.
2121

2222
For versions of Python < 2.7.9, you’ll need to install pyasn1, ndg-httpsclient, pyOpenSSL.
2323

@@ -470,10 +470,81 @@ returned by the server in the specified time. For example:
470470
471471
This will cause both add_event() and add_events() to timeout after 100 seconds. If this timeout limit is hit, a requests.Timeout will be raised. Due to a bug in the requests library, you might also see an SSLError (https://github.com/kennethreitz/requests/issues/1294)
472472

473-
Create Scoped Keys
473+
Create Access Keys
474474
''''''''''''''''''
475475

476-
The Python client enables you to create `Scoped Keys <https://keen.io/docs/security/#scoped-key>`_ easily. For example:
476+
The Python client enables the creation and manipulation of `Access Keys <https://keen.io/docs/access/access-keys>`_. Examples:
477+
478+
.. code-block:: python
479+
480+
from keen.client import KeenClient
481+
# You could also simply use: import keen
482+
# If you do this, you will need your project ID and master key set in environment variables.
483+
484+
client = KeenClient(
485+
project_id="xxxx",
486+
master_key="zzzz"
487+
)
488+
489+
# Create an access key. See: https://keen.io/docs/access/access-keys/#customizing-your-access-key
490+
client.create_access_key(name="Dave_Barry_Key", is_enabled=True, permitted=["writes", "cached_queries"],
491+
options={"cached_queries": {"allowed": ["dave_barry_in_cyberspace_sales"]}})
492+
493+
# Display all access keys associated with this client's project.
494+
client.list_access_keys()
495+
496+
# Get details on a particular access key.
497+
client.get_access_key(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ")
498+
499+
# Revoke (disable) an access key.
500+
client.revoke_access_key(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ")
501+
502+
# Unrevoke (re-enable) an access key.
503+
client.unrevoke_access_key(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ")
504+
505+
# Change just the name of an access key.
506+
client.update_access_key_name(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", name="Some_New_Name")
507+
508+
# Add new access key permissions to existing permissions on a given key.
509+
# In this case the set of permissions currently contains "writes" and "cached_queries".
510+
# This function call keeps the old permissions and adds "queries" to that set.
511+
# ("writes", "cached_queries") + ("queries") = ("writes", "cached_queries", "queries")
512+
client.add_access_key_permissions(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", permissions=["queries"])
513+
514+
# Remove one or more access key permissions from a given key.
515+
# In this case the set of permissions currently contains "writes", "cached_queries", and "queries".
516+
# This function call will keep the old permissions not explicitly removed here.
517+
# So we will remove both "writes" and "queries" from the set, leaving only "cached_queries".
518+
# ("writes", "cached_queries", "queries") - ("writes", "queries") = ("cached_queries")
519+
client.remove_access_key_permissions(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", permissions=["writes", "queries"])
520+
521+
# We can also perform a full update on the permissions, replacing all existing permissions with a new list.
522+
# In this case our existing permissions contains only "cached_queries".
523+
# We will replace this set with the "writes" permission with this function call.
524+
# ("cached_queries") REPLACE-WITH ("writes") = ("writes")
525+
client.update_access_key_permissions(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", permissions=["writes"])
526+
527+
# Replace all existing key options with this new options object.
528+
client.update_access_key_options(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", options={"writes": {
529+
"autofill": {
530+
"customer": {
531+
"id": "93iskds39kd93id",
532+
"name": "Ada Corp."
533+
}
534+
}
535+
}})
536+
537+
# Replace everything but the key ID with what is supplied here.
538+
# If a field is not supplied here, it will be set to a blank value.
539+
# In this case, no options are supplied, so all options will be removed.
540+
client.update_access_key_full(access_key_id="ABCDEFGHIJKLMNOPQRSTUVWXYZ", name="Strong_Bad", is_active=True, permitted=["queries"])
541+
542+
543+
Create Scoped Keys (**Deprecated**)
544+
''''''''''''''''''
545+
546+
The Python client enables you to create `Scoped Keys <https://keen.io/docs/security/#scoped-key>`_ easily, but Access Keys are better!
547+
If you need to use them anyway, for legacy reasons, here's how:
477548

478549
.. code-block:: python
479550
@@ -501,7 +572,7 @@ To run tests:
501572
Changelog
502573
---------
503574

504-
This project is in alpha stage at version 0.4.0 . See the full CHANGELOG `here <./CHANGELOG.rst>`_.
575+
This project is in alpha stage at version 0.5.0 . See the full CHANGELOG `here <./CHANGELOG.rst>`_.
505576

506577

507578
Questions & Support

keen/__init__.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,125 @@ def get_all_collections():
496496
"""
497497
_initialize_client_from_environment()
498498
return _client.get_all_collections()
499+
500+
def create_access_key(name, is_active=True, permitted=[], options={}):
501+
""" Creates a new access key. A master key must be set first.
502+
503+
:param name: the name of the access key to create
504+
:param is_active: Boolean value dictating whether this key is currently active (default True)
505+
:param permitted: list of strings describing which operation types this key will permit
506+
Legal values include "writes", "queries", "saved_queries", "cached_queries",
507+
"datasets", and "schema".
508+
:param options: dictionary containing more details about the key's permitted and restricted
509+
functionality
510+
"""
511+
_initialize_client_from_environment()
512+
return _client.create_access_key(name=name, is_active=is_active,
513+
permitted=permitted, options=options)
514+
515+
def list_access_keys():
516+
"""
517+
Returns a list of all access keys in this project. A master key must be set first.
518+
"""
519+
_initialize_client_from_environment()
520+
return _client.list_access_keys()
521+
522+
def get_access_key(access_key_id):
523+
"""
524+
Returns details on a particular access key. A master key must be set first.
525+
526+
:param access_key_id: the 'key' value of the access key to retreive data from
527+
"""
528+
_initialize_client_from_environment()
529+
return _client.get_access_key(access_key_id)
530+
531+
def update_access_key_name(access_key_id, name):
532+
"""
533+
Updates only the name portion of an access key.
534+
535+
:param access_key_id: the 'key' value of the access key to change the name of
536+
:param name: the new name to give this access key
537+
"""
538+
_initialize_client_from_environment()
539+
return _client.update_access_key_name(access_key_id, name)
540+
541+
def add_access_key_permissions(access_key_id, permissions):
542+
"""
543+
Adds to the existing list of permissions on this key with the contents of this list.
544+
Will not remove any existing permissions or modify the remainder of the key.
545+
546+
:param access_key_id: the 'key' value of the access key to add permissions to
547+
:param permissions: the new permissions to add to the existing list of permissions
548+
"""
549+
_initialize_client_from_environment()
550+
return _client.add_access_key_permissions(access_key_id, permissions)
551+
552+
def remove_access_key_permissions(access_key_id, permissions):
553+
"""
554+
Removes a list of permissions from the existing list of permissions.
555+
Will not remove all existing permissions unless all such permissions are included
556+
in this list. Not to be confused with key revocation.
557+
558+
See also: revoke_access_key()
559+
560+
:param access_key_id: the 'key' value of the access key to remove some permissions from
561+
:param permissions: the permissions you wish to remove from this access key
562+
"""
563+
_initialize_client_from_environment()
564+
return _client.remove_access_key_permissions(access_key_id, permissions)
565+
566+
def update_access_key_permissions(access_key_id, permissions):
567+
"""
568+
Replaces all of the permissions on the access key but does not change
569+
non-permission properties such as the key's name.
570+
571+
See also: add_access_key_permissions() and remove_access_key_permissions().
572+
573+
:param access_key_id: the 'key' value of the access key to change the permissions of
574+
:param permissions: the new list of permissions for this key
575+
"""
576+
_initialize_client_from_environment()
577+
return _client.update_access_key_permissions(access_key_id, permissions)
578+
579+
def update_access_key_options(self, access_key_id, options):
580+
"""
581+
Replaces all of the options on the access key but does not change
582+
non-option properties such as permissions or the key's name.
583+
584+
:param access_key_id: the 'key' value of the access key to change the options of
585+
:param options: the new dictionary of options for this key
586+
"""
587+
_initialize_client_from_environment()
588+
return _client.update_access_key_options(access_key_id, options)
589+
590+
def update_access_key_full(access_key_id, name, is_active, permitted, options):
591+
"""
592+
Replaces the 'name', 'is_active', 'permitted', and 'options' values of a given key.
593+
A master key must be set first.
594+
595+
:param access_key_id: the 'key' value of the access key for which the values will be replaced
596+
:param name: the new name desired for this access key
597+
:param is_active: whether the key should become enabled (True) or revoked (False)
598+
:param permitted: the new list of permissions desired for this access key
599+
:param options: the new dictionary of options for this access key
600+
"""
601+
_initialize_client_from_environment()
602+
return _client.update_access_key_full(access_key_id, name, is_active, permitted, options)
603+
604+
def revoke_access_key(access_key_id):
605+
"""
606+
Revokes an access key. "Bad dog! No biscuit!"
607+
608+
:param access_key_id: the 'key' value of the access key to revoke
609+
"""
610+
_initialize_client_from_environment()
611+
return _client.revoke_access_key(access_key_id)
612+
613+
def unrevoke_access_key(access_key_id):
614+
"""
615+
Re-enables an access key.
616+
617+
:param access_key_id: the 'key' value of the access key to re-enable (unrevoke)
618+
"""
619+
_initialize_client_from_environment()
620+
return _client.unrevoke_access_key(access_key_id)

0 commit comments

Comments
 (0)