Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Work in Progress] Python SDK #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# Node.js
node_modules

# Python
*.py[cod]
*.egg-info/
.eggs/
.tox/
15 changes: 15 additions & 0 deletions python/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
tox = "*"
pytest = "*"
responses = "*"

[packages]
requests = "*"

[requires]
python_version = "3.7"
217 changes: 217 additions & 0 deletions python/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# sdk:python

## Usage

```python
from pipedream_sdk import send_event

send_event({'hello': 'world'})

# with exports
send_event(
{'hello': 'world'},
exports={
'event': {'hello': 'world!'},
'other_export': 'howdy',
}
)
```

Importing `send_event` directly will create an instance of `PipedreamSdkClient`
behind the scenes, configured through environment variables. You may also create
the client explicitly:

```python
from pipedream_sdk import PipedreamSdkClient

pd = PipedreamSdkClient(
api_key=YOUR_API_KEY,
sdk_protocol='https',
)
pd.send_event({'hello': 'world'})
```

This may be useful in cases where multiple events need to be pushed to Pipedream
using differing API keys, or where some events should be sent as over HTTPS while
others should be sent without SSL.

## Development

TODO
16 changes: 16 additions & 0 deletions python/pipedream_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .client import PipedreamSdkClient


_CLIENT = None


def _get_client():
global _CLIENT

if _CLIENT is None:
_CLIENT = PipedreamSdkClient()
return _CLIENT


def send_event(*args, **kwargs):
_get_client().send_event(*args, **Jkwargs)
1 change: 1 addition & 0 deletions python/pipedream_sdk/about.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.0.1dev0'
62 changes: 62 additions & 0 deletions python/pipedream_sdk/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import hashlib
import json
import os

import requests

from .about import version


class PipedreamSdkClient:
def __init__(self, api_key=None, api_secret=None, sdk_host=None, sdk_protocol=None):
self._sdk_version = '0.3.0'
self._api_key = api_key or os.getenv('PD_SDK_API_KEY')
self._api_secret = api_secret or os.getenv('PD_SECRET_KEY')
self._sdk_host = (
sdk_host
or os.getenv('PD_SDK_HOST')
or 'sdk.m.pipedream.net'
)
self._sdk_protocol = (
sdk_protocol
or os.getenv('PD_SDK_PROTO')
or 'https'
)

def send_event(self, raw_event, exports=None, deployment=None):
"""Send an event to the PipedreamHQ SDK

:return: None
"""
# Manually create the payload so a signature can be generated
event = {'raw_event': raw_event}
data = json.dumps(event)
headers = {
'content-type': 'application/json',
'user-agent': f'pipedream-sdk:python/{version}',
'x-pd-sdk-version': self._sdk_version,
}

if self._api_secret:
headers['x-pd-sig'] = self._sign(data)
# TODO: warn if no secret is present

#TODO: raise exception if POST fails
response = requests.post(self._request_uri(deployment), data=data, headers=headers)

def _request_uri(self, deployment):
uri_parts = [
f'{self._sdk_protocol}://{self._sdk_host}',
'pipelines',
self._api_key,
]
if deployment:
uri_parts += ['deployments', deployment]
uri_parts.append('events')
return '/'.join(uri_parts)

def _sign(self, data):
sig = hashlib.sha256(self._api_secret.encode('utf-8'))
sig.update(data.encode('utf-8'))
return sig.hexdigest()

Loading