Reorganizar arquivos
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
from functools import wraps
|
||||
from flask import request
|
||||
from api.app import app
|
||||
from .fetch import get_trainer_by_nick_fail, NotFound
|
||||
from .errors import AuthenticationFailure
|
||||
import jwt
|
||||
|
||||
# autenticação do trainer (decorator)
|
||||
def token_required(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
try:
|
||||
token = request.headers["authorization"]
|
||||
data = jwt.decode(token, app.config["SECRET_KEY"], algorithms=["HS256"])
|
||||
trainer = get_trainer_by_nick_fail(data["username"])
|
||||
except (TypeError, KeyError):
|
||||
return AuthenticationFailure("JWT token required")
|
||||
except NotFound:
|
||||
return AuthenticationFailure("Trainer not found")
|
||||
except:
|
||||
return AuthenticationFailure("JWT token is invalid or expired")
|
||||
|
||||
return f(trainer, *args, **kwargs)
|
||||
return decorated
|
||||
|
@@ -1,48 +0,0 @@
|
||||
from api.models.trainer import Trainer
|
||||
import requests
|
||||
import json
|
||||
|
||||
class NotFound(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def get_or_not_found(callback):
|
||||
try:
|
||||
resource = callback()
|
||||
if resource is None:
|
||||
raise NotFound("Resource not found")
|
||||
return resource
|
||||
except:
|
||||
raise NotFound("Resource not found")
|
||||
|
||||
def get_trainer_fail(id):
|
||||
return get_or_not_found(lambda : Trainer.query.get(id))
|
||||
|
||||
def get_trainer_by_nick_fail(nickname):
|
||||
return get_or_not_found(lambda : Trainer.query.filter_by(nickname=nickname).one())
|
||||
|
||||
def get_pokemon_fail(trainer, id):
|
||||
return get_or_not_found(lambda : trainer.pokemons_list.filter_by(id=id).one())
|
||||
|
||||
# helper interno
|
||||
def cant_fetch_error(pokemon):
|
||||
raise NotFound("Could not fetch data for pokemon with id {}".format(pokemon.pokemon_id))
|
||||
|
||||
# seguintes funções puxam informações da pokeapi
|
||||
def set_pokemon_data(pokemon):
|
||||
try:
|
||||
response = requests.get("https://pokeapi.co/api/v2/pokemon/{}".format(pokemon.pokemon_id))
|
||||
if response.status_code != 200:
|
||||
cant_fetch_error(pokemon)
|
||||
pokemon.pokemon_data = json.loads(response.text)
|
||||
except:
|
||||
cant_fetch_error(pokemon)
|
||||
|
||||
async def async_set_pokemon_data(session, pokemon):
|
||||
try:
|
||||
response = await session.get("https://pokeapi.co/api/v2/pokemon/{}".format(pokemon.pokemon_id))
|
||||
if response.status != 200:
|
||||
cant_fetch_error(pokemon)
|
||||
pokemon.pokemon_data = json.loads(await response.text())
|
||||
except:
|
||||
cant_fetch_error(pokemon)
|
@@ -1,33 +0,0 @@
|
||||
from flask import request
|
||||
|
||||
class ParsingException(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def parse_int(name, minimum):
|
||||
try:
|
||||
result = int(request.args.get(name, minimum))
|
||||
except ValueError:
|
||||
raise ParsingException("Couldn't parse {} as integer".format(name))
|
||||
|
||||
if result < minimum:
|
||||
raise ParsingException("{} must be greater than {}".format(name, minimum))
|
||||
|
||||
return result
|
||||
|
||||
def parse_limit():
|
||||
return parse_int("limit", -1)
|
||||
|
||||
def parse_offset():
|
||||
return parse_int("offset", 0)
|
||||
|
||||
def parse_json_obj():
|
||||
try:
|
||||
json = request.get_json()
|
||||
except:
|
||||
raise ParsingException("Failed to parse JSON body")
|
||||
|
||||
if type(json) is not dict:
|
||||
raise ParsingException("Expected JSON object as body")
|
||||
|
||||
return json
|
@@ -1,8 +1,8 @@
|
||||
from api.models.pokemon_owned import pokemon_owned_schema, pokemon_owned_schemas, PokemonOwned
|
||||
from api.app import db
|
||||
from .parse_args import parse_limit, parse_offset, ParsingException, parse_json_obj
|
||||
from api.util.fetch import *
|
||||
from api.util.parse_args import parse_limit, parse_offset, ParsingException, parse_json_obj
|
||||
from .errors import ParsingError, FetchError, ConflictingResources
|
||||
from .fetch import *
|
||||
from aiohttp import ClientSession
|
||||
import asyncio
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
@@ -4,7 +4,7 @@ from api.app import db
|
||||
from api.app import app
|
||||
from flask import request, jsonify
|
||||
from .errors import *
|
||||
from .parse_args import parse_limit, parse_offset, ParsingException, parse_json_obj
|
||||
from api.util.parse_args import parse_limit, parse_offset, ParsingException, parse_json_obj
|
||||
from werkzeug.security import check_password_hash
|
||||
import datetime
|
||||
import jwt
|
||||
|
Reference in New Issue
Block a user