Add integration with flask

graphql
Inex Code 2022-06-24 16:05:18 +03:00
parent 75e3143c82
commit 99beee40d6
6 changed files with 47 additions and 4 deletions

View File

@ -9,6 +9,8 @@ from flask_restful import Api
from flask_swagger import swagger from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint from flask_swagger_ui import get_swaggerui_blueprint
from strawberry.flask.views import AsyncGraphQLView
from selfprivacy_api.resources.users import User, Users from selfprivacy_api.resources.users import User, Users
from selfprivacy_api.resources.common import ApiVersion from selfprivacy_api.resources.common import ApiVersion
from selfprivacy_api.resources.system import api_system from selfprivacy_api.resources.system import api_system
@ -21,6 +23,8 @@ from selfprivacy_api.migrations import run_migrations
from selfprivacy_api.utils.auth import is_token_valid from selfprivacy_api.utils.auth import is_token_valid
from selfprivacy_api.graphql import schema
swagger_blueprint = get_swaggerui_blueprint( swagger_blueprint = get_swaggerui_blueprint(
"/api/docs", "/api/swagger.json", config={"app_name": "SelfPrivacy API"} "/api/docs", "/api/swagger.json", config={"app_name": "SelfPrivacy API"}
) )
@ -83,6 +87,13 @@ def create_app(test_config=None):
return jsonify(swag) return jsonify(swag)
return jsonify({}), 404 return jsonify({}), 404
app.add_url_rule(
"/graphql",
view_func=AsyncGraphQLView.as_view(
"graphql", shema=schema
)
)
if app.config["ENABLE_SWAGGER"] == "1": if app.config["ENABLE_SWAGGER"] == "1":
app.register_blueprint(swagger_blueprint, url_prefix="/api/docs") app.register_blueprint(swagger_blueprint, url_prefix="/api/docs")

View File

@ -0,0 +1,15 @@
"""GraphQL API for SelfPrivacy."""
# pylint: disable=too-few-public-methods
import typing
import strawberry
from selfprivacy_api.graphql.queries.system import System
from selfprivacy_api.graphql.queries.api import Api
@strawberry.type
class Query:
"""Root schema for queries"""
system: System
api: Api
schema = strawberry.Schema(query=Query)

View File

@ -1,9 +1,12 @@
"""API access status""" """API access status"""
# pylint: disable=too-few-public-methods
import datetime import datetime
import string import string
import typing import typing
import strawberry import strawberry
from selfprivacy_api.resolvers.api import get_api_version
@strawberry.type @strawberry.type
class ApiDevice: class ApiDevice:
name: str name: str
@ -20,6 +23,6 @@ class ApiRecoveryKeyStatus:
@strawberry.type @strawberry.type
class Api: class Api:
version: str version: str = strawberry.field(resolver=get_api_version)
devices: typing.List[ApiDevice] devices: typing.List[ApiDevice]
recovery_key: ApiRecoveryKeyStatus recovery_key: ApiRecoveryKeyStatus

View File

@ -1,3 +1,5 @@
"""Common system information and settings"""
# pylint: disable=too-few-public-methods
import typing import typing
import strawberry import strawberry
@ -6,6 +8,7 @@ from selfprivacy_api.graphql.queries.providers import DnsProvider, ServerProvide
@strawberry.type @strawberry.type
class DnsRecord: class DnsRecord:
"""DNS record"""
recordType: str recordType: str
name: str name: str
content: str content: str
@ -14,6 +17,7 @@ class DnsRecord:
@strawberry.type @strawberry.type
class SystemDomainInfo: class SystemDomainInfo:
"""Information about the system domain"""
domain: str domain: str
hostname: str hostname: str
provider: DnsProvider provider: DnsProvider
@ -21,28 +25,33 @@ class SystemDomainInfo:
@strawberry.type @strawberry.type
class AutoUpgradeOptions: class AutoUpgradeOptions:
"""Automatic upgrade options"""
enable: bool enable: bool
allow_reboot: bool allow_reboot: bool
@strawberry.type @strawberry.type
class SshSettings: class SshSettings:
"""SSH settings and root SSH keys"""
enable: bool enable: bool
password_authentication: bool password_authentication: bool
root_ssh_keys: typing.List[str] root_ssh_keys: typing.List[str]
@strawberry.type @strawberry.type
class SystemSettings: class SystemSettings:
"""Common system settings"""
auto_upgrade: AutoUpgradeOptions auto_upgrade: AutoUpgradeOptions
ssh: SshSettings ssh: SshSettings
timezone: str timezone: str
@strawberry.type @strawberry.type
class SystemInfo: class SystemInfo:
"""System components versions"""
system_version: str system_version: str
python_version: str python_version: str
@strawberry.type @strawberry.type
class SystemProviderInfo: class SystemProviderInfo:
"""Information about the VPS/Dedicated server provider"""
provider: ServerProvider provider: ServerProvider
id: str id: str
@ -56,4 +65,4 @@ class System:
settings: SystemSettings settings: SystemSettings
info: SystemInfo info: SystemInfo
provider: SystemProviderInfo provider: SystemProviderInfo
busy: bool busy: bool

View File

@ -0,0 +1,5 @@
"""Resolvers for API module"""
def get_api_version() -> str:
"""Get API version"""
return "1.2.7"

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Unassigned views""" """Unassigned views"""
from flask_restful import Resource from flask_restful import Resource
from selfprivacy_api.resolvers.api import get_api_version
class ApiVersion(Resource): class ApiVersion(Resource):
"""SelfPrivacy API version""" """SelfPrivacy API version"""
@ -23,4 +23,4 @@ class ApiVersion(Resource):
401: 401:
description: Unauthorized description: Unauthorized
""" """
return {"version": "1.2.7"} return {"version": get_api_version()}