collective / sphinxcontrib-httpexample

Adds example directive for sphinx-contrib httpdomain
23 stars 20 forks source link

Add URL Parameters support #38

Closed ludaavics closed 5 years ago

ludaavics commented 5 years ago

Summary

Add support of the URL query parameters, using the query field. Given a line :query param_name: param_value in an http example directive, the key value pair param_name, param_value will be added to the request URL (and excluded from further processing).

See issue #35

Examples

..  http:example:: python-requests curl

    GET /items HTTP/1.1
    Host: localhost
    Accept: application/json
    Authorization: Basic dXNlcjpwYXNzd29yZA==

    :query from: 20170101
    :query to: 20171231
    :query user_id: 12
    :query limit: 20
    :query sort: date(asc)

is equivalent to

..  http:example:: python-requests curl

    GET /items?from=20170101&to=20171231&user_id=12&limit=20&sort=date(asc) HTTP/1.1
    Host: localhost
    Accept: application/json
    Authorization: Basic dXNlcjpwYXNzd29yZA==

For list parameters, pass the same field multiple times:

..  http:example:: python-requests curl

        GET /items HTTP/1.1
        Host: localhost
        Accept: application/json
        Authorization: Basic dXNlcjpwYXNzd29yZA==

        :query from: 20170101
        :query to: 20171231
        :query user_id: 12
        :query user_id: 13
        :query user_id: 15
        :query limit: 20
        :query sort:date(asc)

Finally, any parameters in the original query will be preserved:

..  http:example:: python-requests curl

        GET /items?user_id=12&user_id=13 HTTP/1.1
        Host: localhost
        Accept: application/json
        Authorization: Basic dXNlcjpwYXNzd29yZA==

        :query from: 20170101
        :query to: 20171231
        :query user_id: 15
        :query limit: 20
        :query sort:date(asc)

# /items?user_id=12&user_id=13&from=20170101&to=20171231&user_id=15&limit=20&sort=date(asc)
coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.1%) to 99.237% when pulling f75f973924fc34fae947fb8256eaeded12fa9b40 on ludaavics:master into 011e69f36e95917c2ec0e82f85c3a6aa199184a2 on collective:master.

datakurre commented 5 years ago

Please, try to follow example under tests to add example request fixture for the parsmeters support and then update snapshots-file if the output is correct (I don’t remember that by heart).

The actual implementation can be messy as long as its output is tested.

ludaavics commented 5 years ago

FYI, I couldn't get the snapshot tests to work on my mac. It looks like the output of this line may be slightly platform dependent? I get almost the same results, but with some extra whitespace, as well as extra commas in data structures (e.g. {'k1': 'v1', 'k2': 'v2', } vs {'k1': 'v1', 'k2': 'v2'} in your snapshots).

To be clear, the resulting commands are equivalent, but the string representations are different, causing a bunch of failures.

Conversely, I had to manually edit the snapshot file instead of generating it automatically.

datakurre commented 5 years ago

@ludaavics I'll merge this and create a release for you. Thanks for reporting the issue about requests builder generating platform dependent results. Need to check that on some day.