Merge ~ballot/charm-graylog/+git/charm-graylog:lp1896484 into charm-graylog:master

Proposed by Benjamin Allot
Status: Merged
Merged at revision: 87ea7c490cff1597e04cee9484457701892f19c8
Proposed branch: ~ballot/charm-graylog/+git/charm-graylog:lp1896484
Merge into: charm-graylog:master
Diff against target: 23 lines (+3/-2)
1 file modified
src/lib/charms/layer/graylog/api.py (+3/-2)
Reviewer Review Type Date Requested Status
Xav Paice (community) Approve
Joe Guo (community) Needs Fixing
Paul Goins Approve
Review via email: mp+391046@code.launchpad.net

Commit message

Return as JSON the content only if not empty

Fixes: LP#1896484

To post a comment you must log in.
Revision history for this message
Paul Goins (vultaire) wrote :

+1. Arguably it could be reworked to remove the addition of a return statement, but I don't think it's really that big a deal.

review: Approve
Revision history for this message
Joe Guo (guoqiao) wrote :

requests response object has builtin `.json()` method, maybe use that ?
ref: https://requests.readthedocs.io/en/latest/api/#requests.Response.json

Also, this function is returning boolean OR json, which is a bit weird.
Although that's out of the topic for this patch.

review: Needs Fixing
Revision history for this message
Xav Paice (xavpaice) wrote :

Agreed that it would potentially be preferable to use .json(), but the change is clear and readable as is. Looking at how this function is used, the patch as is will return None where there's no response, which is accounted for in the usage.

The structure of the function itself returning bool or a dict is something to review, but outside the scope of this change.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/charms/layer/graylog/api.py b/src/lib/charms/layer/graylog/api.py
2index ca9cba1..98e52cf 100644
3--- a/src/lib/charms/layer/graylog/api.py
4+++ b/src/lib/charms/layer/graylog/api.py
5@@ -34,7 +34,7 @@ class GraylogApi:
6 self.req_timeout = 3
7 self.req_retries = 4
8
9- def request(self, path, method="GET", data={}, params=None):
10+ def request(self, path, method="GET", data={}, params=None): # noqa: C901
11 """Retrieve data by URL."""
12 if path.startswith("/"):
13 path = path[1:]
14@@ -63,7 +63,8 @@ class GraylogApi:
15 if resp.ok:
16 if method == "DELETE":
17 return True
18- return json.loads(resp.content.decode("utf-8"))
19+ if resp.content:
20+ return resp.json()
21 else:
22 msg = "API error code: {}".format(resp.status_code)
23 if charm:

Subscribers

People subscribed via source and target branches

to all changes: