Skip to content

Commit d08bd03

Browse files
committed
Handle redirects
1 parent 647ded9 commit d08bd03

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

api/callers/api_caller.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from exceptions import ResponseObjectNotExistError
33
from exceptions import UrlBuildError
44
from exceptions import JsonParseError
5-
from requests.auth import HTTPBasicAuth
5+
from exceptions import ConfigError
66
import json
77

88

@@ -57,7 +57,10 @@ def call(self, request_handler, headers={'User-agent': 'VxApi Connector'}):
5757
caller_function = getattr(request_handler, self.request_method_name)
5858
headers['api-key'] = self.api_key
5959

60-
self.api_response = caller_function(self.get_full_endpoint_url(), data=self.data, params=self.params, files=self.files, headers=headers, verify=False)
60+
self.api_response = caller_function(self.get_full_endpoint_url(), data=self.data, params=self.params, files=self.files, headers=headers, verify=False, allow_redirects=False)
61+
if self.if_request_redirect():
62+
raise ConfigError('Got redirect while trying to reach server URL. Please try to update it and pass the same URL base which is visible in the browser URL bar. '
63+
'For example: it should be \'https://www.hybrid-analysis.com\', instead of \'http://www.hybrid-analysis.com\' or \'https://hybrid-analysis.com\'')
6164
self.api_result_msg = self.prepare_response_msg()
6265

6366
def attach_data(self, options):
@@ -74,6 +77,9 @@ def attach_files(self, files):
7477
def if_request_success(self):
7578
return int(int(self.api_response.status_code) / 200) == 1 # 20x status code
7679

80+
def if_request_redirect(self):
81+
return int(int(self.api_response.status_code) / 300) == 1 # 30x status code
82+
7783
def prepare_response_msg(self) -> str:
7884
if self.api_response is None:
7985
raise ResponseObjectNotExistError('It\'s not possible to get response message since API was not called.')

0 commit comments

Comments
 (0)