Skip to content

Commit

Permalink
Add support for MISSING in params and headers
Browse files Browse the repository at this point in the history
This is an internal feature of the client class.
Entirely independently from the payload filtering process (which needs
to handle the complex payload types which may be used), a simple
helper for `dict|None` data is used to strip out MISSING from any
query params and headers before sending.

Testing also confirms the behavior of MISSING on simple JSON and
Form-POST data.
  • Loading branch information
sirosen committed Oct 25, 2023
1 parent 6ada04d commit 0c28615
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/globus_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ def request(
method=method,
url=url,
data=utils.PayloadWrapper._prepare(data),
query_params=query_params,
headers=rheaders,
query_params=utils.filter_missing(query_params),
headers=utils.filter_missing(rheaders),
encoding=encoding,
authorizer=self.authorizer,
allow_redirects=allow_redirects,
Expand Down
6 changes: 6 additions & 0 deletions src/globus_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def __repr__(self) -> str:
MISSING = MissingType()


def filter_missing(data: dict[str, t.Any] | None) -> dict[str, t.Any] | None:
if data is None:
return None
return {k: v for k, v in data.items() if v is not MISSING}


def sha256_string(s: str) -> str:
return hashlib.sha256(s.encode("utf-8")).hexdigest()

Expand Down
57 changes: 57 additions & 0 deletions tests/functional/base_client/test_filter_missing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
import urllib.parse

import pytest

from globus_sdk import utils
from globus_sdk._testing import RegisteredResponse, get_last_request, load_response


@pytest.fixture(autouse=True)
def setup_mock_responses():
load_response(
RegisteredResponse(
path="https://foo.api.globus.org/bar",
json={"foo": "bar"},
)
)
load_response(
RegisteredResponse(
path="https://foo.api.globus.org/bar",
method="POST",
json={"foo": "bar"},
)
)


def test_query_params_can_filter_missing(client):
res = client.get("/bar", query_params={"foo": "bar", "baz": utils.MISSING})
assert res.http_status == 200
req = get_last_request()
assert req.params == {"foo": "bar"}


def test_headers_can_filter_missing(client):
res = client.get("/bar", headers={"foo": "bar", "baz": utils.MISSING})
assert res.http_status == 200
req = get_last_request()
assert req.headers["foo"] == "bar"
assert "baz" not in req.headers


def test_json_body_can_filter_missing(client):
res = client.post("/bar", data={"foo": "bar", "baz": utils.MISSING})
assert res.http_status == 200
req = get_last_request()
sent = json.loads(req.body)
assert sent == {"foo": "bar"}


def test_form_body_can_filter_missing(client):
res = client.post(
"/bar", data={"foo": "bar", "baz": utils.MISSING}, encoding="form"
)
assert res.http_status == 200
req = get_last_request()
sent = urllib.parse.parse_qs(req.body)
assert sent == {"foo": ["bar"]}

0 comments on commit 0c28615

Please sign in to comment.