add script to create kubernetes_asyncio python client (#60)

* feat: package named kubernetes_asyncio

* feat: fix rest client

* fix: patch rest client to support strategic-merge-patch+json

* fix: docs, move patch to ext. file
This commit is contained in:
Tomasz Prus
2018-06-15 01:40:54 +02:00
committed by Brendan Burns
parent 9a2dd07e0c
commit 55f9ae0366
3 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
21a22,23
> import asyncio
>
81a84,86
> def __del__(self):
> asyncio.ensure_future(self.pool_manager.close())
>
130a136,138
> if headers['Content-Type'] == 'application/json-patch+json':
> if not isinstance(body, list):
> headers['Content-Type'] = 'application/strategic-merge-patch+json'
164c172,174
< async with self.pool_manager.request(**args) as r:
---
> r = await self.pool_manager.request(**args)
> if _preload_content:
>
168,169c178,179
< # log response body
< logger.debug("response body: %s", r.data)
---
> # log response body
> logger.debug("response body: %s", r.data)
171,172c181,182
< if not 200 <= r.status <= 299:
< raise ApiException(http_resp=r)
---
> if not 200 <= r.status <= 299:
> raise ApiException(http_resp=r)

70
openapi/python-asyncio.sh Executable file
View File

@@ -0,0 +1,70 @@
#!/bin/bash
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
ARGC=$#
if [ $# -ne 2 ]; then
echo "Usage:"
echo " python-asyncio.sh OUTPUT_DIR SETTING_FILE_PATH"
echo " Setting file should define KUBERNETES_BRANCH, CLIENT_VERSION, and PACKAGE_NAME"
exit 1
fi
OUTPUT_DIR=$1
SETTING_FILE=$2
mkdir -p "${OUTPUT_DIR}"
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")
pushd "${SCRIPT_ROOT}" > /dev/null
SCRIPT_ROOT=`pwd`
popd > /dev/null
pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null
source "${SCRIPT_ROOT}/client-generator.sh"
source "${SETTING_FILE}"
SWAGGER_CODEGEN_COMMIT=f9b2839a3076f26db1b8fc61655a26662f2552ee; \
CLIENT_LANGUAGE=python-asyncio; \
CLEANUP_DIRS=(client/apis client/models docs test); \
kubeclient::generator::generate_client "${OUTPUT_DIR}"
echo "--- Patching generated code..."
find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} +
find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} +
find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/kubernetes_asyncio.client-python/client-python/g' {} +
# workaround https://github.com/swagger-api/swagger-codegen/pull/7905
find "${OUTPUT_DIR}/client" -type f -name \*.py ! -name '__init__.py' -exec sed -i '/^from .*models.*/d' {} \;
# workaround https://github.com/swagger-api/swagger-codegen/pull/8204
# + closing session
# + support application/strategic-merge-patch+json
patch "${OUTPUT_DIR}/client/rest.py" "${SCRIPT_ROOT}/python-asyncio-rest.py.patch"
# fix imports
find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/import client\./import kubernetes_asyncio.client./g' {} +
find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/from client/from kubernetes_asyncio.client/g' {} +
find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/getattr(client\.models/getattr(kubernetes_asyncio.client.models/g' {} +
echo "---Done."

View File

@@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.kubernetes</groupId>
<artifactId>client-python</artifactId>
<version>1.0-SNAPSHOT</version>
<name>client-python</name>
<url>http://kubernetes.io</url>
<build>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger-codegen-version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${generator.spec.path}</inputSpec>
<language>python</language>
<library>asyncio</library>
<gitUserId>kubernetes-client</gitUserId>
<gitRepoId>python</gitRepoId>
<configOptions>
<packageName>${generator.package.name}</packageName>
<packageVersion>${generator.client.version}</packageVersion>
<sortParamsByRequiredFlag>true</sortParamsByRequiredFlag>
</configOptions>
<output>${generator.output.path}</output>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- dependencies are needed for the client being generated -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>
</dependencies>
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<!-- Default values for the generator parameters. -->
<generator.output.path>.</generator.output.path>
<generator.spec.path>swagger.json</generator.spec.path>
<generator.package.name>swagger_client</generator.package.name>
<generator.client.version>unversioned</generator.client.version>
</properties>
</project>