Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] how can we utilize the retry mechanism for requests made via sdk. #6503

Closed
dnome2 opened this issue Sep 15, 2017 · 3 comments
Closed

Comments

@dnome2
Copy link

dnome2 commented Sep 15, 2017

Description

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.

Swagger-codegen version

2.2.3

Swagger declaration file content or url
@dnome2 dnome2 changed the title how can we utilize the retry mechanism for requests made via sdk. [python] how can we utilize the retry mechanism for requests made via sdk. Sep 15, 2017
@wing328
Copy link
Contributor

wing328 commented Sep 24, 2017

cc @taxpon @frol @mbohlool

@arcward
Copy link
Contributor

arcward commented Sep 24, 2017

@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:

  • When creating new client like 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

@dnome2
Copy link
Author

dnome2 commented Sep 26, 2017

Thanks a ton @arcward ! Will try this out and let you know if i see any issues.

@wing328 wing328 closed this as completed Dec 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants