Skip to content

Commit

Permalink
fix all pylint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Firebird1029 committed Mar 16, 2024
1 parent fe392ce commit 9fe6161
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
6 changes: 4 additions & 2 deletions backend/db.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"""Supabase code for Flask app. From https://supabase.com/blog/oauth2-login-python-flask-apps"""

import os
from supabase import Client
from dotenv import load_dotenv
from flask import g
from werkzeug.local import LocalProxy
from supabase.client import Client, ClientOptions
from flask_storage import FlaskSessionStorage


# load environment variables from .env file
load_dotenv()

url: str = os.getenv("SUPABASE_URL")
key: str = os.getenv("SUPABASE_KEY")


def get_supabase() -> Client:
"""Supabase code for Flask app. From https://supabase.com/blog/oauth2-login-python-flask-apps"""
if "supabase" not in g:
g.supabase = Client(
url,
Expand Down
5 changes: 5 additions & 0 deletions backend/flask_storage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Supabase code for Flask app. From https://supabase.com/blog/oauth2-login-python-flask-apps"""

from gotrue import SyncSupportedStorage
from flask import session


class FlaskSessionStorage(SyncSupportedStorage):
"""Supabase code for Flask app. From https://supabase.com/blog/oauth2-login-python-flask-apps"""

def __init__(self):
self.storage = session

def get_item(self, key: str) -> str | None:
if key in self.storage:
return self.storage[key]
return None

def set_item(self, key: str, value: str) -> None:
self.storage[key] = value
Expand Down
31 changes: 26 additions & 5 deletions backend/server.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
from flask import Flask, request, json
from db import supabase
"""Main server code for the backend API."""

# don't lint TODOs
# pylint: disable=fixme

from flask import Flask, request
from gotrue.errors import AuthApiError
from flask_restful import Resource, Api, abort
from postgrest.exceptions import APIError
from db import supabase

# Setup Flask app and API
# TODO setup CORS
app = Flask(__name__)
app.secret_key = "super secret key" # TODO change key, move to separate file
app.config["SESSION_TYPE"] = "filesystem" # TODO change to something else in future
api = Api(app)


class Ping(Resource):
"""API route to check if the server is running."""

def get(self):
"""Return pong for a successful ping."""
return "pong"


api.add_resource(Ping, "/api/ping")


def check_auth(req, user_id):
"""Check if the user is authorized to perform the action."""
session = req["session"]["data"]["session"]
access_token = session["access_token"]
refresh_token = session["refresh_token"]
Expand All @@ -33,27 +44,34 @@ def check_auth(req, user_id):
if user.id != user_id:
abort(403, message="You are not authorized to perform this action.")
except AuthApiError as e:
return e.message, 403
abort(403, message=e.message)


class NewGame(Resource):
"""New Game"""

def post(self):
"""Create a new game."""
req = request.get_json()
check_auth(req, req["user_id"])
name, gameType, details = req["name"], req["type"], req["details"]

try:
name, game_type, details = req["name"], req["type"], req["details"]

# insert row into games table in Supabase
res = (
supabase.table("games")
.insert(
{
"owner": req["user_id"],
"name": name,
"type": gameType,
"type": game_type,
"details": details,
}
)
.execute()
)

return {"success": True, "data": res.data}
except APIError as e:
return {
Expand All @@ -66,7 +84,10 @@ def post(self):


class Game(Resource):
"""Existing Game"""

def get(self, url_tag):
"""Get a game by its URL tag."""
try:
res = supabase.table("games").select("*").eq("url_tag", url_tag).execute()
return {"success": True, "data": res.data, "count": len(res.data)}
Expand Down

0 comments on commit 9fe6161

Please sign in to comment.