44
55Example:
66 >>> from bundleup import BundleUp
7- >>> client = BundleUp("your-api-key")
8- >>> connections = client.connections.list()
7+ >>> with BundleUp("your-api-key") as client:
8+ ... connections = client.connections.list()
99"""
1010
11+ from typing import Optional
12+ import requests
13+
1114from .base import Base
1215from .connection import Connection , Connections
1316from .integration import Integration , Integrations
1417from .webhooks import Webhook , Webhooks
1518from .proxy import Proxy
1619from .unify import Unify
1720from .utils import validate_non_empty_string
21+ from .exceptions import BundleUpError , ValidationError , APIError
1822
1923__version__ = "0.1.0"
2024
2125
2226class BundleUp :
23- """Main BundleUp SDK client."""
27+ """Main BundleUp SDK client with context manager support ."""
2428
25- def __init__ (self , api_key : str ):
29+ def __init__ (self , api_key : str , session : Optional [ requests . Session ] = None ):
2630 """
2731 Initialize the BundleUp client.
2832
2933 Args:
3034 api_key: Your BundleUp API key
35+ session: Optional requests session for connection pooling
3136
3237 Raises:
33- ValueError : If api_key is not a valid non-empty string
38+ ValidationError : If api_key is not a valid non-empty string
3439
3540 Example:
3641 >>> client = BundleUp("your-api-key")
42+ >>> # or use as context manager
43+ >>> with BundleUp("your-api-key") as client:
44+ ... connections = client.connections.list()
3745 """
3846 validate_non_empty_string (api_key , "api_key" )
3947 self ._api_key = api_key
48+ self ._session = session or requests .Session ()
49+ self ._owns_session = session is None
4050
41- # Initialize resource instances
42- self ._connections = Connections (api_key )
43- self ._integrations = Integrations (api_key )
44- self ._webhooks = Webhooks (api_key )
51+ # Initialize resource instances with shared session
52+ self ._connections = Connections (api_key , self ._session )
53+ self ._integrations = Integrations (api_key , self ._session )
54+ self ._webhooks = Webhooks (api_key , self ._session )
55+
56+ def __enter__ (self ):
57+ """Enter context manager."""
58+ return self
59+
60+ def __exit__ (self , exc_type , exc_val , exc_tb ):
61+ """Exit context manager and cleanup resources."""
62+ self .close ()
63+ return False
64+
65+ def close (self ):
66+ """Close the underlying session if owned by this client."""
67+ if self ._owns_session and self ._session :
68+ self ._session .close ()
4569
4670 @property
4771 def connections (self ) -> Connections :
@@ -93,13 +117,13 @@ def proxy(self, connection_id: str) -> Proxy:
93117 Proxy instance for the specified connection
94118
95119 Raises:
96- ValueError : If connection_id is not a valid non-empty string
120+ ValidationError : If connection_id is not a valid non-empty string
97121
98122 Example:
99123 >>> proxy = client.proxy("connection-id")
100124 >>> data = proxy.get("/users")
101125 """
102- return Proxy (self ._api_key , connection_id )
126+ return Proxy (self ._api_key , connection_id , self . _session )
103127
104128 def unify (self , connection_id : str ) -> Unify :
105129 """
@@ -112,14 +136,18 @@ def unify(self, connection_id: str) -> Unify:
112136 Unify instance for the specified connection
113137
114138 Raises:
115- ValueError : If connection_id is not a valid non-empty string
139+ ValidationError : If connection_id is not a valid non-empty string
116140
117141 Example:
118142 >>> unify = client.unify("connection-id")
119143 >>> channels = unify.chat.channels()
120144 >>> repos = unify.git.repos()
121145 """
122- return Unify (self ._api_key , connection_id )
146+ return Unify (self ._api_key , connection_id , self ._session )
147+
148+ def __repr__ (self ) -> str :
149+ """Return a string representation of the client."""
150+ return f"BundleUp(version='{ __version__ } ')"
123151
124152
125153__all__ = [
@@ -133,4 +161,7 @@ def unify(self, connection_id: str) -> Unify:
133161 "Webhooks" ,
134162 "Proxy" ,
135163 "Unify" ,
164+ "BundleUpError" ,
165+ "ValidationError" ,
166+ "APIError" ,
136167]
0 commit comments