项目作者: devsetgo

项目描述 :
A test/psuedo API to use as sample data or test data.
高级语言: Python
项目地址: git://github.com/devsetgo/test-api.git
创建时间: 2019-04-06T12:15:51Z
项目社区:https://github.com/devsetgo/test-api

开源协议:MIT License

下载


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

    1. git clone https://github.com/devsetgo/test-api.git
    2. python-3 -m venv env
    3. source env/bin/activate
    4. 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
    1. from src folder: ./scripts/install.sh
    2. Production: pip3 install -r requirements.txt
    3. Development: pip3 install -r requirements/dev.txt

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

  1. $ cp .env_example .env

.env_sample file

  1. # # This is used to determin if .env or other external config is used. True is for a .env file and false for docker enviroment
  2. # # option: dotenv, docker
  3. USE_ENV='dotenv'
  4. # Application information
  5. TITLE="Test API"
  6. DESCRIPTION="Test APIs for tools and other examples"
  7. APP_VERSION='One'
  8. OWNER='Your Name'
  9. WEBSITE='https://your.domain.com/support'
  10. # Demo settings
  11. CREATE_SAMPLE_DATA=true
  12. NUMBER_TASKS=10
  13. NUMBER_USERS=10
  14. NUMBER_GROUPS=10
  15. # Cofigurations
  16. HOST_DOMAIN='https://your.domain.com'
  17. #prd for production or 'dev' for development
  18. RELEASE_ENV='dev'
  19. # Turn HTTPS Middleware on (True) or off (False)
  20. HTTPS_ON=false
  21. # Turn on Prometheus endpoint
  22. PROMETHEUS_ON=true
  23. # data base URI
  24. SQLALCHEMY_DATABASE_URI='sqlite:///sqlite_db/api.db'
  25. # Loguru settings
  26. LOGURU_RETENTION='10 days'
  27. LOGURU_ROTATION='100 MB'
  28. # Values NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL
  29. LOGURU_LOGGING_LEVEL='INFO'
  30. # Workers - Set to 1 for CPUs x 2 + 1
  31. WORKERS=4
  32. # Leave intact for license
  33. CREATED_BY='Mike Ryan'
  34. LICENSE_TYPE='MIT'
  35. LICENSE_LINK='https://github.com/devsetgo/test-api/blob/master/LICENSE'

Start the app

  1. FROM SCR
  2. Development"
  3. ./scripts/dev_run.sh
  4. Production:
  5. ./scripts/prd_run.sh
  6. UVICORN
  7. Development:
  8. uvicorn main:app --port 5000 --reload
  9. python3 main.py (running Uvicorn from Code - no reload)
  10. Production:
  11. uvicorn main:app --port 5000 --workers 2
  12. python3 main.py (running Uvicorn from code)
  13. gunicorn -c gunicorn_cfg.py main:app
  14. # Note: gunicorn is the config for the dockerfile
  15. Docker
  16. Docker: docker pull mikeryan56/test-api:latest

Run Tests

By commands

SchemaThesis

  1. 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
  2. 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
  1. ./scripts/tests.sh
  1. python3 -m pytest

Create coverage badge

  1. coverage-badge -o coverage.svg -f

As a Script from src directory

  1. ./scripts/tests.sh

Pre-Commit & Hooks

  1. - Follow install instructionsL: [https://pre-commit.com/#install](https://pre-commit.com/#install)
  2. - pre-commit install
  3. - 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

  • [X] Setup CI/CD Pipeline for test and deployment
    • [X] Travis-CI replaced by Github Actions
    • [X] 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
  • [x] Better organization

  • Standardize API pattern for versioning
  • Access Controls
    • Add Access controls and signup
    • Add JWT/Token access
    • Rate limiting
  • [X] 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