swagger-api / swagger-api/swagger-codegen

[Python] allow caller to determine "Accept" content-type

Open
#11,078 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The Python client requests the Accept header from a list, calling method select_header_accept. This does not work well when a single API call needs to be done with "application/xml" rather than "application/json". Work-arounds I have found do not work well with asynchronous models.

Swagger-codegen version

3.0.24 and 3.0.25

Swagger declaration file content or url

The operationId "get/almaws/v1/bibs/{mms_id}" will return different information if "application/json" is selected as opposed to "application/xml". The Machine Readable Catalog (MARC) specification supports an XML serialization, but there is no JSON serialization.

https://developers.exlibrisgroup.com/wp-content/uploads/alma/openapi/bibs.yaml

Command line used for generation

My process is pretty complicated - see https://github.com/NLM-OCCS/python-alma-sdk for a public version that does not disclose additional magic for handling API keys. As a summary, here is the command-line used:

java -jar lib/swagger-codegen-cli.jar generate -l python -c api-config.json -i https://developers.exlibrisgroup.com/wp-content/uploads/alma/openapi/bibs.yaml --resolve-fully -o target

Some notes on the arguments:

  • api-config.json is below:
{
  "packageName": "alma_api",
  "modelPackage": "models",
  "apiPackage": "apis"
}
Steps to reproduce
  1. Install Apache Ant and place in path
  2. Install CPython 3.6+
  3. Clone the repository: git clone git@github.com:NLM-OCCS/python-alma-sdk.git
  4. Run ant bootstrap to install apache ivy: ant bootstrap
  5. Run ant resolve to download swagger-codegen-cli: ant resolve
  6. Generate the API as described above - java -jar lib/swagger-codegen-cli.jar generate -l python -c api-config -i https://developers.exlibrisgroup.com/wp-content/uploads/alma/openapi/bibs.yaml --resolve-fully -o target
  7. Change into the target directory.
  8. Create a virtualenv, activate it, and install the requirements and the ipython (interactive python) environment.
  9. Run the following in an ipython shell
from alma_api import Configuration, ApiClient, CatalogApi

config = Configuration()
config.api_key['apikey'] = 'an appropriate key'
client = ApiClient(config)
catapi = CatalogApi(client)

r = catapi.getalmawsv1bibsmms_id('991572500000541', _preload_content=False)
Suggest a fix/enhancement

Change modules/swagger-codegen/src/main/resources/python/api.mustache to add an additional _accept_header s argument to each generated {operation_id}_with_http_info(), such as the generated code:

        all_params.append('_accept_header')

Change the code to determine the accept header as follows:

        # HTTP header `Accept`
        accept_header = all_params.pop('_accept_header', None)
        header_params['Accept'] = accept_header if accept_header else self.api_client.select_header_accept(
            ['application/json', 'application/xml'])  # noqa: E501

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/python/api.mustache and reproduce generation using the linked Alma OpenAPI document and command. Check the generated getalmawsv1bibsmms_id_with_http_info() path and verify that callers can select application/xml for one operation while the existing default Accept selection still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.