def _auth_internal(): token = request.headers.get("Authorization") if not token: raise BadRequest("Missing auth header") if token != SoccerConfig.INTERNAL_TOKEN: raise BadRequest("Wrong token")
def rate_limited(*args, **kwargs): key = 'rate-limit/%s/%s/' % (key_func(), scope_func()) rlimit = RateLimit(key, limit, per, send_x_headers) g._view_rate_limit = rlimit if rlimit.over_limit: raise BadRequest("You hit the rate limit") return f(*args, **kwargs)
def team_unfavorite(): """Unfavorite user **endpoint** .. sourcecode:: http POST /user/unfavorite **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "message": "Unfavorite team success" } :form team_id: team yang akan di-unfavoritekan """ team_id = request.form.get("team_id") if not team_id: raise BadRequest("team_id tidak boleh kosong") unfavorite = teamfavorite_ctrl.set_unfavorite(auth.user.id, team_id) response = {"status": 200, "message": "Berhasil unfavorite team"} return jsonify(response)
def _auth(*scopes): if not user: raise BadRequest("Missing auth header") for s in scopes: if s not in role_scope[user.role]: raise Forbidden("you don't have access to perform this action")
def standing_list(): """Get list team in standing **endpoint** .. sourcecode:: http GET /standing **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "result": [ { } ] } :query liga: liga yang akan ditampilkan standing listnya """ liga = request.args.get("liga") periode = request.args.get("periode") if not liga: raise BadRequest("Nama liga tidak boleh kosong") if not liga: periode = datetime.today().year standing = standing_ctrl.get(liga=liga, periode=periode) response = { "status": 200 if standing != [] else 204, "result": _entity_standing_list(standing) } return jsonify(response)
def set_unfavorite(actor: int, team_id: int) -> None: """Action unfavorite user Args: actor: actor yang akan melakukan unfavorite team_id: id team yang akan di unfavoritkan """ # check apakah sudah di favoritkan atau belum favorite = TeamFavorites.query.filter( TeamFavorites.user_id == actor, TeamFavorites.team_id == team_id, ).first() if not favorite: raise BadRequest("Team tidak pernah di favorite") # set unfavorite favorite.is_deleted = 1 db.session.add(favorite)
def set_favorite(actor: int, team_id: int) -> TeamFavorites: """Action favorite user Args: actor: actor yang akan melakukan favorite team_id: id team yang akan difavoritkan """ # check sudah difavoritkan apa belum team = TeamFavorites.query.filter( TeamFavorites.user_id == actor, TeamFavorites.team_id == team_id, ).first() if team: raise BadRequest("Team sudah masuk kedalam daftar favorite") # set favorite favorite = TeamFavorites(actor, team_id) db.session.add(favorite) db.session.flush() return favorite
def player_list(): """Get list player **endpoint** .. sourcecode:: http GET /player **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "has_next": false, "has_prev": false, "total": 1, "result": [ { "id": 1, "shortname": Messi, "fullname": Lionel Messi, "backnumber": 10, "height": 165, "weight": 61, "nation": Argentina, "team": { "id": 1, "shortname": FCB, "fullname": Bacelona FC, "liga": La Liga, "stadion": Camp Nou, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., }, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., } ] } :query page: pagination page :query count: count result per page :query team: team yang akan dicari list pemainnya """ page = request.args.get("page", "1") count = request.args.get("count", "12") team_id = request.args.get("team_id") if not team_id: raise BadRequest("Nama team tidak boleh kosong") # type conversion page = int(page) count = int(count) team_id = int(team_id) player = player_ctrl.get_list(page=page, count=count, team_id=team_id) response = { "status": 200 if player.items != [] else 204, "has_next": player.has_next, "has_prev": player.has_prev, "total": player.total, "result": _entity_player_list(player.items) } return jsonify(response)
def player_create(): """Create player **endpoint** .. sourcecode:: http POST /player/create **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "id": 1, "shortname": "Messi", "fullname": "Lionel Messi", "backnumber": 10, "height": 165, "weight": 60, "nation": Argentina, "team_id": 1, "avatar": { "large": "", "medium": "", "small": "", } } :form shortname: nama punggung dari pemain :form fullname: nama lengkap dari pemain :form backnumber: nomor punggung dari pemain :form height: tinggi dari pemain :form weight: berat badan pemain :form nation: kebangsaan dari pemain :form team_id: id team pemain """ shortname = request.form.get("shortname") fullname = request.form.get("fullname") backnumber = request.form.get("backnumber") height = request.form.get("height", "0") weight = request.form.get("weight", "0") nation = request.form.get("nation", "Indonesia") team_id = request.form.get("team_id", "1") player_avatar = request.files.get("player_avatar") if None in (shortname, fullname, backnumber, player_avatar): raise BadRequest( "shortname, fullname, backnumber, player_avatar tidak boleh kosong" ) # type conversion backnumber = int(backnumber) height = int(height) weight = int(weight) team_id = int(team_id) player = player_ctrl.create( shortname=shortname, fullname=fullname, backnumber=backnumber, team_id=team_id, height=height, weight=weight, nation=nation, player_avatar=player_avatar, ) response = { "status": 200, "id": player.id, "shortname": player.shortname, "fullname": player.fullname, "backnumber": player.backnumber, "height": player.height, "weight": player.weight, "nation": player.nation, "team": player.team_id, "avatar": player.avatar_json, } return jsonify(response)
def team_create(): """Create team **endpoint** .. sourcecode:: http POST /team/create **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { status: 200, id: 1, shortname: FCB, fullname: FC Barcelona, liga: La Liga Spanyol, stadion: Camp Nou, website: www.fcbarcelona.com, birthday: DD-MM-YYYY } :form shortname: nama pendek dari team :form fullname: nama lengkap dari team :form liga: nama liga yang diikuti team :form stadion: nama stadion kandang team :form website: website official dari team :form birthday: tanggal club lahir """ shortname = request.form.get("shortname") fullname = request.form.get("fullname") liga = request.form.get("liga") stadion = request.form.get("stadion") website = request.form.get("website") birthday = request.form.get("birthday") if None in (shortname, fullname): raise BadRequest("shortname, fullname tidak boleh kosong") # type conversion liga = int(liga) team = team_ctrl.create( shortname=shortname, fullname=fullname, liga=liga, stadion=stadion, website=website, birthday=birthday ) response = { "status": 200, "id": team.id, "shortname": team.shortname, "fullname": team.fullname, "liga": team.liga, "stadion": team.stadion, "website": team.website, "birthday": team.birthday, "avatar": team.avatar_json, } return jsonify(response)
def team_list(): """Get list team **endpoint** .. sourcecode:: http GET /team **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "has_next": false, "has_prev": false, "total": 1, "result": [ { "id": 1, "shortname": FCB, "fullname": Barcelona FC, "liga": { "id": 1, "name": La Liga, "nation": Spain, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., }, "website": www.barcelona.com, "birhtday": 1555867523, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., } ] } :query page: pagination pag :query count: count result per page :query liga: liga yang dipilih """ page = request.args.get("page", "1") count = request.args.get("count", "12") liga = request.args.get("liga") if not liga: raise BadRequest("id liga tidak boleh kosong") # type conversion page = int(page) count = int(count) liga = int(liga) team = team_ctrl.get_list(page=page, count=count, liga=liga) response = { "status": 200 if team.items != [] else 204, "has_next": team.has_next, "has_prev": team.has_prev, "total": team.total, "result": _entity_team_list(team.items) } return jsonify(response)
def search_player(): """Search player **endpoint** .. sourcecode:: http GET /search/player **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "has_next": false, "has_prev": false, "total": 1, "result": [ { } ] } :query keyword: keyword for search :query next_id: start id player for search :query last_id: last id player for search :query page: pagination pag :query count: count result per page optional: :query sort: sort list player - match: sort by match query - id: sort by id asc - -id: sort by id desc - name: sort by name asc - -name: sort by name desc """ keyword = request.args.get("keyword") page = request.args.get("page", "1") count = request.args.get("count", "12") sort = request.args.get("sort", "match") next_id = request.args.get("next_id") last_id = request.args.get("last_id") # raise BadRequest is missing keyword if not keyword: raise BadRequest("Keyword kosong") if not page.isdigit() or not count.isdigit(): raise BadRequest("page dan count harus integer") if sort and sort not in ("-id", "id", "match", "name", "-name"): raise BadRequest("Sort tidak didukung") # type conversion page = int(page) count = int(count) next_id = int(next_id) last_id = int(last_id) players = search.search_player(keyword, next_id, last_id, page, count, sort) # get players result result = [] response = { "status": 200, "result": result, "has_prev": players.has_prev if players else False, "has_next": players.has_next if players else False, "total": players.total if players else 0 } return jsonify(response)
def search_team(): """Search team **endpoint** .. sourcecode:: http GET /search/team **success response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: text/javascript { "status": 200, "has_next": false, "has_prev": false, "total": 1, "result": [ { "status": 200, "id": 1, "shortname": FCB, "fullname": Barcelona FC, "liga": { "id": 1, "name": La Liga, "nation": Spain, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., }, "website": www.barcelona.com, "birthday": 1555769584, "image": asset.soccer-app..., "image_icon": asset.soccer-app..., "image_thumb": asset.soccer-app..., } ] } :query keyword: keyword for search :query next_id: start id team for search :query last_id: last_id team fro search :query page: pagination page :query count: count result per page optional: :query sort: sort list team - match: sort by match query - id: sort id by id asc - -id: sort id by id desc - name: sort by name asc - -name: sort by name desc """ keyword = request.args.get("keyword") page = request.args.get("page", "1") count = request.args.get("count", "12") sort = request.args.get("sort", "match") next_id = request.args.get("next_id", "0") last_id = request.args.get("last_id", "0") # raise BadRequest is missing keyword if not keyword: raise BadRequest("Keyword kosong") if not page.isdigit() or not count.isdigit(): raise BadRequest("page dan count harus integer") if sort and sort not in ("-id", "id", "match", "-name", "name"): raise BadRequest("Sort tidak didukung") # type conversion page = int(page) count = int(count) next_id = int(next_id) last_id = int(last_id) teams = search.search_team(keyword, next_id, last_id, page, count, sort) # get teams result result = [] if teams != None: for team in teams.items: result.append({ "id": team.id, "shortname": team.shortname, "fullname": team.fullname, "liga": { "id": team.liga.id, "name": team.liga.name, "nation": team.liga.nation, "image": team.liga.image_url, "image_icon": team.liga.image_icon_url, "image_thumb": team.liga.image_thumb_url, }, "website": team.website, "birthday": team.birthday, "image": team.image_url, "image_icon": team.image_icon_url, "image_thumb": team.image_thumb_url, }) response = { "status": 200, "result": result, "has_prev": teams.has_prev if teams else False, "has_next": teams.has_next if teams else False, "total": teams.total if teams else 0 } return jsonify(response)