19 Commits

Author SHA1 Message Date
858e18362f Added a single assert
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-14 13:55:20 +02:00
b226256187 fixed tests not running 2020-05-14 13:54:19 +02:00
32648d0f7a tests added
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-13 14:22:48 +02:00
7a565e0511 Fixed consumer test
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 21:25:18 +02:00
2c5444a245 post
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 21:08:37 +02:00
9fec4e81cc minor
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 21:04:13 +02:00
6ead97eee0 minor fix
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 21:03:40 +02:00
2227554977 backup 2020-05-08 21:02:49 +02:00
c58b6449aa tests definded
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 20:55:47 +02:00
f643b91a8b test_initial_commit
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 20:36:45 +02:00
d6b8c528de fixed sentry stuff
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 20:08:43 +02:00
2e8bc421c6 Merge pull request 'dev-consumer-timeout-fix' (#7) from dev-consumer-timeout-fix into dev
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 20:05:33 +02:00
7fbd62659d sync view fix
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2020-05-08 19:57:42 +02:00
d34756d9fc fixed getting customer instead of producer
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 19:56:09 +02:00
67d71fa43e Fixed customers endpoint
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 19:48:31 +02:00
eb03474392 Configure timeouts from envvar
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 19:44:42 +02:00
58d7de6073 updated redis structure
Some checks failed
continuous-integration/drone/push Build is failing
2020-05-08 19:34:29 +02:00
f39fa6f936 Merge pull request 'Add saving IP addresses of producers' (#5) from dev-prodip into dev
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing
2020-04-17 16:57:26 +02:00
2c6ab3d96d added saving producer ips to redis
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-04-17 16:21:42 +02:00
22 changed files with 156 additions and 259 deletions

2
.coveragerc Normal file
View File

@ -0,0 +1,2 @@
[run]
omit=venv/*

View File

@ -30,32 +30,6 @@ steps:
- docker push "$DOCKER_USERNAME/consumer_api"
- docker push "$DOCKER_USERNAME/consumer_api:$DRONE_BUILD_NUMBER"
- name: make_docs
image: python:3.8
commands:
- pip3 install Sphinx sphinx_rtd_theme
- pip3 install -r requirements.txt
- cd docs
- make html
- name: build_docs
image: docker:stable-dind
volumes:
- name: dockersock
path: /var/run
environment:
DOCKER_USERNAME:
from_secret: DOCKER_USERNAME
DOCKER_PASSWORD:
from_secret: DOCKER_PASSWORD
commands:
- cd docs
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- docker build -t="$DOCKER_USERNAME/consumer-api-docs" .
- docker build -t="$DOCKER_USERNAME/consumer-api-docs:$DRONE_BUILD_NUMBER" .
- docker push "$DOCKER_USERNAME/consumer-api-docs"
- docker push "$DOCKER_USERNAME/consumer-api-docs:$DRONE_BUILD_NUMBER"
- name: slack
image: plugins/slack
settings:

View File

@ -1,12 +0,0 @@
================
P2P Consumer API
================
Repository for the consumer's API
implemented interfaces / functionalities:
* consumer
* log
* sync

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from flask import Flask
import os
from db import redis_client
@ -14,16 +15,27 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "app"
__version__text__ = "1"
sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10")
# Setup sentry
SENTRY_DSN = os.environ.get("SENTRY_DSN")
if SENTRY_DSN:
sentry_sdk.init(
dsn = SENTRY_DSN,
integrations = [FlaskIntegration()],
send_default_pii = True,
release = os.environ.get('RELEASE_ID', 'test'),
environment = os.environ.get('RELEASEMODE', 'dev')
)
app = Flask(__name__)
app.config['REDIS_URL'] = os.environ['REDIS_URL']
app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID']
app.config['CUSTOMER_TIMEOUT'] = int(os.environ.get('CUSTOMER_TIMEOUT', 30))
app.config['PRODUCER_TIMEOUT'] = int(os.environ.get('PRODUCER_TIMEOUT', 60))
redis_client.init_app(app)
for view in [ConsumersView, LogView, SyncView]:
view.register(app, trailing_slash=False)
view.register(app, trailing_slash = False)
if __name__ == "__main__":
app.run(debug=True)
app.run(debug = True)

View File

@ -0,0 +1,5 @@
import os
# Setup environment variables for testing
os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54"
os.environ["REDIS_URL"] = "redis://192.168.111.121/0"

View File

@ -0,0 +1,22 @@
import db
import pytest
@pytest.fixture
def client(mocker):
mocker.patch("db.redis_client")
db.redis_client.get.side_effect=lambda a: None
from app import app
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_response_length(client):
r = client.get('/consumers')
assert r.status_code == 200

View File

@ -0,0 +1,30 @@
import db
import pytest
@pytest.fixture
def client(mocker):
mocker.patch("db.redis_client")
db.redis_client.get.side_effect=lambda a: None
from app import app
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_log_code_get(client):
r = client.get('/log')
assert r.status_code == 405
def test_log_code_post(client):
data = {
"uuid": "asdasdasd",
"message": "Hello There!"
}
r = client.post('/log', json=data)
assert r.status_code == 204

View File

@ -0,0 +1,30 @@
import db
import pytest
@pytest.fixture
def client(mocker):
mocker.patch("db.redis_client")
db.redis_client.get.side_effect=lambda a: None
from app import app
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_log_code_get(client):
r = client.get('/sync')
assert r.status_code == 405
def test_log_code_post(client):
data = {
"uuid": "tesuuid"
}
r = client.post('/sync', json=data)
assert r.status_code == 200

View File

@ -1,15 +1,19 @@
import json
from db import redis_client
from flask import jsonify, current_app
from flask import jsonify
from flask_classful import FlaskView
class ConsumersView(FlaskView):
def get(self):
# load the currently available consumer list from the redis database
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
keys = redis_client.keys('consumer_*')
# jsonify and return the list of active consumers
return jsonify([v['ip'] for k, v in consumer_list.items()])
list_of_customer_ips = []
for key in keys:
info = json.loads((redis_client.get(key) or b"{}").decode('utf-8'))
list_of_customer_ips.append(info['ip'])
return jsonify(list_of_customer_ips)

View File

@ -1,12 +1,32 @@
from flask import request, current_app, Response
from flask_classful import FlaskView
from db import redis_client
class LogView(FlaskView):
def post(self):
# display received message
# Record the IP address of the producer
remote_uuid = request.json['uuid']
remote_ip = request.remote_addr
prod_key = f"producer_{remote_uuid}"
last_known_remote_ip = redis_client.get(prod_key)
if last_known_remote_ip:
last_known_remote_ip = last_known_remote_ip.decode('utf-8')
if not last_known_remote_ip:
current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}")
elif last_known_remote_ip != remote_ip:
current_app.logger.info(
f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
# update expirity
redis_client.set(prod_key, remote_ip.encode('utf-8'))
redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"])
# print out message
current_app.logger.info(f"New message: {request.json['message']}")
# return HTTP 204 - No content message
return Response(status = 204)

View File

@ -1,7 +1,8 @@
import json
import time
from flask import request, current_app, jsonify
from flask_classful import FlaskView
from db import redis_client
from db import redis_client # ez nagyon otvar
class SyncView(FlaskView):
@ -10,25 +11,28 @@ class SyncView(FlaskView):
remote_uuid = request.json['uuid']
remote_ip = request.remote_addr
# load the currently available consumer list from the redis database
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
cust_key = f"consumer_{remote_uuid}"
if remote_uuid not in consumer_list.keys():
# display newly registered consumer
last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8'))
if not last_known_info:
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
else:
if consumer_list[remote_uuid]['ip'] != remote_ip:
# log address changes
if last_known_info['ip'] != remote_ip:
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
# update consumer list redis databasse
consumer_list.update(
{remote_uuid: {"ip": remote_ip}}
)
redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8'))
info = {
"uuid": remote_uuid,
"ip": remote_ip,
"last_seen": time.time()
}
redis_client.set(cust_key, json.dumps(info).encode('utf-8'))
redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"])
# return with the current UUID
response = {
"uuid": current_app.config['LOCAL_UUID']
}
return jsonify(response)

View File

@ -1,3 +0,0 @@
FROM nginx:latest
COPY _build/html/ /usr/share/nginx/html

View File

@ -1,20 +0,0 @@
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

View File

@ -1,55 +0,0 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
# -- Project information -----------------------------------------------------
project = 'Consumer API'
copyright = '2020, GoldenPogacsa Inc.'
author = 'GoldenPogacsa Inc.'
# The full version, including alpha/beta/rc tags
release = '0.1.0'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
]
# Add any paths that contain templates here, relative to this directory.
templates_path = []
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []

View File

@ -1,23 +0,0 @@
.. Consumer API documentation master file, created by
sphinx-quickstart on Wed Apr 8 21:31:18 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Consumer API's documentation!
========================================
.. toctree::
:maxdepth: 2
:caption: Contents:
readme
source/modules
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
* `Git repository <https://git.kmlabz.com/GoldenPogacsa/consumer-api>`_

View File

@ -1,35 +0,0 @@
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
if "%1" == "" goto help
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd

View File

@ -1 +0,0 @@
.. include:: ../README.rst

View File

@ -1,7 +0,0 @@
app module
==========
.. automodule:: app
:members:
:undoc-members:
:show-inheritance:

View File

@ -1,7 +0,0 @@
db module
=========
.. automodule:: db
:members:
:undoc-members:
:show-inheritance:

View File

@ -1,9 +0,0 @@
consumer_api
============
.. toctree::
:maxdepth: 4
app
db
views

View File

@ -1,38 +0,0 @@
views package
=============
Submodules
----------
views.consumers\_view module
----------------------------
.. automodule:: views.consumers_view
:members:
:undoc-members:
:show-inheritance:
views.log\_view module
----------------------
.. automodule:: views.log_view
:members:
:undoc-members:
:show-inheritance:
views.sync\_view module
-----------------------
.. automodule:: views.sync_view
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: views
:members:
:undoc-members:
:show-inheritance:

4
requirements_dev.txt Normal file
View File

@ -0,0 +1,4 @@
pytest
pytest-mock
mock
coverage