Skip to main content

Dynamically configurable server

Project description

loose-server

https://travis-ci.com/KillAChicken/loose-server.svg?branch=master

Loose server is a simple configurable server. It can be used to create temporary servers, dynamically add or remove endpoints and set responses for them from an application.

Installation

$ python -m pip install loose-server

Additional packages will be installed: Flask, flask-restful (required for the server) and requests (required for the http clients).

Usage

Loose server has 2 variations:

  • Standalone server with API to manage rules via HTTP.

  • Flask application that can be used as a configurable mock in unit-tests.

Standalone server

A server can be started by the command

$ python -m looseserver.default.server.run
 * Serving Flask app "looseserver" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:50000/ (Press CTRL+C to quit)

API endpoints are nested to the base configuration url. By default it is /_configuration/.

from looseserver.default.client.rule import PathRule
from looseserver.default.client.response import FixedResponse
from looseserver.default.client.http import HTTPClient

client = HTTPClient(configuration_url="http://127.0.0.1:50000/_configuration/")

path_rule_spec = PathRule(path="example")
path_rule = client.create_rule(rule=path_rule_spec)

json_response = FixedResponse(
    status=200,
    headers={"Content-Type": "application/json"},
    body='{"key": "value"}',
    )
client.set_response(rule_id=path_rule.rule_id, response=json_response)

All configured endpoints have a common prefix. By default it is /routes/. The response can be obtained by the following request

$ curl http://127.0.0.1:50000/routes/example -i
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 16
Server: Werkzeug/0.15.2 Python/3.5.2
Date: Fri, 05 Apr 2019 14:08:50 GMT

{"key": "value"}

Configurable mock

Loose server can be used as a mock server in the following way

from urllib.parse import urljoin

from looseserver.server.application import DEFAULT_BASE_ENDPOINT, DEFAULT_CONFIGURATION_ENDPOINT
from looseserver.default.server.application import configure_application
from looseserver.default.client.flask import FlaskClient
from looseserver.default.client.rule import PathRule
from looseserver.default.client.response import FixedResponse

application = configure_application()

app_client=application.test_client()

client = FlaskClient(
    configuration_url=DEFAULT_CONFIGURATION_ENDPOINT,
    application_client=app_client,
    )

path_rule_spec = PathRule(path="example")
path_rule = client.create_rule(rule=path_rule_spec)

json_response = FixedResponse(
    status=200,
    headers={"Content-Type": "application/json"},
    body='{"key": "value"}',
    )
client.set_response(rule_id=path_rule.rule_id, response=json_response)

response = app_client.get(urljoin(DEFAULT_BASE_ENDPOINT, "example"))
assert response.headers["Content-Type"] == "application/json"
assert response.json == {'key': 'value'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loose-server-0.4.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

loose_server-0.4-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file loose-server-0.4.tar.gz.

File metadata

  • Download URL: loose-server-0.4.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for loose-server-0.4.tar.gz
Algorithm Hash digest
SHA256 717fd6f7ade308301fa317907e9d884e0d458557cdc79b36d3ec820bdc58c419
MD5 d839657281ca109e1beb2288342d433c
BLAKE2b-256 568de2bc0bd3dbe9062a796149b1b0932f059e74326a6a283970182c2023e895

See more details on using hashes here.

File details

Details for the file loose_server-0.4-py3-none-any.whl.

File metadata

  • Download URL: loose_server-0.4-py3-none-any.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for loose_server-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 71f99d3bd1e6e73978afad9369752c7c1207e9f96be9603ffea16e29ec8111d7
MD5 1cb7d83e4ab33fb5a9980deb5b45000f
BLAKE2b-256 009857c58f2cfc8cec96d7639dbae2f7747de39b0cf52dee30043f4b48dc4253

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page