Skip to content

Commit

Permalink
Add timeout by default to requests, fix issues when SM is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
NaniteBased committed Oct 21, 2020
1 parent 4871f14 commit ee289e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
23 changes: 13 additions & 10 deletions Interfaces/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,18 @@ def GetNsdRequirements(self, nsd: str) -> Optional[Metal]:
return None

def GetBaseSliceDescriptors(self) -> List[str]:
url = f"{self.api_url}/base_slice_des"
response = self.HttpGet(url, {"Accept": "application/json"})
data: List[Dict] = self.ResponseToJson(response)
res = []
for desc in data:
descId = desc.get('Slice_des_ID', desc.get('base_slice_des_id', None))
if descId is not None:
res.append(descId)
return res

try:
url = f"{self.api_url}/base_slice_des"
response = self.HttpGet(url, {"Accept": "application/json"})
data: List[Dict] = self.ResponseToJson(response)
res = []
for desc in data:
descId = desc.get('Slice_des_ID', desc.get('base_slice_des_id', None))
if descId is not None:
res.append(descId)
return res
except Exception as e:
Log.E(f"Exception while retrieving Base Slice Descriptors: {e}")
return []


20 changes: 11 additions & 9 deletions REST/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Payload(Enum):

class RestClient:
HEADERS = {'Accept-Language': 'en-US;q=0.5,en;q=0.3'}
RETRIES = 10
RETRIES = 3
FILENAME_PATTERN = re.compile(r".*filename=\"(.*)\"")

def __init__(self, api_host, api_port, suffix, https=False, insecure=False):
Expand Down Expand Up @@ -69,15 +69,16 @@ def GetFilename(self, content_disposition):
return result.group(1)
return "unknown_filename"

def HttpGet(self, url, extra_headers=None):
def HttpGet(self, url, extra_headers=None, timeout=10):
traceId = self.GetTraceId()
extra_headers = {} if extra_headers is None else extra_headers

self.Trace(traceId, url, 'GET', headers=extra_headers)
return self.DumpResponse(traceId, self.pool.request('GET', url, headers=extra_headers, retries=self.RETRIES))
return self.DumpResponse(traceId, self.pool.request('GET', url, headers=extra_headers,
retries=self.RETRIES, timeout=timeout))

def HttpPost(self, url, extra_headers=None, body: Optional[Union[str, Dict]] = None,
files=None, payload: Payload = None):
files=None, payload: Payload = None, timeout=10):
traceId = self.GetTraceId()
extra_headers = {} if extra_headers is None else extra_headers

Expand All @@ -101,26 +102,27 @@ def HttpPost(self, url, extra_headers=None, body: Optional[Union[str, Dict]] = N
if files is None:
return self.DumpResponse(traceId,
self.pool.request('POST', url, body=body or '',
headers={**self.HEADERS, **extra_headers}, retries=self.RETRIES))
headers={**self.HEADERS, **extra_headers},
retries=self.RETRIES, timeout=timeout))
else:
return self.DumpResponse(traceId,
post(f"{self.api_url}{url}", data=body, headers={**self.HEADERS, **extra_headers},
files=files, verify=not self.insecure))

def HttpPatch(self, url, extra_headers=None, body=''):
def HttpPatch(self, url, extra_headers=None, body='', timeout=10):
traceId = self.GetTraceId()
extra_headers = {} if extra_headers is None else extra_headers
self.Trace(traceId, url, 'PATCH', headers=extra_headers, body=body)
return self.DumpResponse(traceId, self.pool.request('PATCH', url, body=body,
headers={**self.HEADERS, **extra_headers},
retries=self.RETRIES))
retries=self.RETRIES, timeout=timeout))

def HttpDelete(self, url, extra_headers=None):
def HttpDelete(self, url, extra_headers=None, timeout=10):
traceId = self.GetTraceId()
extra_headers = {} if extra_headers is None else extra_headers
self.Trace(traceId, url, 'DELETE', headers=extra_headers)
return self.DumpResponse(traceId, self.pool.request('DELETE', url, headers={**self.HEADERS, **extra_headers},
retries=self.RETRIES))
retries=self.RETRIES, timeout=timeout))

@staticmethod
def ResponseStatusCode(response) -> int:
Expand Down

0 comments on commit ee289e9

Please sign in to comment.