Skip to content

Commit

Permalink
Merge pull request #30 from 0xliam/always_ensure_ascii
Browse files Browse the repository at this point in the history
fix: always convert json payloads to ascii
  • Loading branch information
markciecior authored Jul 30, 2024
2 parents 70a920e + 00b597e commit 7acb132
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions connectpyse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
cw_api_settings = json.load(fin)

API_URL = cw_api_settings['API_URL']
ENSURE_ASCII = cw_api_settings['ENSURE_ASCII'] if 'ENSURE_ASCII' in cw_api_settings else False
_cid = cw_api_settings['COMPANYID']
_pubk = cw_api_settings['PUBLICKEY']
_privk = cw_api_settings['PRIVATEKEY']
Expand All @@ -31,4 +32,5 @@
basic_auth['clientId'] = _clientId
except FileNotFoundError as e:
API_URL = ''
ENSURE_ASCII = False
basic_auth = {}
5 changes: 3 additions & 2 deletions connectpyse/cw_controller.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Parent class for module controller classes
from .config import API_URL, basic_auth
from .config import API_URL, basic_auth, ENSURE_ASCII
from .restapi import Client


class CWController(Client):
def __init__(self, url=None, auth=None):
def __init__(self, url=None, auth=None, ensure_ascii=None):
# self.module_url comes from child
# self.module comes from child
# self._class comes from child
Expand All @@ -16,6 +16,7 @@ def __init__(self, url=None, auth=None):
self.pageSize = ''
self.API_URL = url if url is not None else API_URL
self.basic_auth = auth if auth is not None else basic_auth
self.ensure_ascii = ensure_ascii if ensure_ascii is not None else ENSURE_ASCII
super().__init__('{}/{}'.format(self.API_URL, self.module_url))

def _format_user_params(self):
Expand Down
12 changes: 7 additions & 5 deletions connectpyse/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ def __getattribute__(self, attr):
except AttributeError:
return Endpoint(
self.root_path,
attr
attr,
ensure_ascii=self.ensure_ascii
)


class Endpoint(object):

def __init__(self, root_path, endpoint):
def __init__(self, root_path, endpoint, ensure_ascii):
self.endpoint = endpoint
self.root_path = root_path
self.ensure_ascii = ensure_ascii

def _url(self, path, *args):
url = "{}/{}/".format(self.root_path, path)
Expand Down Expand Up @@ -93,7 +95,7 @@ def post(self, user_data, the_id=None, user_params={}, user_headers={}, files=No
url = self._url(self.endpoint)

if user_data:
strjsondata = json.dumps(user_data, ensure_ascii=False)
strjsondata = json.dumps(user_data, ensure_ascii=self.ensure_ascii)
resp = req.post(
url,
data=strjsondata,
Expand Down Expand Up @@ -121,7 +123,7 @@ def post(self, user_data, the_id=None, user_params={}, user_headers={}, files=No

def put(self, the_id, user_data, user_params={}, user_headers={}):

strjsondata = json.dumps(user_data, ensure_ascii=False)
strjsondata = json.dumps(user_data, ensure_ascii=self.ensure_ascii)

resp = req.put(
self._url(self.endpoint, the_id),
Expand All @@ -141,7 +143,7 @@ def put(self, the_id, user_data, user_params={}, user_headers={}):

def patch(self, the_id, user_data, user_params={}, user_headers={}):

strjsondata = json.dumps(user_data, ensure_ascii=False)
strjsondata = json.dumps(user_data, ensure_ascii=self.ensure_ascii)

resp = req.patch(
self._url(self.endpoint, the_id),
Expand Down

0 comments on commit 7acb132

Please sign in to comment.