0

I am using the azure-keyvault python package to authenticate with azure. I am trying to add retry logic to the api call client.get_secret(secretName) shown below.

import os
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential

keyVaultName = os.environ["KEY_VAULT_NAME"]
KVUri = f"https://{keyVaultName}.vault.azure.net"

credential = DefaultAzureCredential()
client = SecretClient(vault_url=KVUri, credential=credential)

secretName = input("Input a name for your secret > ")
secretValue = input("Input a value for your secret > ")

print(f"Creating a secret in {keyVaultName} called '{secretName}' with the value '{secretValue}' ...")

client.set_secret(secretName, secretValue)

print(" done.")

print(f"Retrieving your secret from {keyVaultName}.")

retrieved_secret = client.get_secret(secretName)

I want to use the retrying (https://pypi.org/project/retrying/) python package as below example:

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
    print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"

and my question is how can I add @retry(...) to a method call like client.get_secret(secretName), where client is an object of an imported package?

Thank you

3awny
  • 319
  • 1
  • 2
  • 10
  • 1
    You need to write your own function containing the parts of the program that you want to repeat until it succeeds – ivvija May 11 '22 at 14:02
  • @ivvija is there a way like with overriding the function? – 3awny May 11 '22 at 15:03
  • If you want to use the `retrying` package, I would second what @ivvija recommended. I'm not familiar with `retrying`, but it seems that you would need to implement your own class in order to override the function. You can configure some retrying capabilities on the imported `SecretClient` by using a custom `RetryPolicy` from `azure-core`, though ([policy list](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#available-policies)). You can provide a retry policy to the client's constructor as a `retry_policy` keyword argument. – mccoyp May 11 '22 at 17:42
  • @McCoyPatiño are you aware of any other retry packages that would make this easier? I'm open to suggestions – 3awny May 12 '22 at 11:09

0 Answers0