Skip to content

Commit 0000342

Browse files
Retry Key Validation in Sparse Server Failure Case (#81)
* retry key validation five times with a space of 1s in failure event * style
1 parent b58af4b commit 0000342

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

roboflow/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import sys
3+
import time
34

45
import requests
56

@@ -10,7 +11,7 @@
1011
__version__ = "0.2.20"
1112

1213

13-
def check_key(api_key, model, notebook):
14+
def check_key(api_key, model, notebook, num_retries=0):
1415
if type(api_key) is not str:
1516
raise RuntimeError(
1617
"API Key is of Incorrect Type \n Expected Type: "
@@ -28,11 +29,23 @@ def check_key(api_key, model, notebook):
2829
else:
2930
# validate key normally
3031
response = requests.post(API_URL + "/?api_key=" + api_key)
31-
r = response.json()
3232

33-
if "error" in r or response.status_code != 200:
33+
if response.status_code == 401:
3434
raise RuntimeError(response.text)
35+
36+
if response.status_code != 200:
37+
# retry 5 times
38+
if num_retries < 5:
39+
print("retrying...")
40+
time.sleep(1)
41+
num_retries += 1
42+
return check_key(api_key, model, notebook, num_retries)
43+
else:
44+
raise RuntimeError(
45+
"There was an error validating the api key with Roboflow server."
46+
)
3547
else:
48+
r = response.json()
3649
return r
3750
else: # then you're using a dummy key
3851
sys.stdout.write(

0 commit comments

Comments
 (0)