Skip to content

Commit c059690

Browse files
committed
[python] regenerate client samples
Regenerate the aiohttp and lazy-import Python clients with copied defaults and independent implicit clients.
1 parent 01a2bfc commit c059690

26 files changed

Lines changed: 574 additions & 85 deletions

samples/openapi3/client/petstore/python-aiohttp/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ async with petstore_api.ApiClient(configuration) as api_client:
7979

8080
```
8181

82+
An API instance constructed without an explicit or registered default
83+
`ApiClient` owns its client. Use the API as a context manager:
84+
85+
```python
86+
async with petstore_api.AnotherFakeApi() as api_instance:
87+
# Call API methods through api_instance.
88+
pass
89+
90+
```
91+
92+
Direct construction requires
93+
`await api_instance.close()`. Closing an API constructed
94+
with an explicit or registered default client is a no-op; the caller remains
95+
responsible for that client. Configuration-owned values are copied for an
96+
implicit client. Logging resources retain their identity. Configuration
97+
containers and proxy-header mappings are copied; other stateful transport
98+
extension objects remain caller-owned.
99+
100+
`Configuration.set_default()` stores a copy, so later changes to the supplied
101+
object do not affect new implicit clients. `Configuration.get_default()`
102+
returns that stored baseline, while `Configuration.get_default_copy()` returns
103+
a copy.
104+
82105
## Documentation for API Endpoints
83106

84107
All URIs are relative to *http://petstore.swagger.io:80/v2*

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/another_fake_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,28 @@ class AnotherFakeApi:
3232
"""
3333

3434
def __init__(self, api_client=None) -> None:
35+
# api_client remains publicly assignable. Retain the client acquired at
36+
# construction so reassignment cannot transfer or discard ownership.
3537
if api_client is None:
36-
api_client = ApiClient.get_default()
38+
api_client, owns_api_client = ApiClient._get_default_or_new()
39+
else:
40+
owns_api_client = False
3741
self.api_client = api_client
42+
self._owned_api_client: Optional[ApiClient] = (
43+
api_client if owns_api_client else None
44+
)
45+
46+
async def close(self) -> None:
47+
owned_api_client = self._owned_api_client
48+
self._owned_api_client = None
49+
if owned_api_client is not None:
50+
await owned_api_client.close()
51+
52+
async def __aenter__(self):
53+
return self
54+
55+
async def __aexit__(self, exc_type, exc_value, traceback):
56+
await self.close()
3857

3958

4059
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/default_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,28 @@ class DefaultApi:
3030
"""
3131

3232
def __init__(self, api_client=None) -> None:
33+
# api_client remains publicly assignable. Retain the client acquired at
34+
# construction so reassignment cannot transfer or discard ownership.
3335
if api_client is None:
34-
api_client = ApiClient.get_default()
36+
api_client, owns_api_client = ApiClient._get_default_or_new()
37+
else:
38+
owns_api_client = False
3539
self.api_client = api_client
40+
self._owned_api_client: Optional[ApiClient] = (
41+
api_client if owns_api_client else None
42+
)
43+
44+
async def close(self) -> None:
45+
owned_api_client = self._owned_api_client
46+
self._owned_api_client = None
47+
if owned_api_client is not None:
48+
await owned_api_client.close()
49+
50+
async def __aenter__(self):
51+
return self
52+
53+
async def __aexit__(self, exc_type, exc_value, traceback):
54+
await self.close()
3655

3756

3857
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,28 @@ class FakeApi:
4747
"""
4848

4949
def __init__(self, api_client=None) -> None:
50+
# api_client remains publicly assignable. Retain the client acquired at
51+
# construction so reassignment cannot transfer or discard ownership.
5052
if api_client is None:
51-
api_client = ApiClient.get_default()
53+
api_client, owns_api_client = ApiClient._get_default_or_new()
54+
else:
55+
owns_api_client = False
5256
self.api_client = api_client
57+
self._owned_api_client: Optional[ApiClient] = (
58+
api_client if owns_api_client else None
59+
)
60+
61+
async def close(self) -> None:
62+
owned_api_client = self._owned_api_client
63+
self._owned_api_client = None
64+
if owned_api_client is not None:
65+
await owned_api_client.close()
66+
67+
async def __aenter__(self):
68+
return self
69+
70+
async def __aexit__(self, exc_type, exc_value, traceback):
71+
await self.close()
5372

5473

5574
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_classname_tags123_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,28 @@ class FakeClassnameTags123Api:
3232
"""
3333

3434
def __init__(self, api_client=None) -> None:
35+
# api_client remains publicly assignable. Retain the client acquired at
36+
# construction so reassignment cannot transfer or discard ownership.
3537
if api_client is None:
36-
api_client = ApiClient.get_default()
38+
api_client, owns_api_client = ApiClient._get_default_or_new()
39+
else:
40+
owns_api_client = False
3741
self.api_client = api_client
42+
self._owned_api_client: Optional[ApiClient] = (
43+
api_client if owns_api_client else None
44+
)
45+
46+
async def close(self) -> None:
47+
owned_api_client = self._owned_api_client
48+
self._owned_api_client = None
49+
if owned_api_client is not None:
50+
await owned_api_client.close()
51+
52+
async def __aenter__(self):
53+
return self
54+
55+
async def __aexit__(self, exc_type, exc_value, traceback):
56+
await self.close()
3857

3958

4059
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/import_test_datetime_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,28 @@ class ImportTestDatetimeApi:
3030
"""
3131

3232
def __init__(self, api_client=None) -> None:
33+
# api_client remains publicly assignable. Retain the client acquired at
34+
# construction so reassignment cannot transfer or discard ownership.
3335
if api_client is None:
34-
api_client = ApiClient.get_default()
36+
api_client, owns_api_client = ApiClient._get_default_or_new()
37+
else:
38+
owns_api_client = False
3539
self.api_client = api_client
40+
self._owned_api_client: Optional[ApiClient] = (
41+
api_client if owns_api_client else None
42+
)
43+
44+
async def close(self) -> None:
45+
owned_api_client = self._owned_api_client
46+
self._owned_api_client = None
47+
if owned_api_client is not None:
48+
await owned_api_client.close()
49+
50+
async def __aenter__(self):
51+
return self
52+
53+
async def __aexit__(self, exc_type, exc_value, traceback):
54+
await self.close()
3655

3756

3857
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/pet_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,28 @@ class PetApi:
3434
"""
3535

3636
def __init__(self, api_client=None) -> None:
37+
# api_client remains publicly assignable. Retain the client acquired at
38+
# construction so reassignment cannot transfer or discard ownership.
3739
if api_client is None:
38-
api_client = ApiClient.get_default()
40+
api_client, owns_api_client = ApiClient._get_default_or_new()
41+
else:
42+
owns_api_client = False
3943
self.api_client = api_client
44+
self._owned_api_client: Optional[ApiClient] = (
45+
api_client if owns_api_client else None
46+
)
47+
48+
async def close(self) -> None:
49+
owned_api_client = self._owned_api_client
50+
self._owned_api_client = None
51+
if owned_api_client is not None:
52+
await owned_api_client.close()
53+
54+
async def __aenter__(self):
55+
return self
56+
57+
async def __aexit__(self, exc_type, exc_value, traceback):
58+
await self.close()
4059

4160

4261
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/store_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,28 @@ class StoreApi:
3333
"""
3434

3535
def __init__(self, api_client=None) -> None:
36+
# api_client remains publicly assignable. Retain the client acquired at
37+
# construction so reassignment cannot transfer or discard ownership.
3638
if api_client is None:
37-
api_client = ApiClient.get_default()
39+
api_client, owns_api_client = ApiClient._get_default_or_new()
40+
else:
41+
owns_api_client = False
3842
self.api_client = api_client
43+
self._owned_api_client: Optional[ApiClient] = (
44+
api_client if owns_api_client else None
45+
)
46+
47+
async def close(self) -> None:
48+
owned_api_client = self._owned_api_client
49+
self._owned_api_client = None
50+
if owned_api_client is not None:
51+
await owned_api_client.close()
52+
53+
async def __aenter__(self):
54+
return self
55+
56+
async def __aexit__(self, exc_type, exc_value, traceback):
57+
await self.close()
3958

4059

4160
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/user_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,28 @@ class UserApi:
3333
"""
3434

3535
def __init__(self, api_client=None) -> None:
36+
# api_client remains publicly assignable. Retain the client acquired at
37+
# construction so reassignment cannot transfer or discard ownership.
3638
if api_client is None:
37-
api_client = ApiClient.get_default()
39+
api_client, owns_api_client = ApiClient._get_default_or_new()
40+
else:
41+
owns_api_client = False
3842
self.api_client = api_client
43+
self._owned_api_client: Optional[ApiClient] = (
44+
api_client if owns_api_client else None
45+
)
46+
47+
async def close(self) -> None:
48+
owned_api_client = self._owned_api_client
49+
self._owned_api_client = None
50+
if owned_api_client is not None:
51+
await owned_api_client.close()
52+
53+
async def __aenter__(self):
54+
return self
55+
56+
async def __aexit__(self, exc_type, exc_value, traceback):
57+
await self.close()
3958

4059

4160
@validate_call

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
) -> None:
8383
# use default configuration if none is provided
8484
if configuration is None:
85-
configuration = Configuration.get_default()
85+
configuration = Configuration.get_default_copy()
8686
self.configuration = configuration
8787

8888
self.rest_client = rest.RESTClientObject(configuration)
@@ -119,18 +119,15 @@ def set_default_header(self, header_name, header_value):
119119
_default = None
120120

121121
@classmethod
122-
def get_default(cls):
123-
"""Return new instance of ApiClient.
124-
125-
This method returns newly created, based on default constructor,
126-
object of ApiClient class or returns a copy of default
127-
ApiClient.
122+
def _get_default_or_new(cls):
123+
if cls._default is not None:
124+
return cls._default, False
125+
return cls(), True
128126

129-
:return: The ApiClient object.
130-
"""
131-
if cls._default is None:
132-
cls._default = ApiClient()
133-
return cls._default
127+
@classmethod
128+
def get_default(cls):
129+
"""Return the registered default ApiClient, or a new client."""
130+
return cls._get_default_or_new()[0]
134131

135132
@classmethod
136133
def set_default(cls, default):

0 commit comments

Comments
 (0)