Skip to main content

A package to accelerate REST API development

Project description

NovaAPI

NovaAPI is a python package to accelerate REST API development.

Status

Version Python Package Quality Gate Security Rating Reliability Maintainability Coverage Bugs Code Smells PyPI - License

Getting Started

To install NovaAPI and its dependencies, you can use pip as follows:

pip install NovaAPI

If you are interested in developing the package itself, scroll down to Installing for Development

First example

As our first example, we'll create a contact API which could be used to create an address book. We need to create two files: Contact.py and ContactDAO.py, shown below.

Contact.py

In this file, we describe our entity, that is, our contact. We include all information we want to add to the database. Beside our custom defined fields, as it inherits from Entity we also have an UUID, the creation datetime and the last modified datetime automatically generated.

In this example, we use the "type" key in the metadata to gain control over how our data is going to be stored in the database. As our attributes are strings, they would be VARCHAR(100) if no type was informed.

from dataclasses import dataclass, field

from nova_api.entity import Entity

@dataclass
class Contact(Entity):
    first_name: str = field(default=None, metadata={"type":"VARCHAR(45)"})
    last_name: str = ''
    telephone_number: str = field(default=None, metadata={"type":"VARCHAR(15)"})
    email: str = field(default=None, metadata={"type":"VARCHAR(255)"})
ContactDAO.py

In out ContactDAO, we only need to inherit from GenericSQLDAO and assign our return_class in the init method

from nova_api.generic_dao import GenericSQLDAO

from Contact import Contact


class ContactDAO(GenericSQLDAO):
    def __init__(self, database=None):
        super(ContactDAO, self).__init__(database=database,
                                         return_class=Contact)

Starting a server for this example locally

If we want to run this example through a local flask server, we can use the following server.py file. It will generate the api files and start the server at port 8080. You also need a database running at localhost with a root user with password root.

server.py
import connexion
from flask_cors import CORS
from os import environ
from nova_api import create_api_files


debug = False
port = 8080
entity = "Contact"
dao_class = entity + 'DAO'
version = "1"

# Import entity and dao
mod = __import__(dao_class, fromlist=[dao_class])
entity_dao = getattr(mod, dao_class)
mod = __import__(entity, fromlist=[entity])
entity_class = getattr(mod, entity)

# Generate api documentation and implementation
create_api_files(entity_class, entity_dao, version)

# Create the table in the database
dao = entity_dao()
dao.create_table_if_not_exists()

# Create the application instance
app = connexion.App(__name__, specification_dir=".")
CORS(app.app)

# Add the api to the flask server
app.add_api(entity.lower() + "_api.yml")
print("Done adding api for {ent}".format(ent=entity))

if __name__ == '__main__':
    app.run(debug=debug, port=port)

After creating the server.py file, we can start it with the following command:

$ python server.py
...
Done adding api for Contact
 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Next, we can navigate to localhost:8080/v1/contact/ui to see the live swagger documentation as shown below:

Swagger Documentation

And you're all set to start using your contact api!

Installing for Development

If you want to contribute, clone this repo and navigate to its folder, it can be done with the following command:

git clone https://github.com/novaweb-mobi/nova-api && cd nova-api

Next, install the dependencies with pip:

pip install -r requirements.txt

And you're all set! You can move on to testing with the analyze script available at analyze. To execute it on Linux, just run:

$ chmod u+x analyze && ./analyze

Automatic Tests

The unit tests are available at tests. Before running them, you must have pytest, pytest-mock and pytest-order installed.

Executing tests

The best way to run the test is with the analyze script available at the root of the project. To run them, execute the following commands:

pip install -r requirements.txt
chmod u+x analyze
./analyze

Deploy

When deploying to production, you should always use a production grade server(that's not flask) like uWSGI or NGINX. If you use Kubernetes or docker in your production environment, you can use our docker image available at this repo.

You can create your Dockerfile as the following example, with sets up an API for User:

FROM docker.pkg.github.com/novaweb-mobi/connexion-api-docker/novaapi:0.1.0
COPY User.py .
COPY UserDAO.py .
ENV PORT 8080
ENV ENTITIES User
ENV DB_URL 172.18.0.2
ENV DB_USER root
ENV DB_PASSWORD root
ENV DB_NAME default

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and our PR submission policy.

Versioning

We use SemVer for versioning. For available versions, check thr tags on this repo.

Authors

  • Fábio Trevizolo - Initial Work - FabioTS
  • Mateus Berardo - Initial Work - MatTerra

Also check the list of contributors.

License

This project is licensed under a MIT license - check the LICENSE file for details.

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

NovaAPI-0.2.0a4.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

NovaAPI-0.2.0a4-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file NovaAPI-0.2.0a4.tar.gz.

File metadata

  • Download URL: NovaAPI-0.2.0a4.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for NovaAPI-0.2.0a4.tar.gz
Algorithm Hash digest
SHA256 6c456bc0a0f89f2b0c0d0dbf74f5d8ecfc1a0d29a164056f840591befc106aea
MD5 eb8f6600903b889496151d04df5bde5d
BLAKE2b-256 d6b19e2b8f115da4866367b9ab9afb33c8a0c811137eca772e42aeeab0209a83

See more details on using hashes here.

File details

Details for the file NovaAPI-0.2.0a4-py3-none-any.whl.

File metadata

  • Download URL: NovaAPI-0.2.0a4-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for NovaAPI-0.2.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c02c043d9ca099a124799c30e700adf2ed1fd67b1bcac0b6dcf6f0b86ba0739
MD5 30f2131faa0928e85050f431d451259d
BLAKE2b-256 8c783492bb3a2adc1404e0e327435d730b394b85117bd40d36a6db5939f22845

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page