def try_it_n_times(operation, expected_error_codes, custom_error='<STR_LIT>', n=<NUM_LIT:10>):
for i in itertools.count():<EOL><INDENT>try:<EOL><INDENT>operation()<EOL>break<EOL><DEDENT>except UpCloudAPIError as e:<EOL><INDENT>if e.error_code not in expected_error_codes:<EOL><INDENT>raise e<EOL><DEDENT>sleep(<NUM_LIT:3>)<EOL><DEDENT>if i >= n - <NUM_LIT:1>:<EOL><INDENT>raise UpCloudClientError(custom_error)<EOL><DEDENT><DEDENT>
Try a given operation (API call) n times.

Raises if the API call fails with an error_code that is not expected.
Raises if the API call has not succeeded within n attempts.
Waits 3 seconds betwee each attempt.