Skip to content

Commit 114ecb7

Browse files
authored
feat!: accept positional arguments when creating client (#231)
Modifies client creation to accept the server URL and API key as optional positional arguments. BREAKING CHANGE: Swaps the api_key and url argument positions in Client init.
1 parent c927af0 commit 114ecb7

File tree

7 files changed

+216
-67
lines changed

7 files changed

+216
-67
lines changed

src/posit/connect/client.py

Lines changed: 109 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from requests import Response, Session
6-
from typing import Optional
6+
from typing import Optional, overload
77

88
from . import hooks, me, urls
99

@@ -47,11 +47,114 @@ class Client:
4747
Server version.
4848
"""
4949

50-
def __init__(
51-
self,
52-
api_key: Optional[str] = None,
53-
url: Optional[str] = None,
54-
) -> None:
50+
@overload
51+
def __init__(self) -> None:
52+
"""Initialize a Client instance.
53+
54+
Creates a client instance using credentials read from the environment.
55+
56+
Environment Variables
57+
---------------------
58+
CONNECT_SERVER - The Connect server URL.
59+
CONNECT_API_KEY - The API key credential for client authentication.
60+
61+
Examples
62+
--------
63+
Client()
64+
"""
65+
...
66+
67+
@overload
68+
def __init__(self, url: str) -> None:
69+
"""Initialize a Client instance.
70+
71+
Creates a client instance using a provided URL and API key credential read from the environment.
72+
73+
Environment Variables
74+
---------------------
75+
CONNECT_API_KEY - The API key credential for client authentication.
76+
77+
Parameters
78+
----------
79+
url : str
80+
The Connect server URL.
81+
82+
Examples
83+
--------
84+
Client("https://connect.example.com)
85+
"""
86+
...
87+
88+
@overload
89+
def __init__(self, url: str, api_key: str) -> None:
90+
"""Initialize a Client instance.
91+
92+
Parameters
93+
----------
94+
url : str
95+
The Connect server URL.
96+
api_key : str
97+
The API key credential for client authentication.
98+
99+
Examples
100+
--------
101+
>>> Client("https://connect.example.com", abcdefghijklmnopqrstuvwxyz012345")
102+
"""
103+
...
104+
105+
@overload
106+
def __init__(self, *args, **kwargs) -> None:
107+
"""Initialize a Client instance."""
108+
...
109+
110+
def __init__(self, *args, **kwargs) -> None:
111+
"""Initialize a Client instance.
112+
113+
Environment Variables
114+
---------------------
115+
CONNECT_SERVER - The Connect server URL.
116+
CONNECT_API_KEY - The API key credential for client authentication.
117+
118+
Parameters
119+
----------
120+
*args
121+
Variable length argument list. Can accept:
122+
- (url: str)
123+
url: str
124+
The Connect server URL.
125+
- (url: str, api_key: str)
126+
url: str
127+
The Connect server URL.
128+
api_key: str
129+
The API key credential for client authentication.
130+
131+
**kwargs
132+
Keyword arguments. Can include 'url' and 'api_key'.
133+
134+
Examples
135+
--------
136+
>>> Client()
137+
>>> Client("https://connect.example.com")
138+
>>> Client("https://connect.example.com", abcdefghijklmnopqrstuvwxyz012345")
139+
>>> Client(api_key=""abcdefghijklmnopqrstuvwxyz012345", url="https://connect.example.com")
140+
"""
141+
api_key = None
142+
url = None
143+
if len(args) == 1 and isinstance(args[0], str):
144+
url = args[0]
145+
elif (
146+
len(args) == 2
147+
and isinstance(args[0], str)
148+
and isinstance(args[1], str)
149+
):
150+
url = args[0]
151+
api_key = args[1]
152+
else:
153+
if "api_key" in kwargs and isinstance(kwargs["api_key"], str):
154+
api_key = kwargs["api_key"]
155+
if "url" in kwargs and isinstance(kwargs["url"], str):
156+
url = kwargs["url"]
157+
55158
self.config = Config(api_key=api_key, url=url)
56159
session = Session()
57160
session.auth = Auth(config=self.config)

tests/posit/connect/metrics/test_usage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test(self):
165165
)
166166

167167
# setup
168-
c = connect.Client("12345", "https://connect.example")
168+
c = connect.Client("https://connect.example", "12345")
169169

170170
# invoke
171171
events = c.metrics.usage.find()
@@ -239,7 +239,7 @@ def test(self):
239239
)
240240

241241
# setup
242-
c = connect.Client("12345", "https://connect.example")
242+
c = connect.Client("https://connect.example", "12345")
243243

244244
# invoke
245245
view_event = c.metrics.usage.find_one()
@@ -275,7 +275,7 @@ def test_none(self):
275275
)
276276

277277
# setup
278-
c = connect.Client("12345", "https://connect.example")
278+
c = connect.Client("https://connect.example", "12345")
279279

280280
# invoke
281281
view_event = c.metrics.usage.find_one(content_guid="not-found")

tests/posit/connect/test_bundles.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test(self):
113113
)
114114

115115
# setup
116-
c = Client("12345", "https://connect.example")
116+
c = Client("https://connect.example", "12345")
117117
bundle = c.content.get(content_guid).bundles.get(bundle_id)
118118

119119
# invoke
@@ -157,7 +157,7 @@ def test(self):
157157
)
158158

159159
# setup
160-
c = Client("12345", "https://connect.example")
160+
c = Client("https://connect.example", "12345")
161161
bundle = c.content.get(content_guid).bundles.get(bundle_id)
162162

163163
# invoke
@@ -200,7 +200,7 @@ def test_output_as_str(self, mock_file: mock.MagicMock):
200200
)
201201

202202
# setup
203-
c = Client("12345", "https://connect.example")
203+
c = Client("https://connect.example", "12345")
204204
bundle = c.content.get(content_guid).bundles.get(bundle_id)
205205

206206
# invoke
@@ -239,7 +239,7 @@ def test_output_as_io(self):
239239
)
240240

241241
# setup
242-
c = Client("12345", "https://connect.example")
242+
c = Client("https://connect.example", "12345")
243243
bundle = c.content.get(content_guid).bundles.get(bundle_id)
244244

245245
# invoke
@@ -281,7 +281,7 @@ def test_invalid_arguments(self):
281281
)
282282

283283
# setup
284-
c = Client("12345", "https://connect.example")
284+
c = Client("https://connect.example", "12345")
285285
bundle = c.content.get(content_guid).bundles.get(bundle_id)
286286

287287
# invoke
@@ -316,7 +316,7 @@ def test(self):
316316
)
317317

318318
# setup
319-
c = Client("12345", "https://connect.example")
319+
c = Client("https://connect.example", "12345")
320320
content = c.content.get(content_guid)
321321

322322
# invoke
@@ -350,7 +350,7 @@ def test_kwargs_pathname(self):
350350
)
351351

352352
# setup
353-
c = Client("12345", "https://connect.example")
353+
c = Client("https://connect.example", "12345")
354354
content = c.content.get(content_guid)
355355

356356
# invoke
@@ -373,7 +373,7 @@ def test_invalid_arguments(self):
373373
)
374374

375375
# setup
376-
c = Client("12345", "https://connect.example")
376+
c = Client("https://connect.example", "12345")
377377
content = c.content.get(content_guid)
378378

379379
# invoke
@@ -398,7 +398,7 @@ def test(self):
398398
)
399399

400400
# setup
401-
c = Client("12345", "https://connect.example")
401+
c = Client("https://connect.example", "12345")
402402

403403
# invoke
404404
bundles = c.content.get(content_guid).bundles.find()
@@ -427,7 +427,7 @@ def test(self):
427427
)
428428

429429
# setup
430-
c = Client("12345", "https://connect.example")
430+
c = Client("https://connect.example", "12345")
431431

432432
# invoke
433433
bundle = c.content.get(content_guid).bundles.find_one()
@@ -458,7 +458,7 @@ def test(self):
458458
)
459459

460460
# setup
461-
c = Client("12345", "https://connect.example")
461+
c = Client("https://connect.example", "12345")
462462

463463
# invoke
464464
bundle = c.content.get(content_guid).bundles.get(bundle_id)

0 commit comments

Comments
 (0)