01- A Smooth Start

This module will contain what we will build, the projects structure, and installing everything we need to get started

What We Will Build

In this GitBook, we are going to be building a simple API that allows us to store user data, and perform CRUD functions on blogs. Let's define our model objects before we get started

A Blog Post will contain the following

  • ID - This will be the unique identifier for each post

  • Owner ID - The ID of the user that created it. This will be a foreign key linked to the User object below

  • Title - The title of the blog post

  • Content - The content and body of the Blog Post, what makes the post a blog

  • Created At - When the Post was created

  • Modified At - When the Blog Post is edited, this will be changed to the time of the edit

A User will contain the following

  • ID - The unique identifier to distinguish between different users

  • Name - The username associated with the User

  • Email - The email address the User will use to login with. These will be unique

  • Password - The users password. Obviously we will encrypt these before storing them in a database. The User will use this to authenticate on our API, and recieve a Token. More on this later

  • Created At - What date and time the User registered

  • Modified At - The last time at which the User changed any information associated with their account

  • Blogposts - All posts that the User created. The User will be able to edit and delete only these posts

Now that we have defined our models, lets plan out our endpoints. We know that we want to perform CRUD functions on both of our models so that's a good start. Also, because we mentioned authentication before, we will also include those endpoints for registering and logging in. The prefix (GET, POST, DELETE, PUT) before each url, is going to be the type of request to allow on our endpoints. This is just for safety so we don't accidentally send a request to the endpoint that is supposed to give us a users information

  • POST /api/v1/users - create a user(CREATE)

  • GET /api/v1/users - get all registered users(READ)

  • GET /api/v1/users/<user_id> - get a user(READ)

  • GET /api/v1/users/me - get my info(READ)

  • PUT /api/v1/users/me - update my account(UPDATE)

  • DELETE /api/v1/users/me - Delete my account(DELETE)

  • POST /api/v1/blogs - Create a blog post(CREATE)

  • GET /api/v1/blogs - Get all blog post(READ)

  • GET /api/v1/blogs/<blog_id> - Get a single blog post(READ)

  • PUT /api/v1/blogs/<blog_id> - Update a blog post(UPDATE)

  • DELETE /api/v1/blogs/<blog_id> - Delete a blog post(DELETE)

The path api/{version} number is a very common practice in API development. One, it tells us that we are in fact interfacing with an API. And two, it tells us what version we are using. The versioning is really important because we can develop, build, and deploy multiple versions at once. All while having the previous version still up and running. The api path is seen more often when we have a website attached to the API as a separate part. Google for instance uses this practice because they have multiple API's tat belong to them. This way they can keep all of their resources under one domain.

Installing Our Dependencies

To start off, lets make a new project directory and create a virtual environment. Open up a good place to hold this tutorial and run the following commands

  • mkdir flask_blog

  • cd flask_blog

  • python -m venv venv (where python is your version of python)

  • Then activate your virtual environment using the right activate script for your system

Now, we will need everything we talked about in the previous module. Copy the following command and run it in your terminal

pip install flask flask-sqlalchemy psycopg2 flask-migrate flask-script marshmallow flask-bcrypt pyjwt

The Project Directory

Inside of the flask_blog directory, add the following folders and files to mimic the image below

This structure will allow us to maintain and separate our project into meaningful segments. We don't want to define our models in the same file that we use our views. We also want to make sure our two models have separate views because they will handle different things, even though they still interact with each other. Flask is a great tool because there is a thousand ways to build an API or a fully functional website; however, this is best way to structure an API.

Last updated

Was this helpful?