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

Add option to ignore nested parameters in a JSON request body #676

Open
timbmg opened this issue Aug 4, 2022 · 4 comments
Open

Add option to ignore nested parameters in a JSON request body #676

timbmg opened this issue Aug 4, 2022 · 4 comments

Comments

@timbmg
Copy link

timbmg commented Aug 4, 2022

Is it possible to ignore a nested parameter? For example the data of my requests looks like this:

{
  "data": {
    "key": "value",
    "timestamp": "2022-08-04"
  }
}

In this example, I would like to ignore timestamp but not key when creating the cache key. Yes, I could overwrite the cache_key function, but feels like this should be part of the library? Maybe like this: data.timestamp

@timbmg timbmg added the question label Aug 4, 2022
@JWCook
Copy link
Member

JWCook commented Aug 7, 2022

Interesting idea. Do you happen to have any cases where the key you want to ignore is more than 1 level deep (like data.key_1.key_2)?

If not, that would make things much easier. I think having a single "root" element in a request body (data in your example) is fairly common. We could possibly add an option to apply ignored_parameters to anything under that root key. That would be a much simpler change to make than supporting arbitrary levels of JSON keys using dot notation.

@timbmg
Copy link
Author

timbmg commented Aug 9, 2022

No, I don't have nested keys that are more than one level deep. So for my use case, a single level would be sufficient.

@timbmg
Copy link
Author

timbmg commented Aug 12, 2022

I could work on a PR on this @JWCook
As far as I see, this would be adding some logic to the filter_sort_* functions oft cache_keys.py.

@JWCook
Copy link
Member

JWCook commented Aug 15, 2022

That would be great! I don't think the filter_sort_* functions need to change, but normalize_json_body and everything up the call chain from there will need to change.

To get you started, the new normalize_json_body() would look something like this:

def normalize_json_body(
    original_body: Union[str, bytes],
    ignored_parameters: ParamList,
    content_root_key: str = None,
) -> Union[str, bytes]:
    """Normalize and filter a request body with serialized JSON data"""
    if len(original_body) <= 2 or len(original_body) > MAX_NORM_BODY_SIZE:
        return original_body

    try:
        body = json.loads(decode(original_body))
        if content_root_key and isinstance(body, dict) and content_root_key in body:
            body[content_root_key] = filter_sort_json(body[content_root_key], ignored_parameters)
        else:
            body = filter_sort_json(body, ignored_parameters)
        return json.dumps(body)
    # If it's invalid JSON, then don't mess with it
    except (AttributeError, TypeError, ValueError):
        logger.debug('Invalid JSON body')
        return original_body

Then:

Usage would then look like:

session = CachedSession(content_root_key='data')

Does that sound reasonable to you?

@JWCook JWCook changed the title Ignore nested parameter Add option to ignore nested parameters in a JSON request body Aug 15, 2022
@JWCook JWCook added this to the v1.0 milestone Aug 15, 2022
@JWCook JWCook removed this from the v1.0 milestone Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants