collective / sphinxcontrib-httpexample

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

Enhancement: Add 'JavaScript' Tab to Display JavaScript Client Method in sphinxcontrib-httpexample #90

Open Hrittik20 opened 5 months ago

Hrittik20 commented 5 months ago

We propose adding a custom "JavaScript" tab to sphinxcontrib-httpexample. The purpose of this tab is to display the JavaScript client method, specifically createAliasesMutation, which developers can use to call the Plone REST API endpoint for adding URL aliases in bulk.

Refer to the discussion in this pull request for additional details and context.

datakurre commented 5 months ago

Should we call this tab simply “Plone”, because it would be very Plone specific?

Could you provide some code examples for these calls (what kind of code should be displayed on the tab)?

Hrittik @.***> kirjoitti 3.2.2024 kello 19.04:

 We propose adding a custom "JavaScript" tab to sphinxcontrib-httpexample. The purpose of this tab is to display the JavaScript client method, specifically createAliasesMutation, which developers can use to call the Plone REST API endpoint for adding URL aliases in bulk.

Refer to the discussion in this pull request for additional details and context.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

Hrittik20 commented 5 months ago

We want the tab title to be "JavaScript". Perhaps we can explore creating a custom tab that allows us to set the title according to our preferences. We plan to display simple code snippets corresponding to Plone REST API calls. Specifically, we aim to output the JavaScript client method createAliasesMutation.

datakurre commented 5 months ago

Ok. In configuration it could be plone_javascript (or something other unique), but the display value could be JavaScript. That’s way easier than introducing custom labels until there are enough similar use cases to make it worth of the effort.

For further design, I’m really really waiting to see more example results before thinking this more out of thin air :)

Hrittik @.***> kirjoitti 4.2.2024 kello 14.31:

 We want the tab title to be "JavaScript". Perhaps we can explore creating a custom tab that allows us to set the title according to our preferences. We plan to display simple code snippets corresponding to Plone REST API calls. Specifically, we aim to output the JavaScript client method createAliasesMutation.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

Hrittik20 commented 5 months ago

@datakurre We are trying to achieve this structure. However, I can't seem to figure out how to format the request body like http or httpie. Do you know how to do it?

Capture

datakurre commented 5 months ago

I don't think that there is any magic in it. The builder returns formatted string, and the directive places it within Sphinx CodeBlock. (httpie uses shlex-library to format the piped data; http is displayed as such as it is the source for the example directive).

Python builtin json-library has pretty print support, which might help.

>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
    "4": 5,
    "6": 7
}
Hrittik20 commented 5 months ago

This is my builder function for Plone JavaScript.

def build_plone_javascript_command(request):
    javascript_code = 'createAliasesMutation'
    redir_input2 = ''

    # Request body
    data = maybe_str(request.data())
    if data:
        if is_json(request.headers.get('Content-Type', '')):
            redir_input2 = json.dumps(data, indent=2, sort_keys=True,
                           separators=(',', ': '))
        else:
            redir_input2 = data

    # Output string
    output_string = f"{javascript_code}\n|\nconst aliasesData = {redir_input2};"

    return output_string

Perhaps I am overlooking something, which could be causing the format to appear incorrect.

Edit: It was a CSS issue.