-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from evo-company/add-aiohttp-and-requests-client
Add aiohttp and requests client
- Loading branch information
Showing
38 changed files
with
768 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ README.md | |
.lets | ||
helm | ||
.ipython | ||
.ptpython | ||
.secrets | ||
|
||
# Ignore IDE settings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
|
||
HAS_STAGED_PY=$(git diff --staged --diff-filter=d --name-only '*.py') | ||
|
||
if [ -n "$HAS_STAGED_PY" ]; then | ||
|
||
echo "Running mypy ..." | ||
lets mypy | ||
if [[ $? -ne 0 ]]; then | ||
exit 1 | ||
fi | ||
|
||
echo "Running black ..." | ||
lets black --diff --check | ||
if [[ $? -ne 0 ]]; then | ||
exit 1 | ||
fi | ||
|
||
echo "Running ruff ..." | ||
lets ruff-diff | ||
if [[ $? -ne 0 ]]; then | ||
exit 1 | ||
fi | ||
|
||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,32 @@ | ||
Examples | ||
======== | ||
|
||
TODO: refactor examples | ||
|
||
Here you can find examples for gRPC and HTTP clients with: | ||
|
||
- `AIOHTTP` | ||
- `aiohttp` | ||
- `Sanic` | ||
- `Flask` | ||
- `WSGI` | ||
- `httpx` | ||
|
||
Prerequisites: | ||
|
||
.. code-block:: shell | ||
|
||
$ pip install featureflags-client | ||
|
||
If you're using AsyncIO: | ||
|
||
.. code-block:: shell | ||
|
||
$ pip install grpclib | ||
|
||
else: | ||
|
||
.. code-block:: shell | ||
|
||
$ pip install grpcio | ||
|
||
Configuration for all examples located in ``config.py`` module. | ||
|
||
Feature flags and variables are defined in ``flags.py`` module. | ||
|
||
Every example starts a HTTP server and available on http://localhost:5000 | ||
|
||
AIOHTTP: | ||
|
||
.. code-block:: shell | ||
|
||
$ PYTHONPATH=../client:../protobuf python aiohttp_app.py | ||
|
||
Sanic: | ||
- sync + grpc: | ||
|
||
.. code-block:: shell | ||
> pip install featureflags-client[grpclib] | ||
$ PYTHONPATH=../client:../protobuf python sanic_app.py | ||
- async + grpc: | ||
|
||
Flask: | ||
> pip install featureflags-client[grpcio] | ||
.. code-block:: shell | ||
- async + http: | ||
|
||
$ PYTHONPATH=../client:../protobuf python flask_app.py | ||
> pip install featureflags-client[httpx] | ||
WSGI: | ||
or | ||
|
||
.. code-block:: shell | ||
> pip install featureflags-client[aiohttp] | ||
$ PYTHONPATH=../client:../protobuf python wsgi_app.py | ||
- sync + http: | ||
|
||
.. _AIOHTTP: https://aiohttp.readthedocs.io/ | ||
.. _Sanic: https://sanic.readthedocs.io/ | ||
.. _Flask: http://flask.pocoo.org | ||
.. _WSGI: https://www.python.org/dev/peps/pep-0333/ | ||
> pip install featureflags-client[requests] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
|
||
import config | ||
import flags | ||
from aiohttp import web | ||
|
||
from featureflags_client.http.client import FeatureFlagsClient | ||
from featureflags_client.http.managers.aiohttp import AiohttpManager | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
async def on_start(app): | ||
app["ff_manager"] = AiohttpManager( | ||
url=config.FF_URL, | ||
project=config.FF_PROJECT, | ||
variables=[flags.REQUEST_QUERY], | ||
defaults=flags.Defaults, | ||
) | ||
app["ff_client"] = FeatureFlagsClient(app["ff_manager"]) | ||
|
||
try: | ||
await app["ff_client"].preload_async(timeout=5) | ||
except Exception: | ||
log.exception( | ||
"Unable to preload feature flags, application will " | ||
"start working with defaults and retry later" | ||
) | ||
|
||
# Async managers need to `start` and `wait_closed` to be able to | ||
# run flags update loop | ||
app["ff_manager"].start() | ||
|
||
|
||
async def on_stop(app): | ||
await app["ff_manager"].wait_closed() | ||
|
||
|
||
@web.middleware | ||
async def middleware(request, handler): | ||
ctx = {flags.REQUEST_QUERY.name: request.query_string} | ||
with request.app["ff_client"].flags(ctx) as ff: | ||
request["ff"] = ff | ||
return await handler(request) | ||
|
||
|
||
async def index(request): | ||
if request["ff"].TEST: | ||
return web.Response(text="TEST: True") | ||
else: | ||
return web.Response(text="TEST: False") | ||
|
||
|
||
def create_app(): | ||
app = web.Application(middlewares=[middleware]) | ||
|
||
app.router.add_get("/", index) | ||
app.on_startup.append(on_start) | ||
app.on_cleanup.append(on_stop) | ||
|
||
app["config"] = config | ||
|
||
return app | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.INFO) | ||
logging.getLogger("featureflags").setLevel(logging.DEBUG) | ||
|
||
web.run_app(create_app(), port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import logging | ||
|
||
import config | ||
import flags | ||
from flask import Flask, g, request | ||
from werkzeug.local import LocalProxy | ||
|
||
from featureflags_client.http.client import FeatureFlagsClient | ||
from featureflags_client.http.managers.requests import RequestsManager | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
def get_ff_client(): | ||
ff_client = getattr(g, "_ff_client", None) | ||
if ff_client is None: | ||
manager = RequestsManager( | ||
url=config.FF_URL, | ||
project=config.FF_PROJECT, | ||
variables=[flags.REQUEST_QUERY], | ||
defaults=flags.Defaults, | ||
) | ||
ff_client = g._ff_client = FeatureFlagsClient(manager) | ||
return ff_client | ||
|
||
|
||
def get_ff(): | ||
if "_ff" not in g: | ||
g._ff_ctx = get_ff_client().flags( | ||
{ | ||
flags.REQUEST_QUERY.name: request.query_string, | ||
} | ||
) | ||
g._ff = g._ff_ctx.__enter__() | ||
return g._ff | ||
|
||
|
||
@app.teardown_request | ||
def teardown_request(exception=None): | ||
if "_ff" in g: | ||
g._ff_ctx.__exit__(None, None, None) | ||
del g._ff_ctx | ||
del g._ff | ||
|
||
|
||
ff = LocalProxy(get_ff) | ||
|
||
|
||
@app.route("/") | ||
def index(): | ||
if ff.TEST: | ||
return "TEST: True" | ||
else: | ||
return "TEST: False" | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.INFO) | ||
logging.getLogger("featureflags").setLevel(logging.DEBUG) | ||
|
||
app.run(port=5000) |
Oops, something went wrong.