示例#1
0
"""
api_v3 routes.
"""
import json

from kyoukai import Blueprint
from kyoukai.asphalt import HTTPRequestContext
from werkzeug.wrappers import Response

from owapi.blizz_interface import fetch_all_user_pages
from owapi.blizz_interface import get_all_heroes
from owapi.blizz_interface import get_hero_data
from owapi.v3 import parsing
from owapi.v3.v3_util import with_ratelimit

api_v3 = Blueprint("api_v3", prefix="/v3")


@api_v3.after_request
async def add__request(ctx: HTTPRequestContext, r: Response):
    # Edit the body, and add a _request.
    if isinstance(r.response, list):
        h = r.response[0]
    else:
        h = r.response
    if isinstance(h, dict):
        # Add a _request var to the body.
        h["_request"] = {"api_ver": 3, "route": ctx.request.path}

    return r
示例#2
0
文件: app.py 项目: ThomasRoncen/OWAPI
    if ctx.app.config["owapi_do_profiling"]:
        pr = ctx.app.config['owapi_profiling_obj']
        pr.disable()
        s = io.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        # print into s, with regex filter
        ps.print_stats("owapi")
        # strip useless part of path infos and print with logger
        logger.info(s.getvalue().replace(
            os.path.split(os.path.dirname(os.path.realpath(__file__)))[0] + "/", ""
        ))
    return response


# Create the api blueprint and add children
api_bp = Blueprint("api", prefix="/api")


@api_bp.after_request
async def jsonify(ctx, response: Response):
    """
    JSONify the response.
    """
    if not isinstance(response.response, dict):
        return response

    # json.dump the body.
    status_code = response.status_code
    if not any(response.response.values()):
        status_code = 404
    if ctx.request.args.get("format", "json") in ["json_pretty", "pretty"]:
示例#3
0
文件: app.py 项目: azah/OWAPI
@app.root.after_request
async def stop_profiling(ctx: HTTPRequestContext, response: Response):
    if ctx.app.config["owapi_do_profiling"]:
        pr = ctx.cfg['owapi_profiling_obj']
        pr.disable()
        s = io.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        # print into s, with regex filter
        ps.print_stats("owapi")
        # strip useless part of path infos and print with logger
        logger.info(s.getvalue().replace(os.path.split(os.path.dirname(os.path.realpath(__file__)))[0] + "/", ""))
    return response


# Create the api blueprint and add children
api_bp = Blueprint("api", prefix="/api")


@api_bp.after_request
async def jsonify(ctx, response: Response):
    """
    JSONify the response.
    """
    if not isinstance(response.response, dict):
        return response

    # json.dump the body.
    status_code = response.status_code
    if not any(response.response.values()):
        status_code = 404
    if ctx.request.args.get("format", "json") == "json_pretty":
示例#4
0
api_v3 routes.
"""
import json

from kyoukai import HTTPRequestContext
from kyoukai import Response
from kyoukai import Blueprint

from owapi.blizz_interface import fetch_all_user_pages
from owapi.v3.v3_util import with_ratelimit
from owapi.v3 import parsing

from owapi.blizz_interface import get_hero_data
from owapi.blizz_interface import get_all_heroes

api_v3 = Blueprint("api_v3", url_prefix="/v3", reverse_hooks=True)


@api_v3.after_request
async def add__request(ctx: HTTPRequestContext, r: Response):
    # Edit the body, and add a _request.
    if isinstance(r.body, dict):
        # Add a _request var to the body.
        r.body["_request"] = {"api_ver": 3, "route": ctx.request.path}

    return r


@api_v3.errorhandler(404)
async def e404(ctx: HTTPRequestContext, exc):
    return json.dumps({"error": 404, "msg": "profile not found"}), \
示例#5
0
文件: app.py 项目: Js41637/OWAPI
    if ctx.app.config["owapi_do_profiling"]:
        pr = ctx.app.config['owapi_profiling_obj']
        pr.disable()
        s = io.StringIO()
        ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
        # print into s, with regex filter
        ps.print_stats("owapi")
        # strip useless part of path infos and print with logger
        logger.info(s.getvalue().replace(
            os.path.split(os.path.dirname(os.path.realpath(__file__)))[0] + "/", ""
        ))
    return response


# Create the api blueprint and add children
api_bp = Blueprint("api", prefix="/api")


@api_bp.after_request
async def jsonify(ctx, response: Response):
    """
    JSONify the response.
    """
    if not isinstance(response.response, dict):
        return response

    # json.dump the body.
    status_code = response.status_code
    if not any(response.response.values()):
        status_code = 404
    if ctx.request.args.get("format", "json") in ["json_pretty", "pretty"]:
示例#6
0
文件: app.py 项目: Dizolivemint/OWAPI
        "error": 500,
        "msg": "please report this!",
        "exc": repr(exc.__cause__)
    }
    logger.error("Unhandled exception - Blizzard format probably changed!")
    traceback.print_exc()
    return json.dumps(obb), 500, {"Content-Type": "application/json"}


@app.root.errorhandler(404)
async def e404(ctx: HTTPRequestContext, exc: HTTPException):
    return json.dumps({"error": 404}), 404, {"Content-Type": "application/json"}


# Create the api blueprint and add children
api_bp = Blueprint("api", url_prefix="/api")


@api_bp.after_request
async def jsonify(ctx, response: Response):
    """
    JSONify the response.
    """
    if isinstance(response.body, str):
        return response

    # json.dump the body.
    status_code = response.code
    if not any(response.body.values()):
        status_code = 404
    if ctx.request.args.get("format", "json") == "json_pretty":
示例#7
0
import json
from kyoukai import Blueprint
from .util import jsonify
from .models import User

api_v1 = Blueprint("api_v1", url_prefix="/api/v1")


@api_v1.route("/user", methods=["POST"])
async def register_user(ctx):
    body = json.loads(ctx.request.body)
    user = User(username=body["username"],
                password=body["password"],
                email=body["email"])
    ctx.dbsession.add(user)
    ctx.dbsession.commit()
    return "", 200


@api_v1.route("/login", methods=["POST"])
async def login_user(ctx):
    body = json.loads(ctx.request.body)
    user = User.query.filter_by(username=body["username"]).first()
    if user and user.password == body["password"]:
        return jsonify({"access_token": str(user.access_token)})
    else:
        return "Wrong username or password.", 401


@api_v1.route("/system", methods=["POST"])
async def register_system(ctx):