collective / sphinxcontrib-httpexample

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

Quote with " rather than ' (or allow to choose) #52

Closed tuukkamustonen closed 3 years ago

tuukkamustonen commented 4 years ago

The parameters at least for the curl snippets that the extension creates get quoted using '. In Bash (and maybe all POSIX), these strings are not interpreted like the ones quoted with ".

I am using environment variables here and there, to refer to certain given values (so the user doesn't have to repeat themselves when following the guide). However, those variables are now not evaluated. An example that would fail:

curl -i -X POST https://example.com -H 'X-Api-Key: $APIGW_KEY'

Would there be harm in changing the quotation to use double quotes instead of single? At least wrapping a JSON body inside double quotes would introduce ugly escaping, and probably not many users would need double quotes, so maybe default as now but allow to choose double quotes?

datakurre commented 4 years ago

I agree we could default to " for everything else than JSON data arguments. I'm just looking for the easiest solution to do that. Right now we have been using Python's builtin pipes.quote / shlex.quote for escaping and it seems to be hardcoded to wrap strings with '.

datakurre commented 4 years ago

Preview at https://sphinxcontrib-httpexample.readthedocs.io/en/use-double-quotes/

tuukkamustonen commented 4 years ago

Double quotes there now indeed, thanks for the speedy actions!

Would you consider supporting double quotes also for JSON bodies? Some :rule: double or a conf.py option. I do understand it's not a widely requested feature, though.

tuukkamustonen commented 4 years ago

There's also this:

http POST https://example.com <<EOF
{
    "some": {
        "data": "$MY_VARIABLE"
    }
}
EOF

I would assume that's POSIX syntax, so I guess it wouldn't work on Windows/Mac(?).

In the end, the strength of the extension is that it can show the HTTP request in the nicely formatted way (not sure what's it called) and then if you need curl or httpie or whatever invocation, that you would just copy-paste, so it wouldn't hurt to have that bit ugly ("unnecessary" escapes)...

datakurre commented 4 years ago

Option would be OK for me, but needs a little bit more work for testing etc.

I made a branch where that would be the default: https://sphinxcontrib-httpexample.readthedocs.io/en/more-double-quotes/usage.html#examples-with-inline-sources

tuukkamustonen commented 4 years ago

Looks good, though I guess it would be better to leave the \n linefeeds out?

datakurre commented 4 years ago

I guess, those linefeeds appeared only in httpie examples.

I need to find some time to implement the options, write tests for it and update documentation, but otherwise this looks fine. Meanwhile, you are free to use this branch.