Categories
Angular MEA Stack Python

Basic Angular 10 Crud Generator, Powered by Python EVE – Part 1

In this tutorial we’ll build the REST API using Python EVE framework, learn how to use Cerberus validation system to add support for new schema fields and expose the schema endpoint.

In this tutorial we’ll build the REST API using Python EVE framework, learn how to use Cerberus validation system to add support for new schema fields and expose the schema endpoint to be used for the Angular 10 CRUD Generator

Source Code on GitHub

From the official docs:

Eve is an open source Python REST API framework designed for human beings. It allows to effortlessly build and deploy highly customizable, fully featured RESTful Web Services.

Eve is powered by Flask and Cerberus and it offers native support for MongoDB data stores.

Prerequisites:

  • Python 3 — download
  • MongoDB — download
  • I will be using Visual Studio Code — download, but you are welcome to use anything you like
  • Postman — download
  • We’ll be using pip to install EVE Framework:
pip install eve

Creating the Basic API

Create a project folder, and inside create a file run.py.

Inside run.py import EVE, create an EVE object and run app when the program is executed as a main program:

If you only installed the binaries and did not install MongoDB as a service, you must manually start the MongoDB instance. — check the official docs for your operating system

Along with an instance of MongoDB running, EVE requires a configuration file. In the project folder create another file, settings.py.

Inside settings.py weneed to add info about the API resources and MongoDB connections:

MONGO_HOST and MONGO_PORT are optional as they default to a local ‘mongod’ instance

MONGO_USERNAME, MONGO_PASSWORD and MONGO_AUTH_SOURCE are needed only if your database has authentication enabled (It should always have authentication enabled)
MONGO_AUTH_SOURCE is the name of the database on which the user can be authenticated

Now that we are connected let’s create some resources endpoints and define their schema.

Let’s create two basic resources, cats and dogs, with data validation:

So far our API is read only, to enable the full spectrum of CRUD operations we need to enable methods at resource and item level:

Test the API

Save both files and run the program:

python run.py

We can test our API using Postman, by default our API is at localhost, port 5000

Let’s send a GET request on http://localhost:5000

GET request without a specific API resource

GET request without a specific API resource

In settings.py, we have defined the key fields that are needed to create a new item on each resource endpoint, let’s test it for dogs resource API:

POST request on dogs API resource
POST request on dogs API resource

If we try to update an item right now we will get the following error:

PATCH request on dogs API resource item — Error 428
PATCH request on dogs API resource item — Error 428

In order to call a DELETE, PUT or PUSH request on a item endpoint, we also need to provide an _etag related to that particular item. We will disable this requirement for now by adding this line in settings.py:

IF_MATCH = False

Add extra fields for CRUD Generator

Now that we have a usable API, let’s extend the field validation schema with three extra fields for the CRUD generator.

Inside run.py import Validator from eve.io.mongo

By subclassing the base Mongo validator class and then adding a custom _validate_<rulename> method, you extended the available Schema Definition grammar. Let’s add methods for Create, Read, Update, Delete, Detail, Input and Icon. Input and Icon will be used in the next chapters to generate the Create and Edit forms:

The next step is to pass the new validator to the Eve Object and here is the final run.py file:

Let’s update our cats and dogs resources with the newly created fields. In settings.py add the newly created fields to each key field validation:

Expose the schema endpoint for the CRUD Generator

Last step we need to disable pagination and expose the schema endpoint:

Let’s test our form API resource by sending a GET request on http://localhost:5000/form

Angular 10 CRUD Generator - GET request on form API resource
GET request on form API resource

Conclusion

In this tutorial, we saw how to create API’s using Python EVE Framework.
We extended the available Schema Definition grammar and exposed the schema endpoint which we’ll use in the next part for our Angular 10 CRUD generator.

Buy me a coffee if this helped you

By rockaru

Self-taught web professional

I started in school, continued as a freelancer and now I’m a self-taught web professional with more than 10 years of experience in the field

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.