Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change client to accept json data directly #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyopnsense/client.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into the same issue this week and would aggree to this solution, as the post request always needs to be json.
So changing the post to only have json and not adding a post for json besides the raw data one is the correct solution in my mind also.

Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def _get(self, endpoint, raw=False):
)
return self._process_response(response, raw)

def _post(self, endpoint, body, raw=False):
def _post(self, endpoint, data, raw=False):
req_url = "{}/{}".format(self.base_url, endpoint)
response = requests.post(
req_url,
data=body,
json=data,
verify=self.verify_cert,
auth=(self.api_key, self.api_secret),
timeout=self.timeout,
Expand Down
8 changes: 4 additions & 4 deletions pyopnsense/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def test_post_success(self, request_mock):
response_mock.text = json.dumps({"a": "body"})
request_mock.return_value = response_mock
opnclient = client.OPNClient("", "", "")
resp = opnclient._post("fake_url", "body")
resp = opnclient._post("fake_url", {})
self.assertEqual({"a": "body"}, resp)
request_mock.assert_called_once_with(
"/fake_url", data="body", auth=("", ""), timeout=5, verify=False
"/fake_url", json={}, auth=("", ""), timeout=5, verify=False
)

@mock.patch("requests.post")
Expand All @@ -71,7 +71,7 @@ def test_post_failures(self, request_mock):
response_mock.text = json.dumps({"a": "body"})
request_mock.return_value = response_mock
opnclient = client.OPNClient("", "", "")
self.assertRaises(exceptions.APIException, opnclient._post, "fake_url", "body")
self.assertRaises(exceptions.APIException, opnclient._post, "fake_url", {})
request_mock.assert_called_once_with(
"/fake_url", data="body", auth=("", ""), timeout=5, verify=False
"/fake_url", json={}, auth=("", ""), timeout=5, verify=False
)
Loading