Skip to content

Commit

Permalink
Update python-flask templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslewisakl committed Jun 5, 2022
1 parent ffb2a69 commit 647bae7
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 145 deletions.
Binary file modified src/main/resources/servers.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
from obasparql import QueryManager
from {{packageName}}.settings import *
import logging

try:
logging.config.fileConfig(logging_file)
except:
logging.error("Logging config file does not exist {}".format(logging_file))
exit(0)

logger = logging.getLogger(__name__)

query_manager = QueryManager(queries_dir=QUERY_DIRECTORY,
context_dir=CONTEXT_DIRECTORY,
endpoint=ENDPOINT,
endpoint_username=ENDPOINT_USERNAME,
endpoint_password=ENDPOINT_PASSWORD,
named_graph_base=ENDPOINT_GRAPH_BASE,
uri_prefix=ENDPOINT_RESOURCE_PREFIX)

Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
import logging
from pathlib import Path

import connexion
from flask_testing import TestCase
from {{packageName}}.cached import CachedSpecification
from {{packageName}}.encoder import JSONEncoder
from {{packageName}}.settings import *
from connexion.spec import Specification

import logging.config
from obasparql import QueryManager


query_manager = QueryManager(queries_dir=QUERY_DIRECTORY,
context_dir=CONTEXT_DIRECTORY,
queries_types=QUERIES_TYPES,
endpoint=ENDPOINT,
endpoint_username=ENDPOINT_USERNAME,
endpoint_password=ENDPOINT_PASSWORD,
graph_base=ENDPOINT_GRAPH_BASE,
prefix=ENDPOINT_RESOURCE_PREFIX)

from {{packageName}}.encoder import JSONEncoder


class BaseTestCase(TestCase):
logging_file = Path(__file__).parent.parent / "settings" / "logging.ini"
try:
logging.config.fileConfig(logging_file)
except:
logging.error("Logging config file does not exist {}".format(logging_file))
exit(0)
logger = logging.getLogger(__name__)

def create_app(self):
Specification.from_file = CachedSpecification.from_file
logging.getLogger('connexion.operation').setLevel('ERROR')
app = connexion.App(__name__, specification_dir='../openapi/')
app.app.json_encoder = JSONEncoder
app.add_api('openapi.yaml',
pythonic_params=False,
validate_responses=True)
app.add_api('openapi.yaml', pythonic_params=True)
return app.app
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
#!/usr/bin/env python3
{{/supportPython2}}

import connexion
from connexion.spec import Specification
from {{packageName}}.cached import CachedSpecification
import connexion # 1
{{#featureCORS}}
from flask_cors import CORS
{{/featureCORS}}

from {{packageName}} import encoder


def main():
app = connexion.App(__name__, specification_dir='./openapi/')
app.app.json_encoder = encoder.JSONEncoder
Specification.from_file = CachedSpecification.from_file
app.add_api('openapi.yaml',
arguments={'title': '{{appName}}'},
pythonic_params=False)
pythonic_params=True)

{{#featureCORS}}
# add CORS support
CORS(app.app)

{{/featureCORS}}
app.run(port={{serverPort}})


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ T = typing.TypeVar('T')
{{/supportPython2}}


class Model(object):
class Model({{#supportPython2}}object{{/supportPython2}}):
# openapiTypes: The key is attribute name and the
# value is attribute type.
openapi_types = {}
openapi_types{{^supportPython2}}: typing.Dict[str, type]{{/supportPython2}} = {}

# attributeMap: The key is attribute name and the
# value is json key in definition.
attribute_map = {}
attribute_map{{^supportPython2}}: typing.Dict[str, str]{{/supportPython2}} = {}

@classmethod
def from_dict(cls{{^supportPython2}}: typing.Type[T]{{/supportPython2}}, dikt){{^supportPython2}} -> T{{/supportPython2}}:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import connexion
import connexion #2
import six
from openapi_server import query_manager
from openapi_server.utils.vars import {{#lambda.uppercase}}{{baseName}}_type_name{{/lambda.uppercase}}, {{#lambda.uppercase}}{{baseName}}_type_uri{{/lambda.uppercase}}
from typing import Dict
from typing import Tuple
from typing import Union

{{#imports}}{{import}} # noqa: E501
{{#imports}}{{import}} # noqa: E501 zzz
{{/imports}}
from {{packageName}} import util
{{#operations}}
{{#operation}}

def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): # noqa: E501
"""{{#summary}}{{.}}{{/summary}}{{^summary}}{{operationId}}{{/summary}}

{{#notes}}{{.}}{{/notes}} # noqa: E501
def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{^-last}}, {{/-last}}-{{/allParams}}): # noqa: E501 yyy
"""{{summary}}{{^summary}}{{operationId}}{{/summary}}

{{notes}} # noqa: E501

{{#allParams}}
:param {{paramName}}: {{description}}
Expand All @@ -34,7 +36,7 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
{{/isFile}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isListContainer}}
{{#isArray}}
{{#items}}
{{#isPrimitiveType}}
:type {{paramName}}: List[{{>param_type}}]
Expand All @@ -43,8 +45,8 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
:type {{paramName}}: list | bytes
{{/isPrimitiveType}}
{{/items}}
{{/isListContainer}}
{{#isMapContainer}}
{{/isArray}}
{{#isMap}}
{{#items}}
{{#isPrimitiveType}}
:type {{paramName}}: Dict[str, {{>param_type}}]
Expand All @@ -53,12 +55,11 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
:type {{paramName}}: dict | bytes
{{/isPrimitiveType}}
{{/items}}
{{/isMapContainer}}
{{/isMap}}
{{/allParams}}

:rtype: {{#returnType}}{{.}}{{/returnType}}{{^returnType}}None{{/returnType}}
:rtype: Union[{{returnType}}{{^returnType}}None{{/returnType}}, Tuple[{{returnType}}{{^returnType}}None{{/returnType}}, int], Tuple[{{returnType}}{{^returnType}}None{{/returnType}}, int, Dict[str, str]]
"""

{{#allParams}}
{{^isContainer}}
{{#isDate}}
Expand All @@ -71,12 +72,12 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
{{^isFile}}
{{^isUuid}}
if connexion.request.is_json:
{{paramName}} = {{baseType}}.from_dict(connexion.request.get_json()) # noqa: E501
{{paramName}} = {{baseType}}{{^baseType}}{{#dataType}} {{.}}{{/dataType}}{{/baseType}}.from_dict(connexion.request.get_json()) # noqa: E501
{{/isUuid}}
{{/isFile}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isListContainer}}
{{#isArray}}
{{#items}}
{{#isDate}}
if connexion.request.is_json:
Expand All @@ -91,8 +92,8 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
{{paramName}} = [{{complexType}}.from_dict(d) for d in connexion.request.get_json()] # noqa: E501
{{/complexType}}
{{/items}}
{{/isListContainer}}
{{#isMapContainer}}
{{/isArray}}
{{#isMap}}
{{#items}}
{{#isDate}}
if connexion.request.is_json:
Expand All @@ -107,14 +108,8 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
{{paramName}} = {k: {{baseType}}.from_dict(v) for k, v in six.iteritems(connexion.request.get_json())} # noqa: E501
{{/complexType}}
{{/items}}
{{/isMapContainer}}
{{/isMap}}
{{/allParams}}

return query_manager.{{#lambda.lowercase}}{{httpMethod}}_resource{{/lambda.lowercase}}({{#pathParams}}{{paramName}}={{paramName}}{{#hasMore}},{{/hasMore}}{{#-last}},{{/-last}}{{/pathParams}}{{#queryParams}}
{{paramName}}={{paramName}}{{#hasMore}},{{/hasMore}}{{#-last}},{{/-last}}{{/queryParams}}{{#bodyParams}}
body={{paramName}}{{#hasMore}},{{/hasMore}}{{#-last}},{{/-last}}{{/bodyParams}}
rdf_type_uri={{#lambda.uppercase}}{{baseName}}_type_uri{{/lambda.uppercase}},
rdf_type_name={{#lambda.uppercase}}{{baseName}}_type_name{{/lambda.uppercase}},
kls={{baseName}})
return 'do some magic!'
{{/operation}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class {{#operations}}Test{{classname}}(BaseTestCase):
{{paramName}} = {{{example}}}
{{/bodyParam}}
{{#queryParams}}
{{#-first}}query_string = [{{/-first}}{{^-first}} {{/-first}}('{{^vendorExtensions.x-python-connexion-openapi-name}}{{paramName}}{{/vendorExtensions.x-python-connexion-openapi-name}}{{#vendorExtensions.x-python-connexion-openapi-name}}{{vendorExtensions.x-python-connexion-openapi-name}}{{/vendorExtensions.x-python-connexion-openapi-name}}', {{{example}}}){{#hasMore}},{{/hasMore}}{{#-last}}]{{/-last}}
{{#-first}}query_string = [{{/-first}}{{^-first}} {{/-first}}('{{^vendorExtensions.x-python-connexion-openapi-name}}{{paramName}}{{/vendorExtensions.x-python-connexion-openapi-name}}{{#vendorExtensions.x-python-connexion-openapi-name}}{{vendorExtensions.x-python-connexion-openapi-name}}{{/vendorExtensions.x-python-connexion-openapi-name}}', {{{example}}}){{^-last}},{{/-last}}{{#-last}}]{{/-last}}
{{/queryParams}}
headers = { {{#vendorExtensions.x-prefered-produce}}
'Accept': '{{mediaType}}',{{/vendorExtensions.x-prefered-produce}}{{#vendorExtensions.x-prefered-consume}}
'Content-Type': '{{mediaType}}',{{/vendorExtensions.x-prefered-consume}}{{#headerParams}}
headers = { {{#vendorExtensions.x-preferred-produce}}
'Accept': '{{mediaType}}',{{/vendorExtensions.x-preferred-produce}}{{#vendorExtensions.x-preferred-consume}}
'Content-Type': '{{mediaType}}',{{/vendorExtensions.x-preferred-consume}}{{#headerParams}}
'{{paramName}}': {{{example}}},{{/headerParams}}{{#authMethods}}
{{#isOAuth}}'Authorization': 'Bearer special-key',{{/isOAuth}}{{#isApiKey}}'{{name}}': 'special-key',{{/isApiKey}}{{#isBasicBasic}}'Authorization': 'BasicZm9vOmJhcg==',{{/isBasicBasic}}{{#isBasicBearer}}'Authorization': 'Bearer special-key',{{/isBasicBearer}}{{/authMethods}}
{{#isOAuth}}'Authorization': 'Bearer special-key',{{/isOAuth}}{{#isApiKey}}'{{name}}': 'special-key',{{/isApiKey}}{{#isBasicBasic}}'Authorization': 'Basic Zm9vOmJhcg==',{{/isBasicBasic}}{{#isBasicBearer}}'Authorization': 'Bearer special-key',{{/isBasicBearer}}{{/authMethods}}
}
{{#formParams}}
{{#-first}}data = dict({{/-first}}{{^-first}} {{/-first}}{{paramName}}={{{example}}}{{#hasMore}},{{/hasMore}}{{#-last}}){{/-last}}
{{#-first}}data = dict({{/-first}}{{^-first}} {{/-first}}{{paramName}}={{{example}}}{{^-last}},{{/-last}}{{#-last}}){{/-last}}
{{/formParams}}
response = self.client.open(
'{{#contextPath}}{{{.}}}{{/contextPath}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}={{{example}}}{{#hasMore}}, {{/hasMore}}{{^hasMore}}){{/hasMore}}{{/pathParams}},
'{{{contextPath}}}{{{path}}}'{{#pathParams}}{{#-first}}.format({{/-first}}{{paramName}}={{{example}}}{{^-last}}, {{/-last}}{{#-last}}){{/-last}}{{/pathParams}},
method='{{httpMethod}}',
headers=headers{{#bodyParam}},
data=json.dumps({{paramName}}){{^consumes}},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
# Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com"

git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4

if [ "$git_host" = "" ]; then
git_host="{{{gitHost}}}"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi

if [ "$git_user_id" = "" ]; then
git_user_id="{{{gitUserId}}}"
Expand All @@ -32,21 +38,20 @@ git add .
git commit -m "$release_note"

# Sets the new remote
git_remote=`git remote`
git_remote=$(git remote)
if [ "$git_remote" = "" ]; then # git remote not defined

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git
fi

fi

git pull origin master

# Pushes (Forces) the changes in the local repository up to the remote repository
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
git push origin master 2>&1 | grep -v 'To https'

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ coverage.xml
*,cover
.hypothesis/
venv/
.venv/
.python-version
.pytest_cache

# Translations
*.mo
Expand Down
Loading

0 comments on commit 647bae7

Please sign in to comment.