Skip to content

Latest commit

History

History
101 lines (68 loc) 路 3.4 KB

File metadata and controls

101 lines (68 loc) 路 3.4 KB

PyFake-API-Server

PyFake-API-Server is a tool for faking API server easily and quickly by configuration only.

How to use it?

You need to configure a YAML file to set API details. Let's demonstrate a sample configuration for you:

mocked_apis:
  foo_home:
    url: '/foo'
    http:
      request:
        method: 'GET'
      response:
        strategy: string
        value: 'This is Foo home API.'

From above settings, it means setting an API:

  • URL path: /foo
  • Allow HTTP request method GET without parameters
  • If request successfully, it would return value This is Foo home API..

And save above setting as file /User/foo/fake-server-demo/api.yaml.

Let's set up an instance to provide service:

docker run --name fake-server \
           -v /User/foo/fake-server-demo:/mit-pyfake-api-server \
           -p 9672:9672 \
           -d \
           pyfake-api-server:v0.1.0

Try to send a HTTP request to the service:

curl http://127.0.0.1:9672/foo

Congratulations! You successfully configure and set up a web server for faking API.

Environment variables

When you set up a PyFake-API-Server instance by Docker, you can pass one or more environment variables to set its settings.

CONFIG_PATH

This is an option variable. It would set the file path where it should use to configure the APIs and set up web server. In default, its value is api.yaml.

WEB_FRAMEWORK

This is an option variable. The web framework PyFake-API-Server would use to set up web server to fake APIs. It only accepts 3 type values:

  • auto:

    This is the default value. It would automatically detect which web framework PyFake-API-Server could use in current runtime environment.

  • flask:

    Use Python web framework Flask to set up web server.

  • fastapi

    Use Python web framework FastAPI to set up web server.

WORKERS

This is an option variable. How many workers it should use to handle HTTP requests in the web server. In default, its value is 1.

LOG_LEVEL

This is an option variable. Set the log level to let web server site show log message which is not below it. In default, its value is info.

Quick reference