RESTful API and HTTP Automated Tests

Intro

App infrastructure:
  • There are 3 main components of RESTful application:
    1. Frontend - web-app look.
    2. Web Service - between as frontend doesn't communicate with backend directly.
    3. Backend - web-app database.
  • Frontend communicates through RESTful API to web service about what infomation it needs to display in the web-app.
  • Web service perfoms queries to database directy.
Http requests:
  • A RESTful API uses Http requests to help a client (front-end, web, mobile app or client.py) to interact with server's database.
  • Http requests (GET, POST or DELETE) allow for retrieving, inserting or deleting data.
  • With RESTful API, I can define the way of interaction between client and server.
API architecture:
  • The representation of a database to API is Python Data Model.
  • Python Data Model is a class that with its attributes reflects fields of data schema (fields of data table in relational database analogously).
  • In the project, I define two classes: TransactionModel and PayerModel.
  • The representation of Python Data Model to client App is a defined within API Resource.
  • For each specific Data Model we need to define separate API Resource:
    - for TransactionModel there is need to define Transaction API Resource.
    - for PayerModel there is need to define Payer API Resource.
  • Resource is an external representation of internal data Model.
  • Client App can access Python object of given Model only thorugh API Resource.
  • Client App can interact with Resource using HTTP requests.
Http status codes:
  • Web service responses to client app on its requests with following statueses:
    - 404 - not found,
    - 400 - bad request,
    - 202 - accepted,
    - 201 - created,
    - 200 - OK.
Workflow:
  • Client app hits an endpoint sending GET request through RESTful API. Web service accepts it, connects database and formulates response and sends it back to a client with JSON.
  • Client app hits an endpoint sending POST request through RESTful API along with information being wrapped with JSON. Web service accepts it, connects database, parses JSON into Python variables which feed a newly-created Python model object (what creates a new record in database) and returns JSON response with a Http status code to a client.
  • With that solution, RESTful API deciedes what Model objects (what data) are accessible at what place (endpoint) and in what way i.e. getting, posting, putting, deleting.

Features

App includes following features:

  • RESTful API
  • Flask
  • SQLite database
  • Pytest

Demo

Client - API - Server model:
  1. Client hits the api endpoint like server.com/transaction/transaction_id:
    - transaction/ redirects to a specific for transaction web server spot,
    - transaction_id serves as variable which is passed to api resource.
  2. With hitting endpoint client sends POST request passing JSON along with transaction_id variable.
  3. Flask server is listening all the time in the backgorund.
  4. When request comes in, Flask redirects it to API Resource accoring to the hit transaction/ endpoint.
  5. Request calls post method in API Resource which parses parameters from JSON into Python variables.
  6. Resource's POST method creates Model instance passing obtained variables as arguments to class constructor.
  7. Resource uses Model's methods to save instance into database.
Testing:
  • There are 2 kinds of testing we can perform on our application:
    - Unit test - tests single unit of application f.e. we compare if a function returns expexted value or expected data type.
    - Integration test - tests interaction between modules being combined as a one group.
  • Script test_api.py, utilizing pytest library, tests HTTP requests on api.py server:

Tests passed successfully.

Setup

Python libraries installation required:

pip install flask
pip install requests
pip install pytest

Source Code

You can view the source code: HERE