Skip to content

Commit 82b24cb

Browse files
authored
Merge pull request #144 from KZHIWEI/master
add retry when request to lebesgue.
2 parents 713391b + b230d28 commit 82b24cb

File tree

1 file changed

+45
-17
lines changed
  • dpdispatcher/dpcloudserver

1 file changed

+45
-17
lines changed

dpdispatcher/dpcloudserver/api.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import json
3+
import time
4+
35
try:
46
import oss2
57
from oss2 import SizedFileAdapter, determine_part_size
@@ -23,44 +25,70 @@ def __init__(self, email, password):
2325

2426
def get(self, url, params, retry=0):
2527
headers = {'Authorization': "jwt " + self._token}
26-
ret = requests.get(
27-
urljoin(API_HOST, url),
28-
params=params,
29-
timeout=HTTP_TIME_OUT,
30-
headers=headers
31-
)
28+
ret = None
29+
for retry_count in range(3):
30+
try:
31+
ret = requests.get(
32+
urljoin(API_HOST, url),
33+
params=params,
34+
timeout=HTTP_TIME_OUT,
35+
headers=headers
36+
)
37+
except Exception as e:
38+
dlog.error(f"request error {e}")
39+
continue
40+
if ret.ok:
41+
break
42+
else:
43+
dlog.error(f"request error status_code:{ret.status_code} reason: {ret.reason} body: \n{ret.text}")
44+
time.sleep(retry_count * 10)
45+
if ret is None:
46+
raise ConnectionError("request fail")
3247
# print(url,'>>>', params, '<<<', ret.text)
48+
ret.raise_for_status()
3349
ret = json.loads(ret.text)
3450
if ret['code'] == RETCODE.TOKENINVALID and retry <= 3:
35-
dlog.debug("debug: token expire, refresh token")
51+
dlog.error("debug: token expire, refresh token")
3652
if self._login_data is not None:
3753
self.refresh_token()
3854
ret = self.get(url, params, retry=retry + 1)
3955
return ret
4056
if ret['code'] != RETCODE.OK:
4157
raise ValueError(f"{url} Error: {ret['code']} {ret['message']}")
42-
4358
return ret['data']
4459

4560
def post(self, url, params, retry=0):
4661
headers = {'Authorization': "jwt " + self._token}
47-
ret = requests.post(
48-
urljoin(API_HOST, url),
49-
json=params,
50-
timeout=HTTP_TIME_OUT,
51-
headers=headers
52-
)
53-
# print(url,'>>>', params, '<<<', ret.text)
62+
ret = None
63+
for retry_count in range(3):
64+
try:
65+
ret = requests.post(
66+
urljoin(API_HOST, url),
67+
json=params,
68+
timeout=HTTP_TIME_OUT,
69+
headers=headers
70+
)
71+
except Exception as e:
72+
dlog.error(f"request error {e}")
73+
continue
74+
if ret.ok:
75+
break
76+
else:
77+
dlog.error(f"request error status_code:{ret.status_code} reason: {ret.reason} body: \n{ret.text}")
78+
time.sleep(retry_count)
79+
if ret is None:
80+
raise ConnectionError("request fail")
81+
ret.raise_for_status()
5482
ret = json.loads(ret.text)
83+
# print(url,'>>>', params, '<<<', ret.text)
5584
if ret['code'] == RETCODE.TOKENINVALID and retry <= 3:
56-
dlog.debug("debug: token expire, refresh token")
85+
dlog.error("debug: token expire, refresh token")
5786
if self._login_data is not None:
5887
self.refresh_token()
5988
ret = self.post(url, params, retry=retry + 1)
6089
return ret
6190
if ret['code'] != RETCODE.OK:
6291
raise ValueError(f"{url} Error: {ret['code']} {ret['message']}")
63-
6492
return ret['data']
6593

6694
def refresh_token(self):

0 commit comments

Comments
 (0)