Skip to content

Commit

Permalink
nitial Python version
Browse files Browse the repository at this point in the history
  • Loading branch information
lyndsysimon committed Oct 24, 2019
1 parent ed12f1a commit b07839b
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 7 deletions.
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.

29 changes: 22 additions & 7 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# sdk:python

## Usage

```python
from pipedream import sdk as pd
from pipedream_sdk import send_event

pd.send_event(API_KEY, {'hello': 'world'})
send_event({'hello': 'world'})

# with exports
pd.send_event(
API_KEY,
send_event(
{'hello': 'world'},
exports={
'event': {'hello': 'world!'},
Expand All @@ -16,9 +17,23 @@ pd.send_event(
)
```

If you do not provide an `event` export it will be set to the `raw_event`
(first argument). `event` and `raw_event` MUST be a dict or the SDK request
will be invalid.
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

Expand Down
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'
63 changes: 63 additions & 0 deletions python/pipedream_sdk/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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_SDK_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)
import pdb; pdb.set_trace()

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()

8 changes: 8 additions & 0 deletions python/pipedream_sdk/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

client_version = '0.0.1dev0'

sdk_version = '0.3.0'
hostname = os.getenv('PD_SDK_HOST') or 'sdk.m.pipedream.net'
protocol = os.getenv('PD_SDK_PROTO') or 'https'
user_agent = f'pipedream-sdk:python/{client_version}'
Loading

0 comments on commit b07839b

Please sign in to comment.