Skip to content

Commit

Permalink
Add json parameter for POST, PATCH, and PUT
Browse files Browse the repository at this point in the history
This parameter gets passed through to requests, which
automatically JSON-encodes the value and sets an
appropriate Content-Type header.

Support for sending bodies that are actually
JSON-encoded is needed for upcoming PRAW features
such as polls.
  • Loading branch information
jarhill0 committed Apr 29, 2020
1 parent 9da2a13 commit 4f74bd1
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ def _objectify_request(
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
files: Optional[Dict[str, IO]] = None,
json=None,
method: str = "",
params: Optional[Union[str, Dict[str, str]]] = None,
path: str = "",
Expand All @@ -563,6 +564,9 @@ def _objectify_request(
of the request (default: None).
:param files: Dictionary, filename to file (like) object mapping
(default: None).
:param json: JSON-serializable object to send in the body
of the request with a Content-Type header of application/json
(default: None). If ``json`` is provided, ``data`` should not be.
:param method: The HTTP method (e.g., GET, POST, PUT, DELETE).
:param params: The query parameters to add to the request (default:
None).
Expand All @@ -571,7 +575,12 @@ def _objectify_request(
"""
return self._objector.objectify(
self.request(
data=data, files=files, method=method, params=params, path=path
data=data,
files=files,
json=json,
method=method,
params=params,
path=path,
)
)

Expand All @@ -597,15 +606,21 @@ def patch(
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
json=None,
) -> Any:
"""Return parsed objects returned from a PATCH request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body
of the request (default: None).
:param json: JSON-serializable object to send in the body
of the request with a Content-Type header of application/json
(default: None). If ``json`` is provided, ``data`` should not be.
"""
return self._objectify_request(data=data, method="PATCH", path=path)
return self._objectify_request(
data=data, method="PATCH", path=path, json=json
)

def post(
self,
Expand All @@ -615,6 +630,7 @@ def post(
] = None,
files: Optional[Dict[str, IO]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
json=None,
) -> Any:
"""Return parsed objects returned from a POST request to ``path``.
Expand All @@ -625,13 +641,18 @@ def post(
(default: None).
:param params: The query parameters to add to the request (default:
None).
:param json: JSON-serializable object to send in the body
of the request with a Content-Type header of application/json
(default: None). If ``json`` is provided, ``data`` should not be.
"""
data = data or {}
if json is None:
data = data or {}
try:
return self._objectify_request(
data=data,
files=files,
json=json,
method="POST",
params=params,
path=path,
Expand Down Expand Up @@ -660,15 +681,21 @@ def put(
data: Optional[
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
json=None,
):
"""Return parsed objects returned from a PUT request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body
of the request (default: None).
:param json: JSON-serializable object to send in the body
of the request with a Content-Type header of application/json
(default: None). If ``json`` is provided, ``data`` should not be.
"""
return self._objectify_request(data=data, method="PUT", path=path,)
return self._objectify_request(
data=data, json=json, method="PUT", path=path
)

def random_subreddit(self, nsfw: bool = False) -> Subreddit:
"""Return a random lazy instance of :class:`~.Subreddit`.
Expand Down Expand Up @@ -709,6 +736,7 @@ def request(
Union[Dict[str, Union[str, Any]], bytes, IO, str]
] = None,
files: Optional[Dict[str, IO]] = None,
json=None,
) -> Any:
"""Return the parsed JSON data returned from a request to URL.
Expand All @@ -720,8 +748,15 @@ def request(
of the request (default: None).
:param files: Dictionary, filename to file (like) object mapping
(default: None).
:param json: JSON-serializable object to send in the body
of the request with a Content-Type header of application/json
(default: None). If ``json`` is provided, ``data`` should not be.
"""
if data and json:
raise ClientException(
"At most one of `data` and `json` is supported."
)
try:
return self._core.request(
method,
Expand All @@ -730,6 +765,7 @@ def request(
files=files,
params=params,
timeout=self.config.timeout,
json=json,
)
except BadRequest as exception:
try:
Expand Down

0 comments on commit 4f74bd1

Please sign in to comment.