I've recently started learning python to gain some fullstack skills and am working on a backend for a webapp as a learning project. Before I get too far down this path I was wondering if someone wouldn't mind taking a look through my first few routes to let me know if I'm going about things in a totally wrong way or not.

Here's the project repository: https://github.com/bringel/destiny-mod-assistant-server

My goal is to make a flask api that can interact with the Destiny 2 API and return the results to a webapp for display. I'm storing my data in a Postgres database, but wanted to stay away from the full SQLAlchemy ORM for this project so I could get comfortable without too many layers.

import os

from flask import Flask, redirect, request, session
from flask.json import jsonify
from flask_session import Session

from api_server.database import db
from api_server.destiny_api import DestinyAPI
from api_server.models import User
from api_server.repositories.user_repository import UserRepository

def create_app():

    app = Flask(__name__)

    app.secret_key = os.environ.get("SECRET_KEY")
    app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL")
    app.config["SESSION_TYPE"] = "sqlalchemy"
    app.config["SESSION_SQLALCHEMY"] = db

    def login():
        destiny_api = DestinyAPI()
        authorization_url, state = destiny_api.get_authorization_url()

        session["oauth_state"] = state

        return redirect(authorization_url)

    def callback():
        destiny_api = DestinyAPI()
        token = destiny_api.fetch_token(request.url)

        session["oauth_token"] = token

        user = destiny_api.get_bungie_user_linked_profiles()

        session["destiny_membership_type"] = user.destiny_membership_type
        session["destiny_membership_id"] = user.destiny_membership_id

        user_repository = UserRepository()
        existing_user = user_repository.get_user(
            user.destiny_membership_type, user.destiny_membership_id

        if existing_user is None:

        return redirect(os.environ.get("APP_URL"))

    def get_user():
        user_repository = UserRepository()

        membership_type = session["destiny_membership_type"]
        membership_id = session["destiny_membership_id"]

        user = user_repository.get_user(membership_type, membership_id)

        return jsonify(user)

    return app
  • \$\begingroup\$ wanted to stay away from the full SQLAlchemy ORM - so then why is your session type sqlalchemy? \$\endgroup\$
    – Reinderien
    Dec 23 '21 at 3:23
  • \$\begingroup\$ @Reinderien the server sessions library I was trying to use requires using the sqlalchemy orm. In the repository you can see that I'm using sqlalchemy core APIs in my app since I already had to add it as a dependency for the sessions \$\endgroup\$
    – bringel
    Dec 23 '21 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.