From 03668ad372c1f444338b8e26d791222e4e41cb5d Mon Sep 17 00:00:00 2001 From: danfengliu Date: Fri, 14 Feb 2020 16:11:38 +0800 Subject: [PATCH] Build python swagger client for V2.0 Add v2 swagger.yaml python library. Signed-off-by: danfengliu --- Makefile | 9 ++++++--- tests/apitests/python/library/artifact.py | 9 +++++++++ tests/apitests/python/library/base.py | 16 +++++++++++----- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 tests/apitests/python/library/artifact.py diff --git a/Makefile b/Makefile index e8aa5d41f..566578f77 100644 --- a/Makefile +++ b/Makefile @@ -496,9 +496,12 @@ swagger_client: @echo "Generate swagger client" wget -q https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar -O swagger-codegen-cli.jar rm -rf harborclient - mkdir harborclient - java -jar swagger-codegen-cli.jar generate -i api/v2.0/legacy_swagger.yaml -l python -o harborclient - cd harborclient; python ./setup.py install + mkdir -p harborclient/harbor_swagger_client + mkdir -p harborclient/harbor_v2_swagger_client + java -jar swagger-codegen-cli.jar generate -i api/v2.0/legacy_swagger.yaml -l python -o harborclient/harbor_swagger_client -DpackageName=swagger_client + java -jar swagger-codegen-cli.jar generate -i api/v2.0/swagger.yaml -l python -o harborclient/harbor_v2_swagger_client -DpackageName=v2_swagger_client + cd harborclient/harbor_swagger_client; python ./setup.py install + cd harborclient/harbor_v2_swagger_client; python ./setup.py install pip install docker -q pip freeze diff --git a/tests/apitests/python/library/artifact.py b/tests/apitests/python/library/artifact.py new file mode 100644 index 000000000..4dca57942 --- /dev/null +++ b/tests/apitests/python/library/artifact.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +import base +import v2_swagger_client +from v2_swagger_client.rest import ApiException + +class Artifact(base.Base): + def create_artifact(self, expect_status_code = 201, expect_response_body = None, **kwargs): + return None diff --git a/tests/apitests/python/library/base.py b/tests/apitests/python/library/base.py index b1fec5b32..9b5e7ee9c 100644 --- a/tests/apitests/python/library/base.py +++ b/tests/apitests/python/library/base.py @@ -3,6 +3,7 @@ import sys import time import swagger_client +import v2_swagger_client try: from urllib import getproxies except ImportError: @@ -19,7 +20,7 @@ class Credential: self.username = username self.password = password -def _create_client(server, credential, debug): +def _create_client(server, credential, debug, api_type="products"): cfg = swagger_client.Configuration() cfg.host = server.endpoint cfg.verify_ssl = server.verify_ssl @@ -32,8 +33,10 @@ def _create_client(server, credential, debug): proxy = proxies.get('http', proxies.get('all', None)) if proxy: cfg.proxy = proxy - - return swagger_client.ProductsApi(swagger_client.ApiClient(cfg)) + return { + "products": swagger_client.ProductsApi(swagger_client.ApiClient(cfg)), + "artifact": v2_swagger_client.ArtifactApi(v2_swagger_client.ApiClient(cfg)), + }.get(api_type,'Error: Wrong API type') def _assert_status_code(expect_code, return_code): if str(return_code) != str(expect_code): @@ -64,18 +67,21 @@ class Base: def __init__(self, server = Server(endpoint="http://localhost:8080/api", verify_ssl=False), credential = Credential(type="basic_auth", username="admin", password="Harbor12345"), - debug = True): + debug = True, api_type = "products"): if not isinstance(server.verify_ssl, bool): server.verify_ssl = server.verify_ssl == "True" self.server = server self.credential = credential self.debug = debug - self.client = _create_client(server, credential, debug) + self.api_type = api_type + self.client = _create_client(server, credential, debug, api_type=api_type) def _get_client(self, **kwargs): if len(kwargs) == 0: return self.client server = self.server + if "api_type" in kwargs: + server.api_type = kwargs.get("api_type") if "endpoint" in kwargs: server.endpoint = kwargs.get("endpoint") if "verify_ssl" in kwargs: