Skip to content

Commit 8fbe6fc

Browse files
committed
Initial Python version
1 parent a78a391 commit 8fbe6fc

File tree

12 files changed

+435
-7
lines changed

12 files changed

+435
-7
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
# Node.js
12
node_modules
3+
4+
# Python
5+
*.py[cod]
6+
*.egg-info/
7+
.eggs/
8+
.tox/

python/Pipfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
tox = "*"
8+
pytest = "*"
9+
responses = "*"
10+
11+
[packages]
12+
requests = "*"
13+
14+
[requires]
15+
python_version = "3.7"

python/Pipfile.lock

Lines changed: 217 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# sdk:python
22

3+
## Usage
4+
35
```python
4-
from pipedream import sdk as pd
6+
from pipedream_sdk import send_event
57

6-
pd.send_event(API_KEY, {'hello': 'world'})
8+
send_event({'hello': 'world'})
79

810
# with exports
9-
pd.send_event(
10-
API_KEY,
11+
send_event(
1112
{'hello': 'world'},
1213
exports={
1314
'event': {'hello': 'world!'},
@@ -16,9 +17,23 @@ pd.send_event(
1617
)
1718
```
1819

19-
If you do not provide an `event` export it will be set to the `raw_event`
20-
(first argument). `event` and `raw_event` MUST be a dict or the SDK request
21-
will be invalid.
20+
Importing `send_event` directly will create an instance of `PipedreamSdkClient`
21+
behind the scenes, configured through environment variables. You may also create
22+
the client explicitly:
23+
24+
```python
25+
from pipedream_sdk import PipedreamSdkClient
26+
27+
pd = PipedreamSdkClient(
28+
api_key=YOUR_API_KEY,
29+
sdk_protocol='https',
30+
)
31+
pd.send_event({'hello': 'world'})
32+
```
33+
34+
This may be useful in cases where multiple events need to be pushed to Pipedream
35+
using differing API keys, or where some events should be sent as over HTTPS while
36+
others should be sent without SSL.
2237

2338
## Development
2439

python/pipedream_sdk/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .client import PipedreamSdkClient
2+
3+
4+
_CLIENT = None
5+
6+
7+
def _get_client():
8+
global _CLIENT
9+
10+
if _CLIENT is None:
11+
_CLIENT = PipedreamSdkClient()
12+
return _CLIENT
13+
14+
15+
def send_event(*args, **kwargs):
16+
_get_client().send_event(*args, **Jkwargs)

python/pipedream_sdk/about.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = '0.0.1dev0'

python/pipedream_sdk/client.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import hashlib
2+
import json
3+
import os
4+
5+
import requests
6+
7+
from .about import version
8+
9+
10+
class PipedreamSdkClient:
11+
def __init__(self, api_key=None, api_secret=None, sdk_host=None, sdk_protocol=None):
12+
self._sdk_version = '0.3.0'
13+
self._api_key = api_key or os.getenv('PD_SDK_API_KEY')
14+
self._api_secret = api_secret or os.getenv('PD_SDK_SECRET_KEY')
15+
self._sdk_host = (
16+
sdk_host
17+
or os.getenv('PD_SDK_HOST')
18+
or 'sdk.m.pipedream.net'
19+
)
20+
self._sdk_protocol = (
21+
sdk_protocol
22+
or os.getenv('PD_SDK_PROTO')
23+
or 'https'
24+
)
25+
26+
def send_event(self, raw_event, exports=None, deployment=None):
27+
"""Send an event to the PipedreamHQ SDK
28+
29+
:return: None
30+
"""
31+
# Manually create the payload so a signature can be generated
32+
event = {'raw_event': raw_event}
33+
data = json.dumps(event)
34+
headers = {
35+
'content-type': 'application/json',
36+
'user-agent': f'pipedream-sdk:python/{version}',
37+
'x-pd-sdk-version': self._sdk_version,
38+
}
39+
40+
if self._api_secret:
41+
headers['x-pd-sig'] = self._sign(data)
42+
# TODO: warn if no secret is present
43+
44+
#TODO: raise exception if POST fails
45+
response = requests.post(self._request_uri(deployment), data=data, headers=headers)
46+
47+
def _request_uri(self, deployment):
48+
uri_parts = [
49+
f'{self._sdk_protocol}://{self._sdk_host}',
50+
'pipelines',
51+
self._api_key,
52+
]
53+
if deployment:
54+
uri_parts += ['deployments', deployment]
55+
uri_parts.append('events')
56+
return '/'.join(uri_parts)
57+
58+
def _sign(self, data):
59+
sig = hashlib.sha256(self._api_secret.encode('utf-8'))
60+
sig.update(data.encode('utf-8'))
61+
return sig.hexdigest()
62+

python/pipedream_sdk/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
client_version = '0.0.1dev0'
4+
5+
sdk_version = '0.3.0'
6+
hostname = os.getenv('PD_SDK_HOST') or 'sdk.m.pipedream.net'
7+
protocol = os.getenv('PD_SDK_PROTO') or 'https'
8+
user_agent = f'pipedream-sdk:python/{client_version}'

0 commit comments

Comments
 (0)