Skip to content

Commit 6e1bc8d

Browse files
committed
Update custom client to match TS
1 parent 7f411db commit 6e1bc8d

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

src/pipedream/pipedream.py

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
2-
from string import Template
2+
from typing import (
3+
Literal,
4+
Optional,
5+
)
36

47
from .client import (
58
AsyncClient,
@@ -8,37 +11,72 @@
811
from .environment import PipedreamEnvironment
912

1013

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+
1129
class Pipedream(Client):
30+
1231
def __init__(
1332
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,
1738
**kwargs,
1839
):
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+
)
2155

2256

2357
class AsyncPipedream(AsyncClient):
58+
2459
def __init__(
2560
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,
2966
**kwargs,
3067
):
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")
3471

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")
3874

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

Comments
 (0)