Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
/ test-api Public archive

A test/psuedo API to use as sample data or test data.

License

Notifications You must be signed in to change notification settings

devsetgo/test-api

Repository files navigation

Python: image Code style: black Python 3.11

CI/CD Pipeline:

Actions Status Publish Docker image Docker Image

SonarCloud:

Coverage Lines of Code Maintainability Rating Reliability Rating Security Rating Quality Gate Status Vulnerabilities

Test-API a FASTAPI Example

A test/psuedo API to use as sample data or test data. Inspired by FakeResponse.com. Documentation can be found at devsetgo.com/projects/test-api.

Note

  • This requires a *Nix environment to run. (Linux, Unix, Windows 10 WSL (unbuntu tested) and I think Mac OS (I don't use a Mac)
  • It should be considered only an example and just something to learn from.

Create Environment

  • Copy the repository

    git clone https://github.com/devsetgo/test-api.git
    python-3 -m venv env
    source env/bin/activate
    cd app
    
  • Notes:

    • Everything has been test on Python 3.11. Should run on 3.7 - 3.11.
    • You may need to upgrade pip and setuptools first (pip3 install --upgrade pip setuptools)
  • Install requirements

    from src folder: ./scripts/install.sh
    Production: pip3 install -r requirements.txt
    Development: pip3 install -r requirements/dev.txt
    

Setup: Copy .env_sample to .env and set configuration as desired.

$ cp .env_example .env

.env_sample file

# # This is used to determin if .env or other external config is used. True is for a .env file and false for docker enviroment
# # option: dotenv, docker
USE_ENV='dotenv'

# Application information
TITLE="Test API"
DESCRIPTION="Test APIs for tools and other examples"
APP_VERSION='One'
OWNER='Your Name'
WEBSITE='https://your.domain.com/support'

# Demo settings
CREATE_SAMPLE_DATA=true
NUMBER_TASKS=10
NUMBER_USERS=10
NUMBER_GROUPS=10

# Cofigurations
HOST_DOMAIN='https://your.domain.com'
#prd for production or 'dev' for development
RELEASE_ENV='dev'
# Turn HTTPS Middleware on (True) or off (False)
HTTPS_ON=false
# Turn on Prometheus endpoint
PROMETHEUS_ON=true

# data base URI
SQLALCHEMY_DATABASE_URI='sqlite:///sqlite_db/api.db'
# Loguru settings
LOGURU_RETENTION='10 days'
LOGURU_ROTATION='100 MB'
# Values NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL
LOGURU_LOGGING_LEVEL='INFO'
# Workers - Set to 1 for CPUs x 2 + 1
WORKERS=4
# Leave intact for license
CREATED_BY='Mike Ryan'
LICENSE_TYPE='MIT'
LICENSE_LINK='https://github.com/devsetgo/test-api/blob/master/LICENSE'

Start the app

FROM SCR
    Development"
        ./scripts/dev_run.sh
    Production:
        ./scripts/prd_run.sh

UVICORN
    Development:
        uvicorn main:app --port 5000 --reload
        python3 main.py (running Uvicorn from Code - no reload)

    Production:
        uvicorn main:app --port 5000 --workers 2
        python3 main.py (running Uvicorn from code)
        gunicorn -c gunicorn_cfg.py main:app
        # Note: gunicorn is the config for the dockerfile

Docker
    Docker: docker pull mikeryan56/test-api:latest

Run Tests

By commands

SchemaThesis

st run --workers 10 --fixups fast_api --request-timeout 21000 --max-response-time 21000 --max-failures 2 --hypothesis-deadline 2000 --junit-xml junit.xml http://127.0.0.1:5000/openapi.json

st run --workers 6 --fixups fast_api --request-timeout 21000 --max-response-time 21000 --max-failures 2 --junit-xml junit.xml http://127.0.0.1:5000/openapi.json
./scripts/tests.sh
python3 -m pytest

Create coverage badge

    coverage-badge -o coverage.svg -f

As a Script from src directory

./scripts/tests.sh

Pre-Commit & Hooks - Follow install instructionsL: https://pre-commit.com/#install - pre-commit install - pre-commit run -a

Features

  • default

    • GET / (root) Forward to OpenAPI to /docs
    • GET /Information endpoint containing basic app info
    • GET /joke PyJoke list
  • groups

    • GET /api/v1/groups/list
    • GET /api/v1/groups/list/count
    • PUT /api/v1/groups/state
    • POST /api/v1/groups/create
    • GET /api/v1/groups/group
    • POST /api/v1/groups/user/create
    • DELETE /api/v1/groups/user/delete
  • textblob

    • POST /api/v1/textblob/sentiment
    • POST /api/v1/textblob/spellcheck
  • todos

    • GET /api/v1/todo/list
    • GET /api/v1/todo/list/count
    • GET /api/v1/todo/list/{todo_id}
    • DELETE /api/v1/todo/list/{todo_id}
    • PUT /api/v1/todo/list/{todo_id}
    • POST /api/v1/todo/create/
  • users

    • GET /api/v1/users/list
    • GET /api/v1/users/list/count
    • GET /api/v1/users/list/{user_id}
    • DELETE /api/v1/users/list/{user_id}
    • PUT /api/v1/users/list/{user_id}
    • POST /api/v1/users/create/
    • POST /api/v1/users/check-pwd/
  • tools

    • POST /api/v1/tools/convert-to/xml
    • POST /api/v1/tools/convert-to/json
  • silly users

    • GET /api/v1/silly-users/make-one
    • GET /api/v1/silly-users/list
  • health

    • GET /api/health/
    • GET /api/health/system-info
    • GET /api/health/processes

Issues/Bugs

  • None

TODO

  • Setup CI/CD Pipeline for test and deployment
    • [X] Travis-CI replaced by Github Actions
    • SonarCloud
    • Github Actions found in .github/workflow/actions
      • docker-rc - docker build and push when pull request approved for release-candidate branch (calendar version - rc)
      • docker-master - docker build and push when pull request approved for master branch (calender version and latest)
      • ensure docker build only happens after pull_request approved and merged into higher branch
  • Make Twelve Factor App ready

Application

  • Refactor by endpoint (sample, user, etc..)

  • Create tests

    • Minimum of 80%
    • Exception Testing
  • Extend API parameters for Users

    • Pagination
    • Number of Items per list returned (Max)
    • Additional Optional for filtering
  • Extend API parameters for ToDo

    • Pagination
    • Number of Items per list returned (Max)
    • Additional Optional for filtering
  • Extend Tools API

    • Text Conversion
      • XML to JSON (required to be valid XML)
      • JSON to XML (required to be valid XML)
    • Text Functions
      • Language of text
  • Better organization

  • Standardize API pattern for versioning

  • Access Controls

    • Add Access controls and signup
    • Add JWT/Token access
    • Rate limiting
  • Gunicorn with Uvicorn configuration

  • Logging (using Loguru)

  • Build a cookiecutter template for future projects similar to the FastAPI example

  • Add code comments

  • Work on one to many relationshipts

  • Validate userId in ToDo's

  • Work on Connection Pool for SQLite and Postgres for scaling

  • Docker

  • Tutorials/Documentation

    • Basic Overview
    • Explantion of functions