首页 > 其他分享 >OpenAPI Swagger

OpenAPI Swagger

时间:2024-05-13 11:30:07浏览次数:19  
标签:null default client api OpenAPI openapi Swagger type

一. 首先你要启动你的swagger.json或者openapi.json

二. 然后你需要启动openapi生成代码的容器(如:python、javascript、php、golang...)我这里是python

docker run -d -p 8090:8080 --name openapi_generator openapitools/openapi-generator-online:latest-release

 

三. 调用容器生成代码

import logging
import os
import pathlib
import shutil
import tempfile
import zipfile

import requests

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)

L = logging.getLogger("generate_openapi_client")
L.setLevel(logging.DEBUG)


def generate_client(package_name: str, spec_url: str, install_dir: str, openapi_gen_url: str, project_name: str = None):
    if project_name is None:
        project_name = package_name

    L.debug(f"Requesting OpenAPI client generation for python {package_name=}")
    response = requests.post(
        f"{openapi_gen_url}/api/gen/clients/python",
        json={
            "openAPIUrl": spec_url,
            "options": {
                "packageName": package_name,
                "projectName": project_name,
                "generateSourceCodeOnly": "true",
            },
            "spec": {},
        },
    )
    response.raise_for_status()

    data = response.json()
    L.debug(data)
    download_link = data["link"]

    L.debug(f"Downloading generated client {download_link=}")
    response = requests.get(download_link)
    response.raise_for_status()

    with tempfile.TemporaryDirectory() as tmpdirname:
        L.debug(f"created temporary directory {tmpdirname=}")

        tmpdirname = pathlib.Path(tmpdirname)

        extract_file = tmpdirname / "client.zip"
        extract_dir = tmpdirname / "client"

        with open(extract_file, "wb") as f:
            f.write(response.content)

        with zipfile.ZipFile(extract_file, "r") as zip_ref:
            zip_ref.extractall(extract_dir)

        os.makedirs(install_dir, exist_ok=True)
        root_package_name = package_name.split(".")[0]
        source_dir = extract_dir / "python-client" / root_package_name
        # source_dir = extract_dir / 'python-client'

        L.debug(f"Moving source to target dir {source_dir=} {install_dir=}")
        shutil.copytree(source_dir, pathlib.Path(install_dir) / root_package_name, dirs_exist_ok=True)


if __name__ == "__main__":
    PACKAGE_PATH = os.environ.get("PACKAGE_PATH", ".")
    L.debug(f"{PACKAGE_PATH=}")

    generate_client(
        os.environ.get("PACKAGE_NAME", "openapi_client"),
        os.environ.get("OPENAPI_SPEC_URL", "http://host.docker.internal:8000/openapi.json"),
        openapi_gen_url=os.environ.get("OPENAPI_DOCKER_URL", "http://localhost:8090"),
        install_dir=PACKAGE_PATH,
    )

"""
{
  "packageName": {
    "opt": "packageName",
    "description": "python package name (convention: snake_case).",
    "type": "string",
    "optValue": null,
    "default": "openapi_client",
    "enum": null
  },
  "projectName": {
    "opt": "projectName",
    "description": "python project name in setup.py (e.g. petstore-api).",
    "type": "string",
    "optValue": null,
    "default": null,
    "enum": null
  },
  "packageVersion": {
    "opt": "packageVersion",
    "description": "python package version.",
    "type": "string",
    "optValue": null,
    "default": "1.0.0",
    "enum": null
  },
  "packageUrl": {
    "opt": "packageUrl",
    "description": "python package URL.",
    "type": "string",
    "optValue": null,
    "default": null,
    "enum": null
  },
  "hideGenerationTimestamp": {
    "opt": "hideGenerationTimestamp",
    "description": "Hides the generation timestamp when files are generated.",
    "type": "string",
    "optValue": null,
    "default": "true",
    "enum": null
  },
  "generateSourceCodeOnly": {
    "opt": "generateSourceCodeOnly",
    "description": "Specifies that only a library source code is to be generated.",
    "type": "string",
    "optValue": null,
    "default": "false",
    "enum": null
  },
  "recursionLimit": {
    "opt": "recursionLimit",
    "description": "Set the recursion limit. If not set, use the system default value.",
    "type": "string",
    "optValue": null,
    "default": null,
    "enum": null
  },
  "mapNumberTo": {
    "opt": "mapNumberTo",
    "description": "Map number to Union[StrictFloat, StrictInt], StrictStr or float.",
    "type": "string",
    "optValue": null,
    "default": "Union[StrictFloat, StrictInt]",
    "enum": null
  },
  "datetimeFormat": {
    "opt": "datetimeFormat",
    "description": "datetime format for query parameters",
    "type": "string",
    "optValue": null,
    "default": "%Y-%m-%dT%H:%M:%S%z",
    "enum": null
  },
  "dateFormat": {
    "opt": "dateFormat",
    "description": "date format for query parameters",
    "type": "string",
    "optValue": null,
    "default": "%Y-%m-%d",
    "enum": null
  },
  "useOneOfDiscriminatorLookup": {
    "opt": "useOneOfDiscriminatorLookup",
    "description": "Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.",
    "type": "string",
    "optValue": null,
    "default": "false",
    "enum": null
  },
  "library": {
    "opt": "library",
    "description": "library template (sub-template) to use: asyncio, tornado (deprecated), urllib3",
    "type": "string",
    "optValue": null,
    "default": "urllib3",
    "enum": null
  },
  "disallowAdditionalPropertiesIfNotPresent": {
    "opt": "disallowAdditionalPropertiesIfNotPresent",
    "description": "If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.",
    "type": "boolean",
    "optValue": null,
    "default": "true",
    "enum": {
      "false": "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.",
      "true": "Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default."
    }
  }
}

http://localhost:8090/api/gen/clients/python

{
  "openAPIUrl": "https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml",
  "options": {
    "packageName": "sf_pickup_client",
    "projectName": "sf_pickup_client",
    "generateSourceCodeOnly": "true",
  },
  "spec": {}
}


{
  "code": "58276864-f444-4a4a-a93f-ee251e517fb6",
  "link": "http://localhost:8090/api/gen/download/58276864-f444-4a4a-a93f-ee251e517fb6"
}



"""

四. 代码生成成功,会有一个DefaultAPI的class,你的所有api都在这里可以操作。

 

 1 from enum import Enum
 2 
 3 import pytest
 4 
 5 from openapi_client import configuration
 6 from openapi_client.api.default_api import DefaultApi
 7 from openapi_client.api_client import ApiClient
 8 from openapi_client.exceptions import ServiceException
 9 from openapi_client.models.operation import Operation
10 from openapi_client.models.operation_response import OperationResponse
11 from openapi_client.models.operation_type import OperationType
12 
13 
14 @pytest.fixture(scope="module")
15 def default_api():
16     config = configuration.Configuration(host="http://localhost:8000")
17     with ApiClient(config) as client:
18         yield DefaultApi(client)
19 
20 
21 def test_hello_world(default_api: DefaultApi):
22     assert default_api.root() == {"message": "Hello World"}
23 
24 
25 def test_hello_pytest(default_api: DefaultApi):
26     assert default_api.say_hello(name="pytest") == {"message": "Hello pytest"}
27 
28 
29 def test_operation_add(default_api: DefaultApi):
30     op = Operation(a=1, b=2, type=OperationType.ADD)
31     assert default_api.operation(op) == OperationResponse(result=3)
32 
33 
34 def test_operation_sub(default_api: DefaultApi):
35     op = Operation(a=1, b=2, type=OperationType.SUB)
36     assert default_api.operation(op) == OperationResponse(result=-1)
37 
38 
39 def test_operation_mul(default_api: DefaultApi):
40     op = Operation(a=1, b=2, type=OperationType.MUL)
41     assert default_api.operation(op) == OperationResponse(result=2)
42 
43 
44 def test_operation_div(default_api: DefaultApi):
45     op = Operation(a=1, b=2, type=OperationType.DIV)
46     assert default_api.operation(op) == OperationResponse(result=0.5)
47 
48 
49 def test_operation_div_by_zero(default_api: DefaultApi):
50     with pytest.raises(ServiceException):
51         op = Operation(a=1, b=0, type=OperationType.DIV)
52         default_api.operation(op)
53 
54 
55 def test_operation_type():
56     assert OperationType.ADD == "add"
57     assert OperationType.SUB == "sub"
58     assert OperationType.MUL == "mul"
59     assert OperationType.DIV == "div"
60     assert issubclass(OperationType, str)
61     assert issubclass(OperationType, Enum)

 

标签:null,default,client,api,OpenAPI,openapi,Swagger,type
From: https://www.cnblogs.com/watermeloncode/p/18188891

相关文章

  • springmvc+swagger2+struts2
    jar包<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.7</version> </dependency> <dependency> <groupId>com.fasterxml.ja......
  • Spring Boot2中Swagger3使用
    1.依赖引入<!--引入swagger--><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.7.0</version></dependency>2.常用注解介绍swagger2......
  • swagger简介
    Swagger是一个用于设计、构建和文档化WebAPI的开源工具。它可以帮助开发人员设计和描述API,并生成易于理解的文档。通过Swagger,开发人员可以更方便地了解API的功能、参数、请求和响应等信息,提高了API的可读性和易用性。Swagger提供了一套工具和规范,包括:1.SwaggerUI:一个用于展......
  • springboot3.2.3如何集成swagger
    在SpringBoot中集成Swagger可以方便地生成API文档并进行接口测试。要在SpringBoot3.2.3中集成Swagger,你可以按照以下步骤进行操作:1.添加Swagger依赖到pom.xml文件中:点击查看代码<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter<......
  • 报错“Please indicate a valid Swagger or OpenAPI version field”
    报错“PleaseindicateavalidSwaggerorOpenAPIversionfield”报错信息PleaseindicateavalidSwaggerorOpenAPIversionfield.Supportedversionfieldsareswagger:"2.0"andthosethatmatchopenapi:3.0.n(forexample,openapi:3.0.0). 原因分析根......
  • Swagger的配置与使用
    swagger前言学习1.swagger前期准备搜索springfox-swag引入两个jar包,springfox-swagger2和springfox-swagger-ui<!--https://mvnrepository.com/artifact/io.springfox/springfox-swagger2--><dependency><groupId>io.springfox</groupId><a......
  • springboot的日志swagger的配置
    我们导入swagger分为三步一.导入依赖首先我们需要在项目的pom里导入依赖<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0......
  • SpringBoot整合OpenAPI 3生成在线接口文档
    SpringBoot整合springdoc-openapi,自动生成在线接口文档。1、引入pom依赖<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.3.0</version></dependenc......
  • java Swagger 使用汇总
    Swagger1.Swagger简介最流行的api框架restfulapi在线自动生成工具:api文档与api定义同步更新直接运行,可以在线测试api接口支持多种语言:java,php2.官网https://swagger.io在项目中使用swagger需要springboxswagger2ui3.SpringBoot集成Swaggerhttps://mvnreposito......
  • 30.swagger springboot中简单整理
    类似postman接口在线测试API接口哈哈我也不太懂接口还没前后端整合过呢也就是注释你的各种接口吧可有可无先说依赖问题这个是swagger2的依赖当然出现问题了已经更新到了swagger3了参考:https://cloud.tencent.com/developer/article/1936862https://mvnrepos......