|
| 1 | +# Pipedream Python Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPipedreamHQ%2Fpipedream-sdk-python) |
| 4 | +[](https://pypi.python.org/pypi/pipedream) |
| 5 | + |
| 6 | +The Pipedream Python library provides convenient access to the Pipedream API from Python. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```sh |
| 11 | +pip install pipedream |
| 12 | +``` |
| 13 | + |
| 14 | +## Reference |
| 15 | + |
| 16 | +A full reference for this library is available [here](https://github.com/PipedreamHQ/pipedream-sdk-python/blob/HEAD/./reference.md). |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Instantiate and use the client with the following: |
| 21 | + |
| 22 | +```python |
| 23 | +from pipedream import Pipedream |
| 24 | + |
| 25 | +client = Pipedream( |
| 26 | + x_pd_environment="YOUR_X_PD_ENVIRONMENT", |
| 27 | + client_id="YOUR_CLIENT_ID", |
| 28 | + client_secret="YOUR_CLIENT_SECRET", |
| 29 | +) |
| 30 | +client.accounts.create( |
| 31 | + project_id="project_id", |
| 32 | + app_slug="app_slug", |
| 33 | + cfmap_json="cfmap_json", |
| 34 | + connect_token="connect_token", |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +## Async Client |
| 39 | + |
| 40 | +The SDK also exports an `async` client so that you can make non-blocking calls to our API. |
| 41 | + |
| 42 | +```python |
| 43 | +import asyncio |
| 44 | + |
| 45 | +from pipedream import AsyncPipedream |
| 46 | + |
| 47 | +client = AsyncPipedream( |
| 48 | + x_pd_environment="YOUR_X_PD_ENVIRONMENT", |
| 49 | + client_id="YOUR_CLIENT_ID", |
| 50 | + client_secret="YOUR_CLIENT_SECRET", |
| 51 | +) |
| 52 | + |
| 53 | + |
| 54 | +async def main() -> None: |
| 55 | + await client.accounts.create( |
| 56 | + project_id="project_id", |
| 57 | + app_slug="app_slug", |
| 58 | + cfmap_json="cfmap_json", |
| 59 | + connect_token="connect_token", |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +asyncio.run(main()) |
| 64 | +``` |
| 65 | + |
| 66 | +## Exception Handling |
| 67 | + |
| 68 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 69 | +will be thrown. |
| 70 | + |
| 71 | +```python |
| 72 | +from pipedream.core.api_error import ApiError |
| 73 | + |
| 74 | +try: |
| 75 | + client.accounts.create(...) |
| 76 | +except ApiError as e: |
| 77 | + print(e.status_code) |
| 78 | + print(e.body) |
| 79 | +``` |
| 80 | + |
| 81 | +## Pagination |
| 82 | + |
| 83 | +Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object. |
| 84 | + |
| 85 | +```python |
| 86 | +from pipedream import Pipedream |
| 87 | + |
| 88 | +client = Pipedream( |
| 89 | + x_pd_environment="YOUR_X_PD_ENVIRONMENT", |
| 90 | + client_id="YOUR_CLIENT_ID", |
| 91 | + client_secret="YOUR_CLIENT_SECRET", |
| 92 | +) |
| 93 | +response = client.apps.list() |
| 94 | +for item in response: |
| 95 | + yield item |
| 96 | +# alternatively, you can paginate page-by-page |
| 97 | +for page in response.iter_pages(): |
| 98 | + yield page |
| 99 | +``` |
| 100 | + |
| 101 | +## Advanced |
| 102 | + |
| 103 | +### Access Raw Response Data |
| 104 | + |
| 105 | +The SDK provides access to raw response data, including headers, through the `.with_raw_response` property. |
| 106 | +The `.with_raw_response` property returns a "raw" client that can be used to access the `.headers` and `.data` attributes. |
| 107 | + |
| 108 | +```python |
| 109 | +from pipedream import Pipedream |
| 110 | + |
| 111 | +client = Pipedream( |
| 112 | + ..., |
| 113 | +) |
| 114 | +response = client.accounts.with_raw_response.create(...) |
| 115 | +print(response.headers) # access the response headers |
| 116 | +print(response.data) # access the underlying object |
| 117 | +pager = client.apps.list(...) |
| 118 | +print(pager.response.headers) # access the response headers for the first page |
| 119 | +for item in pager: |
| 120 | + print(item) # access the underlying object(s) |
| 121 | +for page in pager.iter_pages(): |
| 122 | + print(page.response.headers) # access the response headers for each page |
| 123 | + for item in page: |
| 124 | + print(item) # access the underlying object(s) |
| 125 | +``` |
| 126 | + |
| 127 | +### Retries |
| 128 | + |
| 129 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 130 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 131 | +retry limit (default: 2). |
| 132 | + |
| 133 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 134 | + |
| 135 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 136 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 137 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 138 | + |
| 139 | +Use the `max_retries` request option to configure this behavior. |
| 140 | + |
| 141 | +```python |
| 142 | +client.accounts.create(..., request_options={ |
| 143 | + "max_retries": 1 |
| 144 | +}) |
| 145 | +``` |
| 146 | + |
| 147 | +### Timeouts |
| 148 | + |
| 149 | +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 150 | + |
| 151 | +```python |
| 152 | + |
| 153 | +from pipedream import Pipedream |
| 154 | + |
| 155 | +client = Pipedream( |
| 156 | + ..., |
| 157 | + timeout=20.0, |
| 158 | +) |
| 159 | + |
| 160 | + |
| 161 | +# Override timeout for a specific method |
| 162 | +client.accounts.create(..., request_options={ |
| 163 | + "timeout_in_seconds": 1 |
| 164 | +}) |
| 165 | +``` |
| 166 | + |
| 167 | +### Custom Client |
| 168 | + |
| 169 | +You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies |
| 170 | +and transports. |
| 171 | + |
| 172 | +```python |
| 173 | +import httpx |
| 174 | +from pipedream import Pipedream |
| 175 | + |
| 176 | +client = Pipedream( |
| 177 | + ..., |
| 178 | + httpx_client=httpx.Client( |
| 179 | + proxies="http://my.test.proxy.example.com", |
| 180 | + transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 181 | + ), |
| 182 | +) |
| 183 | +``` |
| 184 | + |
| 185 | +## Contributing |
| 186 | + |
| 187 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 188 | +Additions made directly to this library would have to be moved over to our generation code, |
| 189 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 190 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 191 | +an issue first to discuss with us! |
| 192 | + |
| 193 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments