Created
April 25, 2019 08:17
-
-
Save saggie/6822bdbe54ec590c302bf7b892e04d78 to your computer and use it in GitHub Desktop.
Polling until service is ready in Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def try_login(): | |
try: | |
response = requests.post(SESSIONS_URI, auth=('username', 'password'), json={}) | |
return response.headers['...'] | |
except: | |
print('The service is not started yet.') | |
return False | |
def try_get_resources(token): | |
try: | |
response = requests.get(RESOURCES_URI, headers=...) | |
return response.getcode() == 200 | |
except: | |
print('The service is not ready yet.') | |
return False | |
if __name__ == '__main__': | |
available_token = False | |
while not available_token: | |
available_token = try_login() | |
time.sleep(10) | |
is_service_ready = False | |
while not is_service_ready: | |
is_service_ready = try_get_resources(available_token) | |
time.sleep(10) | |
print('The service is now ready!') | |
logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment