Skip to main content

frest

How to create a REST API very quickly.

Installation

Install frest by pip package (you must have Python>=3.6). In this example I'm using virtualev, a tool for virtual environment creation.

$ virtualenv env
$ source env/bin/activate
(env) $ pip install frest

Use

Create a project

(env) $ frest --init project                                                                                                                                                

   __               _    
  / _|             | |   
 | |_ _ __ ___  ___| |_ 
 |  _| '__/ _ \/ __| __| 
 | | | | |  __/\__ \ |_  
 |_| |_|  \___||___/\__|



Create project/... OK
Create project/__init__.py... OK

It will create a project tree folder like this one:

  • database.py A file with DATABASE_URI, database path, default is sqlite:///database.db.
  • mail.py A file with mail configuration: server, port, password etc.
  • app.py A file with routes and mail/database configuration.
  • wsgi.py File used for production.

Create first app

Inside project folder, we start to create a new app. For example, we want to store books inside our database.

(env) $ cd project/
(env) $ frest --startapp book

It will print something like this:

   __               _    
  / _|             | |   
 | |_ _ __ ___  ___| |_ 
 |  _| '__/ _ \/ __| __| 
 | | | | |  __/\__ \ |_  
 |_| |_|  \___||___/\__|



Create scheme/... OK
Create scheme/book... OK
Create scheme/book/__init__.py... OK
Create model for book...
Fields id, created_at, updated_at are default on every new model, you can delete it from model file

Create field? (Y/n): 

Now we have to define a model for our book, using 6 data types: integer, string, text, datetime, float, bool. This is an example of our book with name and pubblication date:

Choose field name: name
Choose field type: int, str, text, datetime, float, bool str
Choose string size: 20
Field is nullable? (Y/n): n
Create new field? (Y/n): 
Choose field name: pubblication_date
Choose field type: int, str, text, datetime, float, bool datetime
Field is nullable? (Y/n): n
Create new field? (Y/n): n
Create scheme/book/models.py... OK
Create scheme/book/forms.py... OK
Create scheme/book/routes.py... OK

Now, our book it will be inside scheme/book.

Move scheme routes inside app

Edit project/app.py and put your routes inside.

[...]
from frest.auth.routes import api as api_users
from scheme.book.routes import api as api_books
[...]
app.register_blueprint(api_books)
[...]

Database creation

Open python interpreter, import db and create.

from app import db
db.create_all()

Now exit and check your scheme inside database. It will be something like this.

CREATE TABLE user (
	"userId" INTEGER NOT NULL, 
	email VARCHAR(30), 
	password VARCHAR(30), 
	is_admin BOOLEAN, 
	name VARCHAR(30), 
	created_at DATETIME, 
	PRIMARY KEY ("userId"), 
	CHECK (is_admin IN (0, 1))
);
CREATE TABLE book (
	"bookId" INTEGER NOT NULL, 
	name VARCHAR(20) NOT NULL, 
	pubblication_date DATETIME NOT NULL, 
	created_at DATETIME, 
	updated_at DATETIME, 
	PRIMARY KEY ("bookId")
);
CREATE TABLE token (
	"tokenId" INTEGER NOT NULL, 
	string VARCHAR(20), 
	created_at DATETIME, 
	expired BOOLEAN, 
	user_id INTEGER NOT NULL, 
	PRIMARY KEY ("tokenId"), 
	CHECK (expired IN (0, 1)), 
	FOREIGN KEY(user_id) REFERENCES user ("userId")
);

Start app

Inside project file you have to run python app.py and it's done.

TODO

  • Migrations of database in case of edits
  • Put automatically routes inside app file
  • More datatype
  • Relantionships (1-to-1, 1-to-n, n-to-n)

Metadata

Release files for frest 0.1.5.post2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for frest 0.1.5.post2
File Size Uploaded Provenance Actions
frest-0.1.5.post2.tar.gz 12.4 kB Without provenance Details for frest-0.1.5.post2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for frest 0.1.5.post2
File Interpreter ABI Platform Actions
frest-0.1.5.post2-py3-none-any.whl Python 3 none any Details for frest-0.1.5.post2-py3-none-any.whl

Total release size: 28.0 kB

Release files / frest-0.1.5.post2.tar.gz

Download URL frest-0.1.5.post2.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5c02b8f31ac8f4246b83fcc766d4c704b9f32fa36cd4841663cd4b8c88530cef
BLAKE2b-256 checksum
How to use checksums
a982a891442a66391ac869324d32629afed3f2e9de5bd5a6e2cac7c9b6b80f1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / frest-0.1.5.post2-py3-none-any.whl

Download URL frest-0.1.5.post2-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0101c4dfda9723c226d8dd2df63a7d732a0064d9c5b94e500b24507393db71bc
BLAKE2b-256 checksum
How to use checksums
d2f3ba043411499c1f4c9e2f14af766c3a0b37953ac5d6e6d4278beaedddb138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Security for frest 0.1.5.post2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page