Package: apiclient-pydantic-generator
apiclient-pydantic-generatorThis code generator creates a ApiClient app from an openapi file.This project uses:datamodel-code-generatorto generate pydantic modelsapi-clientto create class ApiClientapi-client-pydanticextension for validate request data and converting json straight to pydantic class.This project highly inspiredfastapi-code-generatorInstallationTo installapiclient-pydantic-generator:$pipinstallapiclient-pydantic-generatorUsageTheapiclient-pydantic-generatorcommand:Usage: apiclient-codegen [OPTIONS]

Options:
  -i, --input FILENAME     [required]
  -o, --output PATH        [required]
  -t, --template-dir PATH
  -b, --base_url TEXT      [default: ]
  -p, --prefix TEXT        If "My" then will be MyApiClient  [default: My]
  -a, --base_api_cls TEXT  Base class for client class  [default:
                           apiclient.APIClient]

  --install-completion     Install completion for the current shell.
  --show-completion        Show completion for the current shell, to copy it
                           or customize the installation.

  --help                   Show this message and exit.ExampleOpenAPIpetstore.yamlopenapi: '3.0.0'
      info:
        version: 1.0.0
        title: Swagger Petstore
        license:
          name: MIT
      servers:
        - url: http://petstore.swagger.io/v1
      paths:
        /pets:
          get:
            summary: List all pets
            operationId: listPets
            tags:
              - pets
            parameters:
              - name: limit
                in: query
                description: How many items to return at one time (max 100)
                required: false
                schema:
                  type: integer
                  format: int32
            responses:
              '200':
                description: A paged array of pets
                headers:
                  x-next:
                    description: A link to the next page of responses
                    schema:
                      type: string
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Pets'
              default:
                description: unexpected error
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Error'
                      x-amazon-apigateway-integration:
                        uri:
                          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
                        passthroughBehavior: when_no_templates
                        httpMethod: POST
                        type: aws_proxy
          post:
            summary: Create a pet
            operationId: createPets
            tags:
              - pets
            responses:
              '201':
                description: Null response
              default:
                description: unexpected error
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Error'
                      x-amazon-apigateway-integration:
                        uri:
                          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
                        passthroughBehavior: when_no_templates
                        httpMethod: POST
                        type: aws_proxy
        /pets/{petId}:
          get:
            summary: Info for a specific pet
            operationId: showPetById
            tags:
              - pets
            parameters:
              - name: petId
                in: path
                required: true
                description: The id of the pet to retrieve
                schema:
                  type: string
            responses:
              '200':
                description: Expected response to a valid request
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Pets'
              default:
                description: unexpected error
                content:
                  application/json:
                    schema:
                      $ref: '#/components/schemas/Error'
          x-amazon-apigateway-integration:
            uri:
              Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
            passthroughBehavior: when_no_templates
            httpMethod: POST
            type: aws_proxy
      components:
        schemas:
          Pet:
            required:
              - id
              - name
            properties:
              id:
                type: integer
                format: int64
              name:
                type: string
              tag:
                type: string
          Pets:
            type: array
            description: list of pet
            items:
              $ref: '#/components/schemas/Pet'
          Error:
            required:
              - code
              - message
            properties:
              code:
                type: integer
                format: int32
              message:
                type: string$apiclient-codegen--inputpetstore.yaml--outputapp_petstore--prefixPetStoreapp_petstore/__init__.py:from.clientimportPetStoreAPIClient__all__=('PetStoreAPIClient',)app_petstore/client.py:from__future__importannotationsfromapiclientimportAPIClientfromapiclient_pydanticimportserialize_all_methodsfrom.endpointsimportEndpointsfrom.modelsimportLimitQueryParams,PetIdPathParams,Pets@serialize_all_methods()classPetStoreAPIClient(APIClient):deflist_pets(self,query_params:LimitQueryParams)->Pets:"""List all pets"""returnself.get(Endpoints.list_pets,query_params)defcreate_pets(self)->None:"""Create a pet"""self.post(Endpoints.create_pets)defshow_pet_by_id(self,path_params:PetIdPathParams)->Pets:"""Info for a specific pet"""returnself.get(Endpoints.show_pet_by_id.format(**path_params))app_petstore/endpoints.py:fromapiclientimportendpoint@endpoint(base_url='http://petstore.swagger.io/v1')classEndpoints:list_pets:str='/pets'create_pets:str='/pets'show_pet_by_id:str='/pets/{pet_id}'app_petstore/models.py:from__future__importannotationsfromtypingimportList,OptionalfrompydanticimportBaseModel,FieldclassPet(BaseModel):id:intname:strtag:Optional[str]=NoneclassPets(BaseModel):__root__:List[Pet]=Field(...,description='list of pet')classError(BaseModel):code:intmessage:strclassLimitQueryParams(BaseModel):limit:Optional[int]=NoneclassPetIdPathParams(BaseModel):petId:strUsing a class:fromapiclient.request_formattersimportJsonRequestFormatterfromapiclient.response_handlersimportJsonResponseHandlerfromapp_petstoreimportPetStoreAPIClientpet_client=PetStoreAPIClient(response_handler=JsonResponseHandler,request_formatter=JsonRequestFormatter)pets=pet_client.list_pets()forpetinpets:print(pet.name)Licenseapiclient-pydantic-generator is released under the MIT License.http://www.opensource.org/licenses/mit-license