5
\$\begingroup\$

Here is a rather large endpoint that I have written in Python using Flask and SQLAlchemy, I was wondering if there are any tips I could get.

The endpoint takes a post request which contains specific user information and then passes some of that information to a function that calculates a percent and then it places the information into a database.

@app.route('/api/pineapple/create_investment', methods=['POST'])
@cross_origin()
def create_investment():
    auth_token = request.headers['Authorization']
    bearer, _, token = auth_token.partition(' ')
    if bearer != 'Bearer':
        raise ValueError('Invalid Access Token')
    else:

        percentage_of_fund = equity_calculator(request.json['amount_invested'],
                                               request.json['size_of_fund'],
                                               request.json['total_equity_offered'])

        new_investment = Investment(user_id=request.json['email'],
                                    company_name=request.json['company_name'],
                                    company_id=request.json['company_id'],
                                    amount_invested=request.json['amount_invested'],
                                    percentage_of_fund=percentage_of_fund)

        db.session.add(new_investment)
        investment_schema = InvestmentSchema()
        try:
            db.session.commit()
            return investment_schema.dump(new_investment), 200
        except exc.SQLAlchemyError as e:
            return jsonify(error=502, text=str(e))
\$\endgroup\$

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.