Coder Social home page Coder Social logo

a-luna / flask-api-tutorial Goto Github PK

View Code? Open in Web Editor NEW
71.0 71.0 18.0 41 KB

Boilerplate for a Flask REST API with JWT-based authentication, built with SQLAlchemy, Flask-RESTx, PyJWT, and pytest. This is a companion repo for a multi-part tutorial series on my personal website.

Home Page: https://aaronluna.dev/series/flask-api-tutorial/overview/

Python 99.34% Mako 0.66%

flask-api-tutorial's Introduction

a-luna's github stats

๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป What Type of Code Did I Write This Week?

From: 12 May 2024 - To: 19 May 2024

Svelte       5 hrs 11 mins   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–“โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘   42.99 %
Python       3 hrs 34 mins   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–’โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘   29.62 %
TypeScript   2 hrs 20 mins   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘   19.42 %
CSS          25 mins         โ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘   03.50 %
TOML         14 mins         โ–’โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘   01.93 %

Coding metrics are powered by Wakatime

  • Add Search to Your Static Site with Lunr.js (Hugo, Vanilla JS) Jun 30 2020 โ€” I decided to document how I implemented a search feature with Hugo and Lunr.js on my personal blog site. Since this is a static site the search functionality is performed entirely within the client's browser. My solution uses vanilla JS DOM manipulation to render the search results. I believe that my approach includes features that are markedly different from the implementations I encountered while researching this task, features which enhance the overall search UX.

  • An Introduction to Decorators in Python Feb 27 2020 โ€” Decorators can be a daunting topic when first encountered. While the Zen of Python states "There should be one-- and preferably only one --obvious way to do it", there are many, equally valid ways to implement the same decorator. These different methods can be categorized as either function-based, class-based, or a hybrid of both. In this post I will explain the design and behavior of Python decorators and provide examples of decorators that I frequently use in my own code.

  • Hugo: Add Copy-to-Clipboard Button to Code Blocks with Vanilla JS Nov 13 2019 โ€” Hugo includes a built-in syntax-highlighter called Chroma. Chroma is extremely fast since it is written in pure Go (like Hugo) and supports every language I can think of. Chroma's speed is especially important since syntax highlighters are notorious for causing slow page loads. However, it lacks one vital feature โ€” an easy way to copy a code block to the clipboard. I decided to document my implementation using only vanilla JS since every blog post I found for this issue relied on jquery to parse the DOM, which is completely unnecessary at this point.

flask-api-tutorial's People

Contributors

a-luna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

flask-api-tutorial's Issues

when running 'tox' or 'tox -e py37'

Get some error:
143, in exec_module
exec(co, module.dict)
File "/home/gofurmzq/Documents/flask-api-with-jwt/venv/lib/python3.7/site-packages/pytest_flask/plugin.py", line 11, in
from flask import json
File "/home/gofurmzq/Documents/flask-api-with-jwt/venv/lib/python3.7/site-packages/flask/init.py", line 14, in
from jinja2 import escape
File "/home/gofurmzq/Documents/flask-api-with-jwt/venv/lib/python3.7/site-packages/jinja2/init.py", line 6, in
from markupsafe import escape
ModuleNotFoundError: No module named @'markupsafe'
ERROR: InvocationError for command /home/gofurmzq/Documents/flask-api-with-jwt/.tox/py37/bin/pytest (exited with code 1)
_________________ summary __________________
ERROR: py37: commands failed

Thanks you i hope you can suggest the solved issue @a-luna ๐Ÿ˜…

Add namespace only if config value is set?

Is there a way to load a namespace only if some variable is set? For example I tried modifying flask-api-tutorial/src/flask_api_tutorial/api/init.py like this:

from flask import Blueprint, current_app
from flask_restx import Api

from flask_api_tutorial.api.auth.endpoints import auth_ns

...

api.add_namespace(auth_ns, path="/auth")

if current_app.config["ENV"] == "development":
    from flask_api_tutorial.api.widgets.endpoints import widget_ns

    api.add_namespace(widget_ns, path="/widgets")

But get this error

Traceback (most recent call last):
  File "C:\Users\memjr\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\memjr\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 425, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\run.py", line 11, in <module>
    app = create_app(os.getenv("FLASK_ENV", "development"))
  File "c:\users\memjr\pycharmprojects\flask-api-tutorial\src\flask_api_tutorial\__init__.py", line 20, in create_app
    from flask_api_tutorial.api import api_bp
  File "c:\users\memjr\pycharmprojects\flask-api-tutorial\src\flask_api_tutorial\api\__init__.py", line 20, in <module>
    if current_app.config["ENV"] == "development":
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\werkzeug\local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\werkzeug\local.py", line 307, in _get_current_object
    return self.__local()
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\globals.py", line 52, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

Edit: formatted code blocks.

pip install -e .[dev] fails in zsh

I am working through this excellent tutorial.

At this step, pip install -e .[dev] failed for me on macOS in zsh. Error message: zsh: no matches found: .[dev].

This issue explains the problem, and their suggested solution resolved this for me. Suggested to use pip install -e .'[dev]'

Perhaps this is obvious to more advanced developers, but I thought it would be helpful to future visitors to this tutorial to update this step.

Thank you.

Cannot pass test_auth_user_no_token, test_auth_user_expired_token, test_logout_token_blacklisted

All tests uses HTTPStatus.UNAUTHORIZED that uses the decorators.py and exceptions.py where theres an extension of werkzeug.exception Unauthorized and Forbidenn methods, that call a function called get_headers() that according to the test traceback call is getting 3 variables instead of 2.

ERROR flask-api:app.py:1742 Exception on /api/v1/auth/user [GET]
Traceback (most recent call last):
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/api.py", line 404, in wrapper
resp = resource(*args, **kwargs)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask/views.py", line 107, in view
return current_app.ensure_sync(self.dispatch_request)(**kwargs)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/resource.py", line 46, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/marshalling.py", line 244, in wrapper
resp = f(*args, **kwargs)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_api/api/auth/endpoints.py", line 65, in get
return get_logged_in_user()
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_api/api/auth/decorators.py", line 15, in decorated
token_payload = _check_access_token(admin_only=False)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_api/api/auth/decorators.py", line 48, in _check_access_token
error_description=result.error,
flask_api.api.exceptions.ApiUnauthorized: 401 Unauthorized: Access token expired. Please log in again.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask/app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/api.py", line 673, in error_router
return original_handler(f)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/api.py", line 671, in error_router
return self.handle_error(e)
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/flask_restx/api.py", line 721, in handle_error
headers = e.get_response().headers
File "/home/raul/Desktop/flask_api/.tox/py37/lib/python3.7/site-packages/werkzeug/exceptions.py", line 150, in get_response
headers = self.get_headers(environ, scope)
TypeError: get_headers() takes 2 positional arguments but 3 were given

Can you please share the requirements.txt or find a work around for this problem?
Really learned a lot, but can't continue if no solution here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.