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

How to populate params value in webhook setup state call #287

Closed
antoniogamizbadger opened this issue Feb 11, 2022 · 2 comments
Closed

How to populate params value in webhook setup state call #287

antoniogamizbadger opened this issue Feb 11, 2022 · 2 comments
Assignees

Comments

@antoniogamizbadger
Copy link

Hi,

I see that when calling the webhook setup state call, I receive this in the body of the POST call:

{
  "consumer": "Consumer",
  "state": "User exists and has resources",
  "states": [
    "User exists and has resources"
  ],
  "params": {}
}

I see the key params and I do not really know how to populate that. I also do not know the meaning of states. Is there any docs for this? I have not been able to found any.

I also asked this in Stack Overflow.

@antoniogamizbadger
Copy link
Author

I fount this documentation: https://docs.pact.io/getting_started/provider_states, but it does not explain at all the content of the body.

@YOU54F
Copy link
Member

YOU54F commented Aug 3, 2023

The TL;DR is states and params are V3 Pact features. states is used in MessagePact implementation in Python (which uses the pact-message ruby gem). params is not used in pact-python backed by the ruby core.

There are examples of use in this repo of state handlers,

https://github.com/pact-foundation/pact-python/blob/master/examples/fastapi_provider/tests/pact_provider.py
https://github.com/pact-foundation/pact-python/blob/master/examples/flask_provider/tests/pact_provider.py

you can ignore the states array for http tests

https://github.com/pact-foundation/pact-provider-verifier#api-with-provider-states

Ignore the states array that you will see if you happen to print out the live provider state set up request body - that was an attempt to make the set up call forwards compatible with the v3 pact specification, which allows multiple provider states. Unfortunately, this forwards compatibilty attempt failed, because the v3 provider states support a map of params, not just a name, so it should have been { "state": { "name": "PROVIDER_STATE, "params": {...} } }. See the next section for the actual v3 support.

Multiple provider states, and those which use params will become relevant in the v3 specification (provided by the rust core) which will be delivered post #367 is merged.

states are mapped in the http proxy for MessagePact in pact-python, see here in code

def _match_states(payload):
"""Match states in payload against stored message handlers."""
log.debug(f'Find handler from payload: {payload}')
handlers = items["states"]
states = handlers['messageHandlers']
log.debug(f'Setup states: {handlers}')
provider_states = payload['providerStates']
for state in provider_states:
matching_state = state['name']
if matching_state in states:
return states[matching_state]
raise HTTPException(status_code=500, detail='No matched handler.')

@YOU54F YOU54F closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants