using python sdk how can we pass the retry parameter/argument in the poolmanager object? Say if the request takes a longer time than normal for whatever reason and so we would like to pass a retry number, can we utilize the retry class supported by urllib3? Appreciate your help.
2.2.3
cc @taxpon @frol @mbohlool
@dnome2 You can access PoolManager through ApiClient.rest_client.pool_manager. If you want changes to apply to every client, you'd need to create ApiClient directly, making sure to pass it whenever you create a new client object.
Using pet store as an example:
PetApi, it creates its own instance of ApiClient unless you pass your own via the api_client kw (as PetApi.api_client)ApiClient creates its own RESTClientObject (as ApiClient.rest_client)RESTClientObject creates its own urllib3.PoolManager (as RESTClientObject.pool_manager)So that's why you'd need to set all new clients to use that particular ApiClient instance- if you don't, they'll create their own with default settings. I guess that might be useful if you want different APIs to have different PoolManager settings or something.
Here's an example of how you'd set and make sure it applies to all the pet store's API clients:
import swagger_client
api_client = swagger_client.ApiClient()
api_client.rest_client.pool_manager.connection_pool_kw['retries'] = 10
pet_api = swagger_client.PetApi(api_client=api_client)
user_api = swagger_client.UserApi(api_client=api_client)
store_api = swagger_client.StoreApi(api_client=api_client)
I just tested it offline on my end and verified urllib3 made 10 attempts, while the default is 3
Sidenote/tangent: If that's annoying to do every time, and you don't mind editing the generated swagger_client library directly, you can sorta make this the default behavior by having ApiClient instantiate the clients on its own, passing self when it does. Quick dirty example, in ApiClient.__init__, you might do:
from .apis import PetApi
self.pet_api = PetApi(api_client=self)
That way you'd only need to create ApiClient and update the pool manager settings once, and could access the other clients through it like api_client.pet_api.find_pets_by_status('foo') and any changes you make via ApiClient later would be reflected in all the clients assigned to it. If you're creating multiple clients it also avoids the need to explicitly set the clients all over again for each one
Thanks a ton @arcward ! Will try this out and let you know if i see any issues.
Most helpful comment
@dnome2 You can access PoolManager through
ApiClient.rest_client.pool_manager. If you want changes to apply to every client, you'd need to createApiClientdirectly, making sure to pass it whenever you create a new client object.Using pet store as an example:
PetApi, it creates its own instance ofApiClientunless you pass your own via theapi_clientkw (asPetApi.api_client)ApiClientcreates its ownRESTClientObject(asApiClient.rest_client)RESTClientObjectcreates its ownurllib3.PoolManager(asRESTClientObject.pool_manager)So that's why you'd need to set all new clients to use that particular
ApiClientinstance- if you don't, they'll create their own with default settings. I guess that might be useful if you want different APIs to have different PoolManager settings or something.Here's an example of how you'd set and make sure it applies to all the pet store's API clients:
I just tested it offline on my end and verified urllib3 made 10 attempts, while the default is 3
Sidenote/tangent: If that's annoying to do every time, and you don't mind editing the generated swagger_client library directly, you can sorta make this the default behavior by having
ApiClientinstantiate the clients on its own, passingselfwhen it does. Quick dirty example, inApiClient.__init__, you might do:That way you'd only need to create
ApiClientand update the pool manager settings once, and could access the other clients through it likeapi_client.pet_api.find_pets_by_status('foo')and any changes you make via ApiClient later would be reflected in all the clients assigned to it. If you're creating multiple clients it also avoids the need to explicitly set the clients all over again for each one