Skip to content

Commit

Permalink
Moved is_string_type and utf8_urlencode to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagiove committed Feb 12, 2020
1 parent 54b85e5 commit daace44
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/RequestsLibrary/RequestsKeywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from robot.utils.asserts import assert_equal

from RequestsLibrary import utils
from RequestsLibrary.compat import httplib, urlencode, PY3
from RequestsLibrary.compat import httplib, PY3
from RequestsLibrary.exceptions import InvalidResponse


Expand Down Expand Up @@ -934,7 +934,7 @@ def _common_request(
self._capture_output()
resp = method_function(
self._get_url(session, uri),
params=self._utf8_urlencode(kwargs.pop('params', None)),
params=utils.utf8_urlencode(kwargs.pop('params', None)),
timeout=self._get_timeout(kwargs.pop('timeout', None)),
cookies=self.cookies,
verify=self.verify,
Expand Down Expand Up @@ -1007,20 +1007,6 @@ def _print_debug(self):
logger.debug(debug_info)


def _utf8_urlencode(self, data):
if self._is_string_type(data):
return data.encode('utf-8')

if not isinstance(data, dict):
return data

utf8_data = {}
for k, v in data.items():
if self._is_string_type(v):
v = v.encode('utf-8')
utf8_data[k] = v
return urlencode(utf8_data)

def _format_data_according_to_header(self, session, data, headers):
# Merged headers are already case insensitive
headers = utils.merge_headers(session, headers)
Expand All @@ -1031,9 +1017,9 @@ def _format_data_according_to_header(self, session, data, headers):
if str(data).strip():
data = json.dumps(data)
elif headers['Content-Type'].find("application/x-www-form-urlencoded") != -1:
data = self._utf8_urlencode(data)
data = utils.utf8_urlencode(data)
else:
data = self._utf8_urlencode(data)
data = utils.utf8_urlencode(data)

return data

Expand Down Expand Up @@ -1095,10 +1081,4 @@ def _log_response(method, response):
response.text)


@staticmethod
def _is_string_type(data):
if PY3 and isinstance(data, str):
return True
elif not PY3 and isinstance(data, unicode):
return True
return False

24 changes: 24 additions & 0 deletions src/RequestsLibrary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from requests.structures import CaseInsensitiveDict

from RequestsLibrary.exceptions import UnknownStatusError
from RequestsLibrary.compat import urlencode, PY3


def parse_named_status(status_code):
Expand Down Expand Up @@ -57,3 +58,26 @@ def json_pretty_print(content):
separators=(
',',
': '))


def is_string_type(data):
if PY3 and isinstance(data, str):
return True
elif not PY3 and isinstance(data, unicode):
return True
return False


def utf8_urlencode(data):
if is_string_type(data):
return data.encode('utf-8')

if not isinstance(data, dict):
return data

utf8_data = {}
for k, v in data.items():
if is_string_type(v):
v = v.encode('utf-8')
utf8_data[k] = v
return urlencode(utf8_data)

0 comments on commit daace44

Please sign in to comment.