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

Capture Group Support #718

Open
jcampbell05 opened this issue May 23, 2024 · 2 comments
Open

Capture Group Support #718

jcampbell05 opened this issue May 23, 2024 · 2 comments

Comments

@jcampbell05
Copy link

Describe the bug

It would be great to be able to use capture groups in the Regexp and be able to read their value in the request passed to the callbacks, currently we have to store the regexp in a common place and parse twice.

Additional context

No response

Version of responses

latest

Steps to Reproduce

N/a

Expected Result

N/a

Actual Result

N/a

@markstory
Copy link
Member

How would you expect to define and use the captured groups? Perhaps an example of what you want to do but cannot do easily today.

@jcampbell05
Copy link
Author

How would you expect to define and use the captured groups? Perhaps an example of what you want to do but cannot do easily today.

For example I would love to be able to do this:

def fetch_invoice(request):
      print(f"Invoice {request.match.invoice_id}")

invoice_regexp = f"https://{config.CHARGEBEE_SITE}.chargebee.com/api/v2/invoices/(?P<invoice_id>.+)"

self.responses.add_callback(
                responses.GET,
                re.compile(invoice_regexp),
                callback=fetch_invoice
)

Right now I have to write it like so:

def fetch_invoice(request):
            match = re.search(invoice_regexp, request.url)
            invoice_id = match.group("invoice_id")

invoice_regexp = f"https://{config.CHARGEBEE_SITE}.chargebee.com/api/v2/invoices/(?P<invoice_id>.+)"

self.responses.add_callback(
                responses.GET,
                re.compile(invoice_regexp),
                callback=fetch_invoice
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants