|
1 | 1 | import os |
2 | | -from string import Template |
| 2 | +from typing import ( |
| 3 | + Literal, |
| 4 | + Optional, |
| 5 | +) |
3 | 6 |
|
4 | 7 | from .client import ( |
5 | 8 | AsyncClient, |
|
8 | 11 | from .environment import PipedreamEnvironment |
9 | 12 |
|
10 | 13 |
|
| 14 | +class OAuthCredentials: |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + client_id: Optional[str] = None, |
| 19 | + client_secret: Optional[str] = None, |
| 20 | + ): |
| 21 | + self.client_id = client_id or os.getenv("PIPEDREAM_CLIENT_ID") |
| 22 | + self.client_secret = client_secret or os.getenv( |
| 23 | + "PIPEDREAM_CLIENT_SECRET") |
| 24 | + |
| 25 | + if not self.client_id or not self.client_secret: |
| 26 | + raise ValueError("OAuth client ID and secret are required") |
| 27 | + |
| 28 | + |
11 | 29 | class Pipedream(Client): |
| 30 | + |
12 | 31 | def __init__( |
13 | 32 | self, |
14 | | - project_id: str, |
15 | | - environment: PipedreamEnvironment = PipedreamEnvironment.PROD, |
16 | | - *args, |
| 33 | + *, |
| 34 | + credentials: OAuthCredentials = OAuthCredentials(), |
| 35 | + project_id: Optional[str] = None, |
| 36 | + environment: Literal["production", "development"] = "production", |
| 37 | + api_environment: PipedreamEnvironment = PipedreamEnvironment.PROD, |
17 | 38 | **kwargs, |
18 | 39 | ): |
19 | | - super().__init__(base_url=_get_base_url(environment), *args, **kwargs) |
20 | | - self.project_id = project_id |
| 40 | + project_id = project_id or os.getenv("PIPEDREAM_PROJECT_ID") |
| 41 | + if not project_id: |
| 42 | + raise ValueError("Project ID is required") |
| 43 | + |
| 44 | + if not credentials.client_id or not credentials.client_secret: |
| 45 | + raise ValueError("OAuth client ID and secret are required") |
| 46 | + |
| 47 | + super().__init__( |
| 48 | + client_id=credentials.client_id, |
| 49 | + client_secret=credentials.client_secret, |
| 50 | + environment=api_environment, |
| 51 | + project_id=project_id, |
| 52 | + x_pd_environment=environment, |
| 53 | + **kwargs, |
| 54 | + ) |
21 | 55 |
|
22 | 56 |
|
23 | 57 | class AsyncPipedream(AsyncClient): |
| 58 | + |
24 | 59 | def __init__( |
25 | 60 | self, |
26 | | - project_id: str, |
27 | | - environment: PipedreamEnvironment = PipedreamEnvironment.PROD, |
28 | | - *args, |
| 61 | + *, |
| 62 | + credentials: OAuthCredentials = OAuthCredentials(), |
| 63 | + project_id: Optional[str] = None, |
| 64 | + environment: Literal["production", "development"] = "production", |
| 65 | + api_environment: PipedreamEnvironment = PipedreamEnvironment.PROD, |
29 | 66 | **kwargs, |
30 | 67 | ): |
31 | | - super().__init__(base_url=_get_base_url(environment), *args, **kwargs) |
32 | | - self.project_id = project_id |
33 | | - |
| 68 | + project_id = project_id or os.getenv("PIPEDREAM_PROJECT_ID") |
| 69 | + if not project_id: |
| 70 | + raise ValueError("Project ID is required") |
34 | 71 |
|
35 | | -def _get_base_url(environment: PipedreamEnvironment) -> str: |
36 | | - if not environment: |
37 | | - raise Exception("Please pass environment to construct the client") |
| 72 | + if not credentials.client_id or not credentials.client_secret: |
| 73 | + raise ValueError("OAuth client ID and secret are required") |
38 | 74 |
|
39 | | - user = os.getenv("DEV_NAMESPACE", "") |
40 | | - return Template(environment.value).substitute( |
41 | | - { |
42 | | - "user": user, |
43 | | - } |
44 | | - ) |
| 75 | + super().__init__( |
| 76 | + client_id=credentials.client_id, |
| 77 | + client_secret=credentials.client_secret, |
| 78 | + environment=api_environment, |
| 79 | + project_id=project_id, |
| 80 | + x_pd_environment=environment, |
| 81 | + **kwargs, |
| 82 | + ) |
0 commit comments