diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18270ce..0c9cc2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,3 @@ -# WARNING: DO NOT EDIT! -# -# This file was generated by plugin_template, and is managed by it. Please use -# './plugin-template --github pulp_file' to update this file. -# -# For more info visit https://github.com/pulp/plugin_template --- name: pulp-openapi-generator PR CI on: pull_request @@ -66,7 +60,7 @@ jobs: run: | echo ::group::HTTPIE sudo apt-get update -yq - sudo -E apt-get -yq --no-install-suggests --no-install-recommends install httpie + sudo -E apt-get -yq --no-install-suggests --no-install-recommends install httpie jq echo ::endgroup:: echo "HTTPIE_CONFIG_DIR=$GITHUB_WORKSPACE/.ci/assets/httpie/" >> $GITHUB_ENV diff --git a/.gitignore b/.gitignore index 5be0c53..2ad937d 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,8 @@ target/ #Ipython Notebook .ipynb_checkpoints -# written in generate.sh -.openapi-generator-ignore - -# generated client packages +# generated stuff +/api.json +/patched-api.json /*-client/ +.openapi-generator-ignore diff --git a/gen-client.sh b/gen-client.sh index 6e4cd72..0d4315a 100755 --- a/gen-client.sh +++ b/gen-client.sh @@ -20,7 +20,7 @@ echo "Unnormalized Version: ${VERSION}" VERSION="$(python3 -c "from packaging.version import Version; print(Version('${VERSION}'))")" echo "Version: ${VERSION}" -OPENAPI_PYTHON_IMAGE="${OPENAPI_PYTHON_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}" +OPENAPI_PYTHON_IMAGE="${OPENAPI_PYTHON_IMAGE:-docker.io/openapitools/openapi-generator-cli:v7.6.0}" OPENAPI_RUBY_IMAGE="${OPENAPI_RUBY_IMAGE:-docker.io/openapitools/openapi-generator-cli:v4.3.1}" OPENAPI_TYPESCRIPT_IMAGE="${OPENAPI_TYPESCRIPT_IMAGE:-docker.io/openapitools/openapi-generator-cli:v5.2.1}" @@ -61,15 +61,23 @@ else VOLUME_DIR="${PWD}" fi +REMOVE_COOKIE_AUTH_FILTER='del(.paths[][].security|select(.)[]|select(.cookieAuth))|del(.components.securitySchemes.cookieAuth)' + +# These two may be needed when upgrading the generator image +FIX_TASK_CREATED_RESOURCES_FILTER='(.components.schemas.TaskResponse|select(.)|.properties.created_resources.items) |= {"$oneOf":[{type:"null"},.]}' +FIX_TASK_ERROR_FILTER='(.components.schemas.TaskResponse|select(.)|.properties.error) |= (del(.readOnly) | .additionalProperties.type = "string")' + if [ "$LANGUAGE" = "python" ] then + cat "${API_SPEC}" | jq "${FIX_TASK_CREATED_RESOURCES_FILTER}|${FIX_TASK_ERROR_FILTER}" > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_PYTHON_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g python \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ "--additional-properties=packageName=pulpcore.client.${PACKAGE},projectName=${PACKAGE}-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED}" \ @@ -86,14 +94,15 @@ then mkdir -p "${PACKAGE}-client" echo git_push.sh > "${PACKAGE}-client/.openapi-generator-ignore" - python3 remove-cookie-auth.py + cat "${API_SPEC}" | jq "${REMOVE_COOKIE_AUTH_FILTER}" > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_RUBY_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g ruby \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ "--additional-properties=gemName=${PACKAGE}_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/${PACKAGE}" \ @@ -105,13 +114,15 @@ fi if [ "$LANGUAGE" = "typescript" ] then + cat "${API_SPEC}" | jq "." > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_TYPESCRIPT_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g typescript-axios \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ -t "${VOLUME_DIR}/templates/typescript-axios" \ diff --git a/remove-cookie-auth.py b/remove-cookie-auth.py deleted file mode 100644 index a6dd112..0000000 --- a/remove-cookie-auth.py +++ /dev/null @@ -1,58 +0,0 @@ -import json - - -def recursive_remove(json_dict): - """ - Recursively search through all of the dictionary and removes any key that is 'cookieAuth' - """ - for key in list(json_dict): - if key == 'cookieAuth': - json_dict.pop(key) - else: - value = json_dict[key] - if isinstance(value, dict): - # Search this dictionary as well - recursive_remove(value) - elif isinstance(value, list): - recursive_list_search(value) - - -def recursive_list_search(json_list): - """ - Recursively search through a passed in list till you hit a dictionary to pass into recursive_remove - Any other objects are don't cares - """ - for i in reversed(range(len(json_list))): - value = json_list[i] - if isinstance(value, dict): - recursive_remove(value) - # Delete the dictionary if it is empty - if not value: - del json_list[i] - elif isinstance(value, list): - recursive_list_search(value) - - -# Open the JSON file -api_file = open('api.json', 'r') - -# Load the api JSON object into dictionary -api_json = json.load(api_file) - -# Close the file -api_file.close() - -# Remove 'cookieAuth' from the schema -recursive_remove(api_json) - -# Serialize the json -new_api_json_object = json.dumps(api_json) - -# Reopen the file in write mode -api_file = open('api.json', 'w') - -# Re-write the api.json file -api_file.write(new_api_json_object) - -# Close the file -api_file.close() diff --git a/templates/python/api.mustache b/templates/python/api.mustache deleted file mode 100644 index 8c5f669..0000000 --- a/templates/python/api.mustache +++ /dev/null @@ -1,260 +0,0 @@ -# coding: utf-8 - -{{>partial_header}} - -from __future__ import absolute_import - -import re # noqa: F401 - -# python 2 and python 3 compatibility library -import six - -from {{packageName}}.api_client import ApiClient -from {{packageName}}.exceptions import ( # noqa: F401 - ApiTypeError, - ApiValueError -) - - -{{#operations}} -class {{classname}}(object): - """NOTE: This class is auto generated by OpenAPI Generator - Ref: https://openapi-generator.tech - - Do not edit the class manually. - """ - - def __init__(self, api_client=None): - if api_client is None: - api_client = ApiClient() - self.api_client = api_client -{{#operation}} - - def {{operationId}}(self, {{#requiredParams}}{{^vendorExtensions.x-isDomain}}{{paramName}}, {{/vendorExtensions.x-isDomain}}{{/requiredParams}}{{#requiredParams}}{{#vendorExtensions.x-isDomain}}{{paramName}}="default",{{/vendorExtensions.x-isDomain}}{{/requiredParams}} **kwargs): # noqa: E501 - """{{#summary}}{{{.}}}{{/summary}}{{^summary}}{{operationId}}{{/summary}} # noqa: E501 - -{{#notes}} - {{{notes}}} # noqa: E501 -{{/notes}} - This method makes a synchronous HTTP request by default. To make an - asynchronous HTTP request, please pass async_req=True -{{#sortParamsByRequiredFlag}} - >>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}async_req=True) -{{/sortParamsByRequiredFlag}} -{{^sortParamsByRequiredFlag}} - >>> thread = api.{{operationId}}({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}async_req=True) -{{/sortParamsByRequiredFlag}} - >>> result = thread.get() - - :param async_req bool: execute request asynchronously -{{#allParams}} - :param {{dataType}} {{paramName}}:{{#description}} {{{description}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional){{/optional}} -{{/allParams}} - :param _preload_content: if False, the urllib3.HTTPResponse object will - be returned without reading/decoding response - data. Default is True. - :param _request_timeout: timeout setting for this request. If one - number provided, it will be total request - timeout. It can also be a pair (tuple) of - (connection, read) timeouts. - :return: {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}None{{/returnType}} - If the method is called asynchronously, - returns the request thread. - """ - kwargs['_return_http_data_only'] = True - return self.{{operationId}}_with_http_info({{#requiredParams}}{{^vendorExtensions.x-isDomain}}{{paramName}}, {{/vendorExtensions.x-isDomain}}{{/requiredParams}}{{#requiredParams}}{{#vendorExtensions.x-isDomain}}{{paramName}}={{paramName}},{{/vendorExtensions.x-isDomain}}{{/requiredParams}} **kwargs) # noqa: E501 - - def {{operationId}}_with_http_info(self, {{#requiredParams}}{{^vendorExtensions.x-isDomain}}{{paramName}}, {{/vendorExtensions.x-isDomain}}{{/requiredParams}}{{#requiredParams}}{{#vendorExtensions.x-isDomain}}{{paramName}}="default",{{/vendorExtensions.x-isDomain}}{{/requiredParams}} **kwargs): # noqa: E501 - """{{#summary}}{{{.}}}{{/summary}}{{^summary}}{{operationId}}{{/summary}} # noqa: E501 - -{{#notes}} - {{{notes}}} # noqa: E501 -{{/notes}} - This method makes a synchronous HTTP request by default. To make an - asynchronous HTTP request, please pass async_req=True -{{#sortParamsByRequiredFlag}} - >>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}async_req=True) -{{/sortParamsByRequiredFlag}} -{{^sortParamsByRequiredFlag}} - >>> thread = api.{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}={{paramName}}_value, {{/required}}{{/allParams}}async_req=True) -{{/sortParamsByRequiredFlag}} - >>> result = thread.get() - - :param async_req bool: execute request asynchronously -{{#allParams}} - :param {{dataType}} {{paramName}}:{{#description}} {{{description}}}{{/description}}{{#required}} (required){{/required}}{{#optional}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/optional}} -{{/allParams}} - :param _return_http_data_only: response data without head status code - and headers - :param _preload_content: if False, the urllib3.HTTPResponse object will - be returned without reading/decoding response - data. Default is True. - :param _request_timeout: timeout setting for this request. If one - number provided, it will be total request - timeout. It can also be a pair (tuple) of - (connection, read) timeouts. - :return: {{#returnType}}tuple({{returnType}}, status_code(int), headers(HTTPHeaderDict)){{/returnType}}{{^returnType}}None{{/returnType}} - If the method is called asynchronously, - returns the request thread. - """ - - {{#servers.0}} - local_var_hosts = [ -{{#servers}} - '{{{url}}}'{{^-last}},{{/-last}} -{{/servers}} - ] - local_var_host = local_var_hosts[0] - if kwargs.get('_host_index'): - _host_index = int(kwargs.get('_host_index')) - if _host_index < 0 or _host_index >= len(local_var_hosts): - raise ApiValueError( - "Invalid host index. Must be 0 <= index < %s" - % len(local_var_host) - ) - local_var_host = local_var_hosts[_host_index] - {{/servers.0}} - local_var_params = locals() - - all_params = [ -{{#allParams}} - '{{paramName}}'{{#hasMore}},{{/hasMore}} -{{/allParams}} - ] - all_params.extend( - [ - 'async_req', - '_return_http_data_only', - '_preload_content', - '_request_timeout' - ] - ) - - for key, val in six.iteritems(local_var_params['kwargs']): - if key not in all_params{{#servers.0}} and key != "_host_index"{{/servers.0}}: - raise ApiTypeError( - "Got an unexpected keyword argument '%s'" - " to method {{operationId}}" % key - ) - local_var_params[key] = val - del local_var_params['kwargs'] -{{#allParams}} -{{^isNullable}} -{{#required}} - # verify the required parameter '{{paramName}}' is set - if self.api_client.client_side_validation and ('{{paramName}}' not in local_var_params or # noqa: E501 - local_var_params['{{paramName}}'] is None): # noqa: E501 - raise ApiValueError("Missing the required parameter `{{paramName}}` when calling `{{operationId}}`") # noqa: E501 -{{/required}} -{{/isNullable}} -{{/allParams}} - -{{#allParams}} -{{#hasValidation}} - {{#maxLength}} - if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and # noqa: E501 - len(local_var_params['{{paramName}}']) > {{maxLength}}): # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be less than or equal to `{{maxLength}}`") # noqa: E501 - {{/maxLength}} - {{#minLength}} - if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and # noqa: E501 - len(local_var_params['{{paramName}}']) < {{minLength}}): # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, length must be greater than or equal to `{{minLength}}`") # noqa: E501 - {{/minLength}} - {{#maximum}} - if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}: # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value less than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}`{{maximum}}`") # noqa: E501 - {{/maximum}} - {{#minimum}} - if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}: # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must be a value greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}`{{minimum}}`") # noqa: E501 - {{/minimum}} - {{#pattern}} - if self.api_client.client_side_validation and '{{paramName}}' in local_var_params and not re.search(r'{{{vendorExtensions.x-regex}}}', local_var_params['{{paramName}}']{{#vendorExtensions.x-modifiers}}{{#-first}}, flags={{/-first}}re.{{.}}{{^-last}} | {{/-last}}{{/vendorExtensions.x-modifiers}}): # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, must conform to the pattern `{{{pattern}}}`") # noqa: E501 - {{/pattern}} - {{#maxItems}} - if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and # noqa: E501 - len(local_var_params['{{paramName}}']) > {{maxItems}}): # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be less than or equal to `{{maxItems}}`") # noqa: E501 - {{/maxItems}} - {{#minItems}} - if self.api_client.client_side_validation and ('{{paramName}}' in local_var_params and # noqa: E501 - len(local_var_params['{{paramName}}']) < {{minItems}}): # noqa: E501 - raise ApiValueError("Invalid value for parameter `{{paramName}}` when calling `{{operationId}}`, number of items must be greater than or equal to `{{minItems}}`") # noqa: E501 - {{/minItems}} -{{/hasValidation}} -{{#-last}} -{{/-last}} -{{/allParams}} - collection_formats = {} - - path_params = {} -{{#pathParams}} - if '{{paramName}}' in local_var_params: - path_params['{{baseName}}'] = local_var_params['{{paramName}}']{{#isListContainer}} # noqa: E501 - collection_formats['{{baseName}}'] = '{{collectionFormat}}'{{/isListContainer}} # noqa: E501 -{{/pathParams}} - - query_params = [] -{{#queryParams}} - if '{{paramName}}' in local_var_params and local_var_params['{{paramName}}'] is not None: # noqa: E501 - query_params.append(('{{baseName}}', local_var_params['{{paramName}}'])){{#isListContainer}} # noqa: E501 - collection_formats['{{baseName}}'] = '{{collectionFormat}}'{{/isListContainer}} # noqa: E501 -{{/queryParams}} - - header_params = {} -{{#headerParams}} - if '{{paramName}}' in local_var_params: - header_params['{{baseName}}'] = local_var_params['{{paramName}}']{{#isListContainer}} # noqa: E501 - collection_formats['{{baseName}}'] = '{{collectionFormat}}'{{/isListContainer}} # noqa: E501 -{{/headerParams}} - - form_params = [] - local_var_files = {} -{{#formParams}} - if '{{paramName}}' in local_var_params: - {{^isFile}}form_params.append(('{{baseName}}', local_var_params['{{paramName}}'])){{/isFile}}{{#isFile}}local_var_files['{{baseName}}'] = local_var_params['{{paramName}}']{{/isFile}}{{#isListContainer}} # noqa: E501 - collection_formats['{{baseName}}'] = '{{collectionFormat}}'{{/isListContainer}} # noqa: E501 -{{/formParams}} - - body_params = None -{{#bodyParam}} - if '{{paramName}}' in local_var_params: - body_params = local_var_params['{{paramName}}'] -{{/bodyParam}} - {{#hasProduces}} - # HTTP header `Accept` - header_params['Accept'] = self.api_client.select_header_accept( - [{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]) # noqa: E501 - - {{/hasProduces}} - {{#hasConsumes}} - # HTTP header `Content-Type` - header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501 - [{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]) # noqa: E501 - - {{/hasConsumes}} - # Authentication setting - auth_settings = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}] # noqa: E501 - - return self.api_client.call_api( - '{{{path}}}', '{{httpMethod}}', - path_params, - query_params, - header_params, - body=body_params, - post_params=form_params, - files=local_var_files, - response_type={{#returnType}}'{{returnType}}'{{/returnType}}{{^returnType}}None{{/returnType}}, # noqa: E501 - auth_settings=auth_settings, - async_req=local_var_params.get('async_req'), - _return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501 - _preload_content=local_var_params.get('_preload_content', True), - _request_timeout=local_var_params.get('_request_timeout'), - {{#servers.0}} - _host=local_var_host, - {{/servers.0}} - collection_formats=collection_formats) -{{/operation}} -{{/operations}} diff --git a/templates/python/configuration.mustache b/templates/python/configuration.mustache index 0ae7dba..dd22741 100644 --- a/templates/python/configuration.mustache +++ b/templates/python/configuration.mustache @@ -2,51 +2,54 @@ {{>partial_header}} -from __future__ import absolute_import - import copy import logging +from logging import FileHandler {{^asyncio}} import multiprocessing {{/asyncio}} import sys +from typing import Optional import urllib3 -import six -from six.moves import http_client as httplib - +import http.client as httplib -class Configuration(object): - """NOTE: This class is auto generated by OpenAPI Generator +JSON_SCHEMA_VALIDATION_KEYWORDS = { + 'multipleOf', 'maximum', 'exclusiveMaximum', + 'minimum', 'exclusiveMinimum', 'maxLength', + 'minLength', 'pattern', 'maxItems', 'minItems' +} - Ref: https://openapi-generator.tech - Do not edit the class manually. +class Configuration: + """This class contains various settings of the API client. - :param host: Base url + :param host: Base url. :param api_key: Dict to store API key(s). Each entry in the dict specifies an API key. The dict key is the name of the security scheme in the OAS specification. The dict value is the API key secret. - :param api_key_prefix: Dict to store API prefix (e.g. Bearer) + :param api_key_prefix: Dict to store API prefix (e.g. Bearer). The dict key is the name of the security scheme in the OAS specification. The dict value is an API key prefix when generating the auth data. - :param username: Username for HTTP basic authentication - :param password: Password for HTTP basic authentication - :param discard_unknown_keys: Boolean value indicating whether to discard - unknown properties. A server may send a response that includes additional - properties that are not known by the client in the following scenarios: - 1. The OpenAPI document is incomplete, i.e. it does not match the server - implementation. - 2. The client was generated using an older version of the OpenAPI document - and the server has been upgraded since then. - If a schema in the OpenAPI document defines the additionalProperties attribute, - then all undeclared properties received by the server are injected into the - additional properties map. In that case, there are undeclared properties, and - nothing to discard. + :param username: Username for HTTP basic authentication. + :param password: Password for HTTP basic authentication. + :param access_token: Access token. {{#hasHttpSignatureMethods}} :param signing_info: Configuration parameters for the HTTP signature security scheme. Must be an instance of {{{packageName}}}.signing.HttpSigningConfiguration {{/hasHttpSignatureMethods}} + :param server_index: Index to servers configuration. + :param server_variables: Mapping with string values to replace variables in + templated server configuration. The validation of enums is performed for + variables with defined enum values before. + :param server_operation_index: Mapping from operation ID to an index to server + configuration. + :param server_operation_variables: Mapping from operation ID to a mapping with + string values to replace variables in templated server configuration. + The validation of enums is performed for variables with defined enum + values before. + :param ssl_ca_cert: str - the path to a file of concatenated CA certificates + in PEM format. {{#hasAuthMethods}} :Example: @@ -107,7 +110,7 @@ conf = {{{packageName}}}.Configuration( 'Authorization' header, which is used to carry the signature. One may be tempted to sign all headers by default, but in practice it rarely works. - This is beccause explicit proxies, transparent proxies, TLS termination endpoints or + This is because explicit proxies, transparent proxies, TLS termination endpoints or load balancers may add/modify/remove headers. Include the HTTP headers that you know are not going to be modified in transit. @@ -135,19 +138,30 @@ conf = {{{packageName}}}.Configuration( _default = None - def __init__(self, host="{{{basePath}}}", + def __init__(self, host=None, api_key=None, api_key_prefix=None, username=None, password=None, - discard_unknown_keys=False, + access_token=None, {{#hasHttpSignatureMethods}} signing_info=None, {{/hasHttpSignatureMethods}} - ): + server_index=None, server_variables=None, + server_operation_index=None, server_operation_variables=None, + ssl_ca_cert=None, + ) -> None: """Constructor """ - self.host = host + self._base_path = "{{{basePath}}}" if host is None else host """Default Base url """ + self.server_index = 0 if server_index is None and host is None else server_index + self.server_operation_index = server_operation_index or {} + """Default server index + """ + self.server_variables = server_variables or {} + self.server_operation_variables = server_operation_variables or {} + """Default server variables + """ self.temp_folder_path = None """Temp file folder for downloading files """ @@ -171,7 +185,9 @@ conf = {{{packageName}}}.Configuration( self.password = password """Password for HTTP basic authentication """ - self.discard_unknown_keys = discard_unknown_keys + self.access_token = access_token + """Access token + """ {{#hasHttpSignatureMethods}} if signing_info is not None: signing_info.host = host @@ -179,18 +195,6 @@ conf = {{{packageName}}}.Configuration( """The HTTP signing configuration """ {{/hasHttpSignatureMethods}} -{{#hasOAuthMethods}} - self.access_token = None - """access token for OAuth/Bearer - """ -{{/hasOAuthMethods}} -{{^hasOAuthMethods}} -{{#hasBearerMethods}} - self.access_token = None - """access token for OAuth/Bearer - """ -{{/hasBearerMethods}} -{{/hasOAuthMethods}} self.logger = {} """Logging Settings """ @@ -202,7 +206,7 @@ conf = {{{packageName}}}.Configuration( self.logger_stream_handler = None """Log stream handler """ - self.logger_file_handler = None + self.logger_file_handler: Optional[FileHandler] = None """Log file handler """ self.logger_file = None @@ -217,7 +221,7 @@ conf = {{{packageName}}}.Configuration( Set this to false to skip verifying SSL certificate when calling API from https server. """ - self.ssl_ca_cert = None + self.ssl_ca_cert = ssl_ca_cert """Set this to customize the certificate file to verify the peer. """ self.cert_file = None @@ -229,6 +233,10 @@ conf = {{{packageName}}}.Configuration( self.assert_hostname = None """Set this to True/False to enable/disable SSL hostname verification. """ + self.tls_server_name = None + """SSL/TLS Server Name Indication (SNI) + Set this to the SNI value expected by the server. + """ {{#asyncio}} self.connection_pool_maxsize = 100 @@ -246,7 +254,7 @@ conf = {{{packageName}}}.Configuration( """ {{/asyncio}} - self.proxy = None + self.proxy: Optional[str] = None """Proxy URL """ self.proxy_headers = None @@ -258,9 +266,21 @@ conf = {{{packageName}}}.Configuration( self.retries = None """Adding retries to override urllib3 default value 3 """ - # Disable client side validation + # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + + self.datetime_format = "{{{datetimeFormat}}}" + """datetime format + """ + + self.date_format = "{{{dateFormat}}}" + """date format + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) @@ -279,7 +299,7 @@ conf = {{{packageName}}}.Configuration( object.__setattr__(self, name, value) {{#hasHttpSignatureMethods}} if name == "signing_info" and value is not None: - # Ensure the host paramater from signing info is the same as + # Ensure the host parameter from signing info is the same as # Configuration.host. value.host = self.host {{/hasHttpSignatureMethods}} @@ -293,21 +313,31 @@ conf = {{{packageName}}}.Configuration( :param default: object of Configuration """ - cls._default = copy.deepcopy(default) + cls._default = default @classmethod def get_default_copy(cls): - """Return new instance of configuration. + """Deprecated. Please use `get_default` instead. + + Deprecated. Please use `get_default` instead. + + :return: The configuration object. + """ + return cls.get_default() + + @classmethod + def get_default(cls): + """Return the default configuration. This method returns newly created, based on default constructor, object of Configuration class or returns a copy of default - configuration passed by the set_default method. + configuration. :return: The configuration object. """ - if cls._default is not None: - return copy.deepcopy(cls._default) - return Configuration() + if cls._default is None: + cls._default = Configuration() + return cls._default @property def logger_file(self): @@ -337,7 +367,7 @@ conf = {{{packageName}}}.Configuration( # then add file handler and remove stream handler. self.logger_file_handler = logging.FileHandler(self.__logger_file) self.logger_file_handler.setFormatter(self.logger_formatter) - for _, logger in six.iteritems(self.logger): + for _, logger in self.logger.items(): logger.addHandler(self.logger_file_handler) @property @@ -359,14 +389,14 @@ conf = {{{packageName}}}.Configuration( self.__debug = value if self.__debug: # if debug status is True, turn on debug logging - for _, logger in six.iteritems(self.logger): + for _, logger in self.logger.items(): logger.setLevel(logging.DEBUG) # turn on httplib debug httplib.HTTPConnection.debuglevel = 1 else: # if debug status is False, turn off debug logging, # setting log level to default `logging.WARNING` - for _, logger in six.iteritems(self.logger): + for _, logger in self.logger.items(): logger.setLevel(logging.WARNING) # turn off httplib debug httplib.HTTPConnection.debuglevel = 0 @@ -394,15 +424,16 @@ conf = {{{packageName}}}.Configuration( self.__logger_format = value self.logger_formatter = logging.Formatter(self.__logger_format) - def get_api_key_with_prefix(self, identifier): + def get_api_key_with_prefix(self, identifier, alias=None): """Gets API key (with prefix if set). :param identifier: The identifier of apiKey. + :param alias: The alternative identifier of apiKey. :return: The token for api key authentication. """ if self.refresh_api_key_hook is not None: self.refresh_api_key_hook(self) - key = self.api_key.get(identifier) + key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None) if key: prefix = self.api_key_prefix.get(identifier) if prefix: @@ -433,12 +464,15 @@ conf = {{{packageName}}}.Configuration( auth = {} {{#authMethods}} {{#isApiKey}} - if '{{keyParamName}}' in self.api_key: + if '{{name}}' in self.api_key{{#vendorExtensions.x-auth-id-alias}} or '{{.}}' in self.api_key{{/vendorExtensions.x-auth-id-alias}}: auth['{{name}}'] = { 'type': 'api_key', 'in': {{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}{{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}, 'key': '{{keyParamName}}', - 'value': self.get_api_key_with_prefix('{{keyParamName}}') + 'value': self.get_api_key_with_prefix( + '{{name}}',{{#vendorExtensions.x-auth-id-alias}} + alias='{{.}}',{{/vendorExtensions.x-auth-id-alias}} + ), } {{/isApiKey}} {{#isBasic}} @@ -532,14 +566,18 @@ conf = {{{packageName}}}.Configuration( {{/servers}} ] - def get_host_from_settings(self, index, variables=None): + def get_host_from_settings(self, index, variables=None, servers=None): """Gets host URL based on the index and variables :param index: array index of the host settings :param variables: hash of variable and the corresponding value + :param servers: an array of host settings or None :return: URL based on host settings """ + if index is None: + return self._base_path + variables = {} if variables is None else variables - servers = self.get_host_settings() + servers = self.get_host_settings() if servers is None else servers try: server = servers[index] @@ -551,7 +589,7 @@ conf = {{{packageName}}}.Configuration( url = server['url'] # go through variables and replace placeholders - for variable_name, variable in server['variables'].items(): + for variable_name, variable in server.get('variables', {}).items(): used_value = variables.get( variable_name, variable['default_value']) @@ -566,3 +604,14 @@ conf = {{{packageName}}}.Configuration( url = url.replace("{" + variable_name + "}", used_value) return url + + @property + def host(self): + """Return generated host.""" + return self.get_host_from_settings(self.server_index, variables=self.server_variables) + + @host.setter + def host(self, value): + """Fix base path.""" + self._base_path = value + self.server_index = None diff --git a/templates/python/partial_api_args.mustache b/templates/python/partial_api_args.mustache new file mode 100644 index 0000000..91f79a2 --- /dev/null +++ b/templates/python/partial_api_args.mustache @@ -0,0 +1,25 @@ +( + self, + {{#allParams}} + {{^vendorExtensions.x-isDomain}} + {{paramName}}: {{{vendorExtensions.x-py-typing}}}{{^required}} = None{{/required}}, + {{/vendorExtensions.x-isDomain}} + {{/allParams}} + {{#allParams}} + {{#vendorExtensions.x-isDomain}} + {{paramName}}: {{{vendorExtensions.x-py-typing}}} = "default", + {{/vendorExtensions.x-isDomain}} + {{/allParams}} + _request_timeout: Union[ + None, + Annotated[StrictFloat, Field(gt=0)], + Tuple[ + Annotated[StrictFloat, Field(gt=0)], + Annotated[StrictFloat, Field(gt=0)] + ] + ] = None, + _request_auth: Optional[Dict[StrictStr, Any]] = None, + _content_type: Optional[StrictStr] = None, + _headers: Optional[Dict[StrictStr, Any]] = None, + _host_index: Annotated[StrictInt, Field(ge=0, le={{#servers.size}}{{servers.size}}{{/servers.size}}{{^servers.size}}1{{/servers.size}})] = 0, + ) \ No newline at end of file diff --git a/templates/python/setup.mustache b/templates/python/setup.mustache index cb2f926..6d62a7c 100644 --- a/templates/python/setup.mustache +++ b/templates/python/setup.mustache @@ -4,25 +4,36 @@ from setuptools import setup, find_packages # noqa: H301 -NAME = "{{{projectName}}}" -VERSION = "{{packageVersion}}" -{{#apiInfo}} -{{#apis}} -{{^hasMore}} # To install the library, run the following # # python setup.py install # # prerequisite: setuptools # http://pypi.python.org/pypi/setuptools - -REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil"] +NAME = "{{{projectName}}}" +VERSION = "{{packageVersion}}" +PYTHON_REQUIRES = ">=3.7" +{{#apiInfo}} +{{#apis}} +{{#-last}} +REQUIRES = [ + "urllib3 >= 1.25.3, < 2.1.0", + "python-dateutil", {{#asyncio}} -REQUIRES.append("aiohttp >= 3.0.0") + "aiohttp >= 3.0.0", + "aiohttp-retry >= 2.8.3", {{/asyncio}} {{#tornado}} -REQUIRES.append("tornado>=4.2,<5") + "tornado>=4.2,<5", {{/tornado}} +{{#hasHttpSignatureMethods}} + "pem>=19.3.0", + "pycryptodome>=3.9.0", +{{/hasHttpSignatureMethods}} + "pydantic >= 2", + "typing-extensions >= 4.7.1", +] + setup( name=NAME, version=VERSION, @@ -32,14 +43,16 @@ setup( url="{{packageUrl}}", keywords=["pulp", "pulpcore", "client", "{{{appName}}}"], install_requires=REQUIRES, - python_requires='>=3.4', # restrict client usage to Python 3 only + python_required=PYTHON_REQUIRES, packages=find_packages(exclude=["test", "tests"]), include_package_data=True, - {{#licenseInfo}}license="{{licenseInfo}}", - {{/licenseInfo}}long_description="""\ - {{appDescription}} # noqa: E501 - """ + {{#licenseInfo}}license="{{.}}", + {{/licenseInfo}}long_description_content_type='text/markdown', + long_description="""\ + {{appDescription}} + """, # noqa: E501 + package_data={"{{{packageName}}}": ["py.typed"]}, ) -{{/hasMore}} +{{/-last}} {{/apis}} -{{/apiInfo}} \ No newline at end of file +{{/apiInfo}}