Giter Club home page Giter Club logo

people-api's Introduction

people-api

Hello ๐Ÿ‘‹๐Ÿป,

This is a REST API with that simulates a simple people record database. Post following the setup instructions, you may want to play around with the API's in either swagger or with the provided postman collections in /postman folder. Enjoy and happy testing.

Tech stack

Uses Flask, Connexion, Swagger and SQL Alchemy

Setup

  • Ensure you have pipenv available. Read this blog to understand all about pipenv
  • Ensure you have cloned this repo and are in the project root directory that has the Pipfile with definitions of all required dependencies.
  • Execute pipenv shell to activate the virtualenv in your terminal
  • Execute pipenv install to install all dependencies
  • cd to people-api
  • Execute python server.py
  • To open swagger navigate to http://0.0.0.0:5000/api/ui/

On a windows machine, you may want to replace the host as 127.0.0.1 in above URL

Project structure

  • server.py has the code to start the Flask app with connexion
  • people.py has the implementation for CRUD operations of the people API
  • swagger.yml has the swagger spec to define the route for the API while also allowing to build out a nice swagger documentation
  • static dir contains the css and js files (following MVC) which define the presentation and the interactions with the web apps API

Common Gotchas

  • To install swagger-ui, please run pipenv install "connexion[swagger-ui]". Read this bug to understand about why zsh needs this to be quoted.
  • If you are on windows platform,
    • Try using 127.0.0.1 as the host instead of 0.0.0.0
    • Also enclose the URL in CURL with double quotes instead of single quotes (Reference thread on stack overflow)

Reference

This follows the steps from a real python tutorial series written by Doug Farrell

Further read

Some useful posts to refer apart from the tutorials

people-api's People

Contributors

automationhacks avatar blulady avatar dependabot[bot] avatar gville 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

Watchers

 avatar

people-api's Issues

Issue with pytest connection to API server

Hi,
I am able to run the server successfully after
python server.py

But when I go to run the first test against the API (test_read_all_has_kent()), I am receiving the HTTPConnection error below that I am unable to resolve.

`self = <urllib3.connection.HTTPConnection object at 0x000002240F35E4F0>

def _new_conn(self):
    """Establish a socket connection and set nodelay settings on it.

    :return: New socket connection.
    """
    extra_kw = {}
    if self.source_address:
        extra_kw["source_address"] = self.source_address

    if self.socket_options:
        extra_kw["socket_options"] = self.socket_options

    try:
            conn = connection.create_connection(
            (self._dns_host, self.port), self.timeout, **extra_kw
        )`

..\venv2\lib\site-packages\urllib3\connection.py:169:

This also occurs of any other tests I try to run with pytest.

RuntimeError: Working outside of application context.

Hi,
I just cloned the codebase and completed the setup process.
I also updated all the dependencies to the latest versions.
when running the python build_database.py command i am getting below error

Traceback (most recent call last):
File "build_database.py", line 15, in
db.create_all()
File "/Users/manubharadwajhs/PycharmProjects/virtual_environments/people-api_2/lib/python3.8/site-packages/flask_sqlalchemy/extension.py", line 868, in create_all
self._call_for_binds(bind_key, "create_all")
File "/Users/manubharadwajhs/PycharmProjects/virtual_environments/people-api_2/lib/python3.8/site-packages/flask_sqlalchemy/extension.py", line 839, in _call_for_binds
engine = self.engines[key]
File "/Users/manubharadwajhs/PycharmProjects/virtual_environments/people-api_2/lib/python3.8/site-packages/flask_sqlalchemy/extension.py", line 628, in engines
app = current_app._get_current_object() # type: ignore[attr-defined]
File "/Users/manubharadwajhs/PycharmProjects/virtual_environments/people-api_2/lib/python3.8/site-packages/werkzeug/local.py", line 513, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.

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

Attribute Error when trying to build the database

Hi,
When I am trying to run python build_database.py it is giving the following error:

Traceback (most recent call last): File "build_database.py", line 15, in <module> db.create_all() File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1039, in create_all self._execute_for_all_tables(app, bind, 'create_all') File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1031, in _execute_for_all_tables op(bind=self.get_engine(app, bind), **extra) File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine return connector.get_engine() File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 555, in get_engine options = self.get_options(sa_url, echo) File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 570, in get_options self._sa.apply_driver_hacks(self._app, sa_url, options) File "/home/arijit/.local/share/virtualenvs/people-api-TjJHHjrg/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 914, in apply_driver_hacks sa_url.database = os.path.join(app.root_path, sa_url.database) AttributeError: can't set attribute

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: person

On executing following commnad:

โฏ python3 server.py

Do we need different steps to setup DB?
Thanks.

127.0.0.1 - - [24/May/2021 14:10:39] "GET //api/people HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1770, in _execute_context
    self.dialect.do_execute(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: person

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/connexion/decorators/decorator.py", line 48, in wrapper
    response = function(request)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/connexion/decorators/uri_parsing.py", line 144, in wrapper
    response = function(request)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/connexion/decorators/parameter.py", line 121, in wrapper
    return function(**kwargs)
  File "/Users/user/projects/API/python_testautomationu.io/people-api/people.py", line 39, in read_all
    people = Person.query.order_by(Person.lname).all()
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2699, in all
    return self._iter().all()
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2834, in _iter
    result = self.session.execute(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1677, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1582, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 329, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1451, in _execute_clauseelement
    ret = self._execute_context(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1813, in _execute_context
    self._handle_dbapi_exception(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1994, in _handle_dbapi_exception
    util.raise_(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1770, in _execute_context
    self.dialect.do_execute(
  File "/Users/user/.virtualenvs/api/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: person
[SQL: SELECT person.person_id AS person_person_id, person.lname AS person_lname, person.fname AS person_fname, person.timestamp AS person_timestamp
FROM person ORDER BY person.lname]
(Background on this error at: http://sqlalche.me/e/14/e3q8)

Windows pycharm not create the database

When run the command
python .\build_database.py

the final result
ma = Marshmallow(app)
File "C:\Users\Yuliana_Saavedra.virtualenvs\people-api-H27KOUgq\lib\site-packages\flask_marshmallow_init_.py", line 103, in init
self.init_app(app)
File "C:\Users\Yuliana_Saavedra.virtualenvs\people-api-H27KOUgq\lib\site-packages\flask_marshmallow_init_.py", line 116, in init_app
self.SQLAlchemySchema.OPTIONS_CLASS.session = db.session
AttributeError: '_SQLAlchemyState' object has no attribute 'session'

image

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.