Skip to content

Commit

Permalink
feat: add backend example for spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cowan-macady committed May 15, 2023
1 parent 8b42995 commit 18fd667
Show file tree
Hide file tree
Showing 19 changed files with 1,854 additions and 0 deletions.
105 changes: 105 additions & 0 deletions examples/sdk-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.pytest_cache/


# Django:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask:
instance/
.webassets-cache

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
#Pipfile.lock

__pypackages__/


# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
.idea
32 changes: 32 additions & 0 deletions examples/sdk-backend/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
connexion = {version = ">=2.6.0", extras = ["swagger-ui"]}
swagger-ui-bundle = ">=0.0.4"
python-dateutil = ">=2.6.0"
indykite-sdk-python = {ref = "v1.20.0", git = "https://github.com/indykite/indykite-sdk-python"}
python-jose = "3.3.0"
gql = "3.4.0"
requests = "2.28.2"
requests-toolbelt = "2.1.0"
flask = "2.3.1"
markupsafe = "2.1.2"
Werkzeug = "2.3.0"
Flask-JWT-Extended = "4.4.4"
Flask-Migrate = "4.0.4"
Flask-SQLAlchemy = "3.0.3"
SQLAlchemy = "2.0.10"
pydantic = "1.10.7"
email-validator = "2.0.0.post2"
python-dotenv = "1.0.0"
psycopg2-binary = "3.1.18"
flask-openapi3 = "2.3.2"
Flask-RQ2 = "18.3"
rq = "1.13.0"
redis = "4.5.4"

[requires]
python_version = "3.11"
1,330 changes: 1,330 additions & 0 deletions examples/sdk-backend/Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/sdk-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


61 changes: 61 additions & 0 deletions examples/sdk-backend/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import traceback
from flask import redirect, url_for
from flask_openapi3 import HTTPBearer, HTTPBase
from flask_openapi3 import Info
from flask_openapi3 import OpenAPI
from werkzeug.exceptions import HTTPException


def init_exception(app: OpenAPI):
from app.utils.exceptions import BaseAPIException

@app.errorhandler(Exception)
def handler(e):
if isinstance(e, HTTPException):
code = e.code
message = e.description
return BaseAPIException(code, message)
else:
print(traceback.format_exc())
return e


def register_apis(app: OpenAPI):
from app.api.app_space import api as app_space_api
from app.api.tenant import api as tenant_api
from app.api.app_with_agent_credentials import api as app_with_agent_credentials_api
app.register_api(app_space_api)
app.register_api(tenant_api)
app.register_api(app_with_agent_credentials_api)


def create_app():
from . import config
app = OpenAPI(
__name__,
info=Info(title=config.APP_NAME, version=config.APP_VERSION),
security_schemes={
"basic": HTTPBase(),
"jwt": HTTPBearer(bearerFormat="JWT")
},
doc_expansion="none",
)

app.json.ensure_ascii = False
app.config.from_object(config)
init_exception(app)
register_apis(app)
return app


app = create_app()


@app.route("/")
def index():
return redirect(url_for("openapi.swagger"))


if __name__ == "__main__":
app.run(debug=True)

1 change: 1 addition & 0 deletions examples/sdk-backend/app/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

37 changes: 37 additions & 0 deletions examples/sdk-backend/app/api/app_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from flask_openapi3 import APIBlueprint
from flask_openapi3 import Tag
from indykite_sdk.config import ConfigClient
from app.config import API_PREFIX
from app.form.app_space import AppSpaceById, AppSpaceCreate
from app.utils.response import get_response, response_data
from app.utils.helper import change_display_to_name

__version__ = "/v1"
__bp__ = "/app_space"
url_prefix = API_PREFIX + __version__ + __bp__
tag = Tag(name="AppSpace", description="AppSpace")
api = APIBlueprint(__bp__, __name__, url_prefix=url_prefix, abp_tags=[tag])


@api.post("")
def create_app_space(body: AppSpaceCreate):
client_config = ConfigClient()
app_space = client_config.create_app_space(body.customer_id,
change_display_to_name(body.display_name),
body.display_name,
body.description,
[])
if app_space:
return response_data("AppSpaceCreate", get_response(app_space))
else:
return response_data("AppSpaceCreate", "Invalid app_space creation")


@api.get("/<id>")
def get_app_space(path: AppSpaceById):
client_config = ConfigClient()
app_space = client_config.get_app_space_by_id(path.id)
if app_space:
return response_data("AppSpaceById", get_response(app_space))
else:
return response_data("AppSpaceById", "Invalid app_space id")
43 changes: 43 additions & 0 deletions examples/sdk-backend/app/api/app_with_agent_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from flask_openapi3 import APIBlueprint
from flask_openapi3 import Tag
from indykite_sdk.config import ConfigClient
from app.config import API_PREFIX
from app.form.app_with_agent_credentials import ApplicationWithAgentCredentialsCreate
from app.utils.response import get_response, get_credentials_response, response_data
import json

__version__ = "/v1"
__bp__ = "/app_with_agent_credentials"
url_prefix = API_PREFIX + __version__ + __bp__
tag = Tag(name=" ApplicationWithAgentCredentials", description=" ApplicationWithAgentCredentials")
api = APIBlueprint(__bp__, __name__, url_prefix=url_prefix, abp_tags=[tag])


@api.post("")
def create_app_with_agent_credentials(body: ApplicationWithAgentCredentialsCreate):
client_config = ConfigClient()
app_with_agent_credentials = client_config.create_application_with_agent_credentials(
body.app_space_id,
body.tenant_id,
body.application_name,
body.application_agent_name,
body.application_agent_credentials_name,
"jwk",
None,
None)
if app_with_agent_credentials:
cred = {'ApplicationWithAgentCredentials': []}
for k, v in app_with_agent_credentials.items():
app = {k: []}
if k == "response_application_agent_credentials":
app_dict = get_credentials_response(v)
else:
app_dict = json.loads(get_response(v))
app[k].append(app_dict)
cred['ApplicationWithAgentCredentials'].append(app)
return response_data("ApplicationWithAgentCredentialsCreate",cred)
else:
return response_data("ApplicationWithAgentCredentialsCreate", "Invalid app_with_agent_credentials creation")



38 changes: 38 additions & 0 deletions examples/sdk-backend/app/api/tenant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask_openapi3 import APIBlueprint
from flask_openapi3 import Tag
from indykite_sdk.config import ConfigClient
from app.config import API_PREFIX
from app.form.tenant import TenantById, TenantCreate
from app.utils.response import get_response, response_data
from app.utils.helper import change_display_to_name

__version__ = "/v1"
__bp__ = "/tenant"
url_prefix = API_PREFIX + __version__ + __bp__
tag = Tag(name="Tenant", description="Tenant")
api = APIBlueprint(__bp__, __name__, url_prefix=url_prefix, abp_tags=[tag])


@api.post("")
def create_tenant(body: TenantCreate):
print(body.display_name)

client_config = ConfigClient()
tenant = client_config.create_tenant(body.issuer_id,
change_display_to_name(body.display_name),
body.display_name,
body.description, [])
if tenant:
return response_data("TenantCreate", get_response(tenant))
else:
return response_data("TenantCreate", "Invalid tenant creation")


@api.get("/<id>")
def get_tenant(path: TenantById):
client_config = ConfigClient()
tenant = client_config.get_tenant_by_id(path.id)
if tenant:
return response_data("TenantById", get_response(tenant))
else:
return response_data("TenantById", "Invalid tenant id")
5 changes: 5 additions & 0 deletions examples/sdk-backend/app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APP_NAME = "SDK example"
APP_VERSION = "1.0.0"
API_PREFIX = "/api"

# INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE in env
20 changes: 20 additions & 0 deletions examples/sdk-backend/app/form/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from flask_openapi3 import FileStorage
from pydantic import BaseModel, Field


class PageModel(BaseModel):
page: int = Field(1, ge=1, description="Page")
page_size: int = Field(15, ge=1, description="Page size")


class IdModel(BaseModel):
id: int = Field(..., description="ID")


class FileModel(BaseModel):
file: FileStorage


class JsonResponse(BaseModel):
code: int = Field(default=0, description="Code")
message: str = Field(default="ok", description="Message")
11 changes: 11 additions & 0 deletions examples/sdk-backend/app/form/app_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel, Field


class AppSpaceById(BaseModel):
id: str = Field(..., description="gid")


class AppSpaceCreate(BaseModel):
customer_id: str = Field(..., description="Customer gid id")
display_name: str = Field(..., description="AppSpace display name")
description: str = Field(..., description="AppSpace description")
9 changes: 9 additions & 0 deletions examples/sdk-backend/app/form/app_with_agent_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import BaseModel, Field


class ApplicationWithAgentCredentialsCreate(BaseModel):
app_space_id: str = Field(..., description="AppSpace gid id")
tenant_id: str = Field(..., description="Tenant gid id")
application_name: str = Field(..., description="Application display name")
application_agent_name: str = Field(..., description="Application Agent name")
application_agent_credentials_name: str = Field(..., description="Application Agent Credentials name")
11 changes: 11 additions & 0 deletions examples/sdk-backend/app/form/tenant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel, Field


class TenantById(BaseModel):
id: str = Field(..., description="gid")


class TenantCreate(BaseModel):
issuer_id: str = Field(..., description="Issuer id")
display_name: str = Field(..., description="Tenant display name")
description: str = Field(..., description="Tenant description")
Empty file.

0 comments on commit 18fd667

Please sign in to comment.