Skip to content

Commit 20feae6

Browse files
committed
feat: initial commit with users and notify endpoints
0 parents  commit 20feae6

10 files changed

+315
-0
lines changed

.editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 4
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true

.gitignore

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
wheels/
22+
*.egg-info/
23+
.installed.cfg
24+
*.egg
25+
MANIFEST
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*.cover
46+
.hypothesis/
47+
.pytest_cache/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
db.sqlite3
57+
58+
# Flask stuff:
59+
instance/
60+
.webassets-cache
61+
62+
# Scrapy stuff:
63+
.scrapy
64+
65+
# Sphinx documentation
66+
docs/_build/
67+
68+
# PyBuilder
69+
target/
70+
71+
# Jupyter Notebook
72+
.ipynb_checkpoints
73+
74+
# pyenv
75+
.python-version
76+
77+
# celery beat schedule file
78+
celerybeat-schedule
79+
80+
# SageMath parsed files
81+
*.sage.py
82+
83+
# Environments
84+
.env
85+
.venv
86+
env/
87+
venv/
88+
ENV/
89+
env.bak/
90+
venv.bak/
91+
92+
# Spyder project settings
93+
.spyderproject
94+
.spyproject
95+
96+
# Rope project settings
97+
.ropeproject
98+
99+
# mkdocs documentation
100+
/site
101+
102+
# mypy
103+
.mypy_cache/
104+
105+
.DS_Store
106+
107+
# Pipenv
108+
Pipfile
109+
Pipfile.lock
110+
.vscode/launch.json

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Knock Labs, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Knock Python library
2+
3+
Knock API access for applications written in Python.
4+
5+
## Documentation
6+
7+
See the [documentation](https://docs.knock.app) for Python usage examples.
8+
9+
## Installation
10+
11+
To install from PyPi, run the following:
12+
13+
```bash
14+
pip install knock-py
15+
```
16+
17+
To install from source, clone the repo and run the following:
18+
19+
```bash
20+
python setup.py install
21+
```
22+
23+
## Configuration
24+
25+
To use the library you must provide a secret API key, provided in the Knock dashboard.
26+
27+
```python
28+
import knock
29+
30+
knock.api_key = "sk_12345"
31+
```

knockapi/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from knockapi.client import Knock
2+
from knockapi.resources import *

knockapi/client.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import requests
2+
3+
__version__ = '0.1.0'
4+
5+
6+
class Connection(object):
7+
def __init__(self, api_key, host='https://api.knock.app'):
8+
self.api_key = api_key
9+
self.host = host
10+
self.client_version = __version__
11+
self.headers = {
12+
'Authorization': 'Bearer {}'.format(self.api_key),
13+
'User-Agent': 'Knock Python - {}'.format(self.client_version)
14+
}
15+
16+
def request(self, method, endpoint, payload=None):
17+
url = '{}/v1{}'.format(self.host, endpoint)
18+
19+
r = requests.request(
20+
method,
21+
url,
22+
json=payload,
23+
headers=self.headers,
24+
)
25+
26+
if r.ok:
27+
return r.json()
28+
return r.raise_for_status()
29+
30+
31+
class Knock(Connection):
32+
"""Client to access the Knock features."""
33+
@property
34+
def _auth(self):
35+
return self.api_key
36+
37+
@property
38+
def _version(self):
39+
return __version__
40+
41+
@property
42+
def users(self):
43+
from .resources import User
44+
return User(self)
45+
46+
def notify(self, name, actor, recipients, data={}):
47+
"""
48+
Triggers a notification workflow.
49+
50+
Args:
51+
name (str): The name of the notification to invoke.
52+
actor (str): The ID of the actor performing this action.
53+
recipients (array): An array of user IDs of who should be notified.
54+
data (dict): Any data to be passed to the notify call.
55+
56+
Returns:
57+
dict: Response from Knock.
58+
"""
59+
params = {
60+
'name': name,
61+
'actor': actor,
62+
'recipients': recipients,
63+
'data': data
64+
}
65+
return self.request("post", "/notify", payload=params)

knockapi/resources/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .users import User

knockapi/resources/service.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Service(object):
2+
def __init__(self, client):
3+
self.client = client

knockapi/resources/users.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from .service import Service
2+
3+
4+
class User(Service):
5+
def get_user(self, id):
6+
"""
7+
Get a user by their id
8+
9+
Args:
10+
id: The users ID
11+
12+
Returns:
13+
dict: User response from Knock.
14+
"""
15+
endpoint = '/users/{}'.format(id)
16+
return self.client.request('get', endpoint)
17+
18+
def identify(self, id, data={}):
19+
"""
20+
Identify a user, upserting them
21+
22+
Args:
23+
id (str): The users ID
24+
data (dict): Other properties to put on the user
25+
26+
Returns:
27+
dict: User response from Knock.
28+
"""
29+
endpoint = '/users/{}'.format(id)
30+
return self.client.request('put', endpoint, payload=data)

setup.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import os
3+
import setuptools
4+
5+
from setuptools.command.install import install
6+
7+
version = '0.1.0'
8+
9+
with open("README.md", "r") as f:
10+
long_description = f.read()
11+
12+
setuptools.setup(
13+
name='knockapi',
14+
version=version,
15+
python_requires='>=2.7.16, <4',
16+
install_requires=[
17+
'requests',
18+
'importlib-metadata ~= 1.0 ; python_version < "3.8"'
19+
],
20+
extras_require={
21+
'dev': [
22+
'bump2version',
23+
]
24+
},
25+
classifiers=[
26+
'Development Status :: 5 - Production/Stable',
27+
'Intended Audience :: Developers',
28+
'Topic :: Software Development :: Libraries',
29+
'License :: OSI Approved :: MIT License',
30+
'Programming Language :: Python :: 2',
31+
'Programming Language :: Python :: 2.7',
32+
'Programming Language :: Python :: 3',
33+
'Programming Language :: Python :: 3.5',
34+
'Programming Language :: Python :: 3.6',
35+
'Programming Language :: Python :: 3.7',
36+
'Programming Language :: Python :: 3.8',
37+
'Programming Language :: Python :: 3.9',
38+
],
39+
description='Client library for the Knock API',
40+
long_description=long_description,
41+
long_description_content_type='text/markdown',
42+
url='https://github.com/knocklabs/knock-python',
43+
packages=setuptools.find_packages(),
44+
author='Knock Labs, Inc.',
45+
author_email='[email protected]',
46+
license='MIT'
47+
)

0 commit comments

Comments
 (0)