swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Combining all API tags into one API name

Open Collinux opened this issue 9 years ago • 3 comments

I have a swagger yaml file with several different tags which tells the Swagger UI to create different sections. It makes the UI have a nice flow to it, however this presents a problem for swagger-codegen because I want all the REST calls to be accessible under one single API. These separate tags are interpreted as different APIs so the generated library is fragmented.

As a simple example, let "Pets" and "Accessories" be two different tags inside a yaml file. "Pets" has the REST functions "getPets", "postPets" whereas "Accessories" has "getAccessory" and "postAccessory". Swagger-codegen will make a separate Pets API which you can call with "my_module.PetsApi().getPets()" and a separate Accessories API which you can call with "my_module.AccessoriesApi().getAccessories()". I would like to be able to call "my_module.MyApi().getPets()" or "my_module.MyApi().getAccessories()" -- where MyApi is like having an upper level tag that grants direct access to any of my module's various API's functions.

I find that this would simplify the syntax for my use case. This question is not specific to any language, I only used python syntax above to illustrate my point. Are there any other command options in swagger-codegen that I can use to combine all the APIs? Or perhaps a different way I can split up the REST functions on the swagger UI that won't mess with my swagger-codegen process?

(swagger-codegen version 2.2.3)

Collinux avatar Oct 19 '17 20:10 Collinux

@Collinux Since you referenced Python... as far as the python client goes, you can totally do this with some extra manual steps. It's what I've done in my client(s). It also helps to keep the underlying configs consistent when you don't wanna roll with the defaults.

Each individual client takes an instance of ApiClient anyway, so you can basically just modify ApiClient to instantiate each client as its own attribute, passing self, so you can do like api_client.pet_api.find_pets_by_status()

Here's a quick example sort of thing I gave in another issue:

Sidenote/tangent: If that's annoying to do every time, and you don't mind editing the generated swagger_client library directly, you can sorta make this the default behavior by having ApiClient instantiate the clients on its own, passing self when it does. Quick dirty example, in ApiClient.__init__, you might do:

from .apis import PetApi
self.pet_api = PetApi(api_client=self)

That way you'd only need to create ApiClient and update the pool manager settings once, and could access the other clients through it like api_client.pet_api.find_pets_by_status('foo') and any changes you make via ApiClient later would be reflected in all the clients assigned to it. If you're creating multiple clients it also avoids the need to explicitly set the clients all over again for each one

arcward avatar Oct 24 '17 21:10 arcward

Before I run the code-gen I run a regex on the swagger file to replace all the tags. Not the best solution, but it works:

perl -i -0pe "s|(tags:\n\s+-)(.*)|\1 default|g" swagger.yaml

nfortenberry avatar Nov 29 '17 23:11 nfortenberry

I do this as a post-process. I generated Python client into one package alma_api, with alma_api/api and alma_api/models. Then, I implement my own add-on such as nlm_alma/apis.py:

from alma_api.apis.reports_api import ReportsApi
from alma_api.aps.paths_api import PathsApi

class AnalyticsApi(ReportsApi, PathsApi):
    pass

This can also be done by pre-processing the spec, and that's probably the better way.

danizen avatar Jun 28 '21 17:06 danizen