From 82cccea33047f84c3f88fed560dfd617b70f4cec Mon Sep 17 00:00:00 2001 From: Yaashika Date: Tue, 15 May 2018 18:39:15 +0530 Subject: [PATCH 01/11] to recover --- .editorconfig | 13 +++++++++++ .gitignore | 38 ++++++++++++++++++++++++++++++ Makefile | 12 ++++++++++ README.rst | 3 +++ api/handler.py | 41 +++++++++++++++++++++++++++++++++ mongo/client.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 2 ++ requirements.txt | 3 +++ server.py | 18 +++++++++++++++ tests/.gitkeep | 0 10 files changed, 185 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.rst create mode 100644 api/handler.py create mode 100644 mongo/client.py create mode 100644 requirements-dev.txt create mode 100644 requirements.txt create mode 100644 server.py create mode 100644 tests/.gitkeep diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..110f0ec --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true + +[*.py] +indent_style = space +indent_size = 4 + +[Makefile] +indent_style = tab \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6331107 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +.DS_Store + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Environments +env/ +venv/ +ENV/ + +# Sphinx documentation +docs/_build/ +.vscode/ +.idea/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4823e91 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +install: + pip3 install -r requirements.txt + +run: + python3 -m server.py + +lint: + pycodestyle --exclude='./ENV/*.*' ./ + +test: lint + pytest tests/ + diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..a52ed99 --- /dev/null +++ b/README.rst @@ -0,0 +1,3 @@ +virtualenv -p python3 ENV + +source ENV/bin/activate diff --git a/api/handler.py b/api/handler.py new file mode 100644 index 0000000..e4ef868 --- /dev/null +++ b/api/handler.py @@ -0,0 +1,41 @@ +import json + +from aiohttp.web import Response, post + +from mongo import client as m_client + + +async def handler(request): + await m_client.do_count() + mdoc = {'status1': 'success'} + return Response(text=json.dumps(mdoc), status=200) + + +async def handle(request): + d = await request.json() + await m_client.do_find_one(d) + mdoc = {'status2': 'success'} + return Response(text=json.dumps(mdoc), status=200) + + +async def update(request): + d = await request.json() + print(d) + await m_client.do_update(d) + response_doc = {'status4': 'success'} + return Response(text=json.dumps(response_doc), status=200) + + +async def delete(request): + await m_client.do_del() + mdoc = {'status3': 'success'} + return Response(text=json.dumps(mdoc), status=200) + + +async def new_user(request): + d = await request.json() + print(d) + await m_client.do_insert(d) + response_doc = {'status': 'success'} + return Response(text=json.dumps(response_doc), status=200) + diff --git a/mongo/client.py b/mongo/client.py new file mode 100644 index 0000000..aa96788 --- /dev/null +++ b/mongo/client.py @@ -0,0 +1,55 @@ +import motor +from bson import ObjectId +from motor.motor_asyncio import AsyncIOMotorClient +import logging + +m_client = motor.motor_asyncio.AsyncIOMotorClient() + +db = m_client.m1 +collection = db.m1c + + +async def do_insert(doc): + result = await collection.insert(doc) + return result + + +async def do_find_all(): + async for document in collection.find({}): + print(document) + logging.info(document) + return document + + +async def do_find_one(d): + document = await collection.find_one(d) + print(document) + + +async def do_count(): + cnt = await collection.find().count() + print('%s Count of documents ' % cnt) + return cnt + + +async def do_del(): + n = await collection.count() + print('%s Documents before calling del()' % n) + result = await collection.delete_many({}) + print('%s Documents after!' % (await collection.count())) + return result + + +async def do_update(d): + id = await collection.find_one(d) + result = await collection.update({'$set': d}) + print(result) + return result + + +async def do_replace(): + pass + + + + diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..1757d7c --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +pycodestyle==2.4.0 +-r requirements.txt \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bacd470 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +bson +aiohttp== 3.1.3 +motor==1.2.1 diff --git a/server.py b/server.py new file mode 100644 index 0000000..707453b --- /dev/null +++ b/server.py @@ -0,0 +1,18 @@ +from aiohttp import web +from api import handler + + +def run(): + """ Starts the Server + """ + app = web.Application() + app.router.add_get('/', handler.handler) + app.router.add_post('/insert', handler.new_user) + app.router.add_get('/find', handler.handle) + app.router.add_put('/update', handler.update) + app.router.add_delete('/delete', handler.delete) + + web.run_app(app) + + +run() diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29 From 3a04de4f2d543247bde93583c60752cfac6dc777 Mon Sep 17 00:00:00 2001 From: Yaashika Date: Wed, 16 May 2018 13:44:39 +0530 Subject: [PATCH 02/11] DONE(except update) --- tests/{.gitkeep => test_handler.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{.gitkeep => test_handler.py} (100%) diff --git a/tests/.gitkeep b/tests/test_handler.py similarity index 100% rename from tests/.gitkeep rename to tests/test_handler.py From b0bc142d36a3f9f01b21d87255c71a60974dbdbc Mon Sep 17 00:00:00 2001 From: Yaashika Date: Wed, 16 May 2018 13:50:21 +0530 Subject: [PATCH 03/11] Done/update Signed-off-by: Yaashika --- api/handler.py | 30 ++++++++++++++++-------------- mongo/client.py | 21 +++++---------------- server.py | 10 +++++----- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/api/handler.py b/api/handler.py index e4ef868..cf2b24a 100644 --- a/api/handler.py +++ b/api/handler.py @@ -1,39 +1,41 @@ import json -from aiohttp.web import Response, post +from aiohttp.web import Response from mongo import client as m_client async def handler(request): - await m_client.do_count() + request = await m_client.do_count() mdoc = {'status1': 'success'} return Response(text=json.dumps(mdoc), status=200) -async def handle(request): - d = await request.json() +async def get_user(user_id=None): + d = await user_id.json() + print(d) await m_client.do_find_one(d) - mdoc = {'status2': 'success'} - return Response(text=json.dumps(mdoc), status=200) + response_doc = {'status2': 'success'} + return Response(text=json.dumps(response_doc), status=200) -async def update(request): - d = await request.json() +async def update_user(user_data): + d = await user_data.json() print(d) await m_client.do_update(d) response_doc = {'status4': 'success'} return Response(text=json.dumps(response_doc), status=200) -async def delete(request): - await m_client.do_del() - mdoc = {'status3': 'success'} - return Response(text=json.dumps(mdoc), status=200) +async def delete_user(user_id): + d = await user_id.json() + await m_client.do_del(d) + response_doc = {'status3': 'success'} + return Response(text=json.dumps(response_doc), status=200) -async def new_user(request): - d = await request.json() +async def add_user(user_data): + d = await user_data.json() print(d) await m_client.do_insert(d) response_doc = {'status': 'success'} diff --git a/mongo/client.py b/mongo/client.py index aa96788..9c8b1aa 100644 --- a/mongo/client.py +++ b/mongo/client.py @@ -1,7 +1,5 @@ import motor -from bson import ObjectId from motor.motor_asyncio import AsyncIOMotorClient -import logging m_client = motor.motor_asyncio.AsyncIOMotorClient() @@ -14,13 +12,6 @@ async def do_insert(doc): return result -async def do_find_all(): - async for document in collection.find({}): - print(document) - logging.info(document) - return document - - async def do_find_one(d): document = await collection.find_one(d) print(document) @@ -32,24 +23,22 @@ async def do_count(): return cnt -async def do_del(): +async def do_del(d): n = await collection.count() print('%s Documents before calling del()' % n) - result = await collection.delete_many({}) + result = await collection.delete_one(d) print('%s Documents after!' % (await collection.count())) return result async def do_update(d): - id = await collection.find_one(d) - result = await collection.update({'$set': d}) + id = await collection.find_one() + print(id) + result = await collection.update_one({"_id": 1}, {'$set': d}) print(result) return result -async def do_replace(): - pass - diff --git a/server.py b/server.py index 468e90f..7d511d1 100644 --- a/server.py +++ b/server.py @@ -8,11 +8,11 @@ def run(): """ Starts the Server """ app = web.Application() - app.router.add_get('/', handler.handler) - app.router.add_post('/insert', handler.new_user) - app.router.add_get('/find', handler.handle) - app.router.add_put('/update', handler.update) - app.router.add_delete('/delete', handler.delete) + app.router.add_get('/user', handler.handler) + app.router.add_post('/add/user', handler.add_user) + app.router.add_get('/find/user', handler.get_user) + app.router.add_put('/update/user', handler.update_user) + app.router.add_delete('/delete/user', handler.delete_user) web.run_app(app) From 50843b59843923aa45e9e72f2db4a49c61e61fdf Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 18 May 2018 15:09:34 +0530 Subject: [PATCH 04/11] CRD_changes Signed-off-by: Yaashika --- api/handler.py | 85 +++++++++++++++++++++++++++++-------------- mongo/client.py | 46 +++++++++++------------ server.py | 10 ++--- tests/test_handler.py | 12 ++++++ 4 files changed, 94 insertions(+), 59 deletions(-) diff --git a/api/handler.py b/api/handler.py index cf2b24a..b9abb44 100644 --- a/api/handler.py +++ b/api/handler.py @@ -1,43 +1,72 @@ import json +from aiohttp import web + from aiohttp.web import Response from mongo import client as m_client -async def handler(request): - request = await m_client.do_count() - mdoc = {'status1': 'success'} - return Response(text=json.dumps(mdoc), status=200) - +async def add_user(request): + # user_id_insert_request + user_data = await request.json() + user_id = user_data.get("_id") + # check if _id already exists + check_id = await m_client.do_find_one(user_id) + if not check_id: + inserted_data = await m_client.do_insert(user_data) + # find the inserted_data + find_data = await m_client.do_find_one(inserted_data) + return Response(text=json.dumps(find_data), status=200) + return Response(text='_id already exists !') -async def get_user(user_id=None): - d = await user_id.json() - print(d) - await m_client.do_find_one(d) - response_doc = {'status2': 'success'} - return Response(text=json.dumps(response_doc), status=200) +async def get_user(request): + # user_id_find_request + user_id = request.match_info.get('user_id') + # check if _id exists + user_data = await m_client.do_find_one(user_id) + if not user_data: + return web.HTTPNotFound(text='Invalid _id !') + return web.Response(text=json.dumps(user_data), status=200) -async def update_user(user_data): - d = await user_data.json() - print(d) - await m_client.do_update(d) - response_doc = {'status4': 'success'} - return Response(text=json.dumps(response_doc), status=200) +async def delete_user(request): + # user_id_delete_request + user_id = request.match_info.get('user_id') + # check if _id exists + user_data = await m_client.do_find_one(user_id) + if not user_data: + return web.HTTPNotFound(text='Invalid _id !') + await m_client.do_del(user_data) + return web.Response(text='Deleted !', status=200) -async def delete_user(user_id): - d = await user_id.json() - await m_client.do_del(d) - response_doc = {'status3': 'success'} - return Response(text=json.dumps(response_doc), status=200) +# Not complete ! +async def update_user(request): + # user_id_update_request + user_id = request.match_info.get('user_id') + user_data = await request.json() + data = dict(user_data) + print(data) + doc = await m_client.do_find_one(user_id) + doc_data = doc.get('f_name', 'l_name') + print(doc_data) + if not doc: + return web.HTTPNotFound(text='Invalid _id !') + '''if f_name is None: + f_name = doc.get("f_name") + await m_client.do_update(user_id, f_name, l_name) + elif l_name is None: + l_name = doc.get("l_name") + await m_client.do_update(user_id, f_name, l_name) + else:''' + await m_client.do_update(user_id, data) + return Response(text='Updated !', status=200) -async def add_user(user_data): - d = await user_data.json() - print(d) - await m_client.do_insert(d) - response_doc = {'status': 'success'} - return Response(text=json.dumps(response_doc), status=200) +""" +async def count_user(request): + d = await m_client.do_count() + return Response(text=json.dumps(d), status=200) +""" diff --git a/mongo/client.py b/mongo/client.py index 9c8b1aa..e32c7b3 100644 --- a/mongo/client.py +++ b/mongo/client.py @@ -1,4 +1,5 @@ import motor + from motor.motor_asyncio import AsyncIOMotorClient m_client = motor.motor_asyncio.AsyncIOMotorClient() @@ -7,38 +8,33 @@ collection = db.m1c -async def do_insert(doc): - result = await collection.insert(doc) - return result - - -async def do_find_one(d): - document = await collection.find_one(d) - print(document) +async def do_insert(user_data): + document = await collection.insert(user_data) + return document -async def do_count(): - cnt = await collection.find().count() - print('%s Count of documents ' % cnt) - return cnt - +async def do_find_one(user_id): + document = await collection.find_one(user_id) + return document -async def do_del(d): - n = await collection.count() - print('%s Documents before calling del()' % n) - result = await collection.delete_one(d) - print('%s Documents after!' % (await collection.count())) - return result +async def do_del(user_id): + await collection.delete_one(user_id) -async def do_update(d): - id = await collection.find_one() - print(id) - result = await collection.update_one({"_id": 1}, {'$set': d}) - print(result) - return result +async def do_update(user_id, user_data): + await collection.update({"_id": user_id}, user_data) +""" +async def do_find_all(): + doc = await collection.find({}) + print(doc) + return doc +async def do_count(): + cnt = await collection.find().count() + print('%s Count of documents ' % cnt) + return cnt +""" diff --git a/server.py b/server.py index 7d511d1..551f16a 100644 --- a/server.py +++ b/server.py @@ -6,13 +6,11 @@ def run(): """ Starts the Server """ - app = web.Application() - app.router.add_get('/user', handler.handler) - app.router.add_post('/add/user', handler.add_user) - app.router.add_get('/find/user', handler.get_user) - app.router.add_put('/update/user', handler.update_user) - app.router.add_delete('/delete/user', handler.delete_user) + app.router.add_get('/api/user/{user_id}', handler.get_user) + app.router.add_post('/api/user', handler.add_user) + app.router.add_put('/api/user/{user_id}', handler.update_user) + app.router.add_delete('/api/user/{user_id}', handler.delete_user) web.run_app(app) diff --git a/tests/test_handler.py b/tests/test_handler.py index e69de29..20beddc 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -0,0 +1,12 @@ +# GET all users +# curl -X GET http://:/api/user + +# GET user with matching +# curl -X GET http://:/api/user/ + +# CREATE user +# curl -X POST http://:/api/user -d {} + +# curl -X DELETE http://:/api/user/ +# +# curl -X PUT http://:/api/user/ -d {} From 9d7d87c27e115afc383b29b64bd7440b8294676f Mon Sep 17 00:00:00 2001 From: Yaashika Date: Tue, 22 May 2018 15:08:49 +0530 Subject: [PATCH 05/11] Syslog_changes Signed-off-by: Yaashika --- api/handler.py | 24 ++++++++++++++++++++---- requirements.txt | 4 +++- server.py | 2 ++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/api/handler.py b/api/handler.py index b9abb44..e4e79e0 100644 --- a/api/handler.py +++ b/api/handler.py @@ -1,4 +1,4 @@ -import json +import ujson from aiohttp import web @@ -6,39 +6,55 @@ from mongo import client as m_client +import logging +logging.basicConfig(level=logging.INFO, + format='%(asctime)s %(levelname)s %(message)s') +logger = logging.getLogger(__name__) + async def add_user(request): + logger.info('READING THE DATA...') # user_id_insert_request user_data = await request.json() user_id = user_data.get("_id") # check if _id already exists check_id = await m_client.do_find_one(user_id) if not check_id: + logger.info('ADDING RECORDS...') inserted_data = await m_client.do_insert(user_data) + logger.info('INSERTED DATA...') # find the inserted_data find_data = await m_client.do_find_one(inserted_data) - return Response(text=json.dumps(find_data), status=200) + logger.debug('RECORDS: %s', find_data) + return Response(text=ujson.dumps(find_data), status=200) + logger.error('_ID ALREADY EXISTS...') return Response(text='_id already exists !') async def get_user(request): + logger.info('READING THE _id...') # user_id_find_request user_id = request.match_info.get('user_id') # check if _id exists user_data = await m_client.do_find_one(user_id) if not user_data: + logging.error('INVALID _id %s !', user_id) return web.HTTPNotFound(text='Invalid _id !') - return web.Response(text=json.dumps(user_data), status=200) + logger.debug('RECORDS: %s', user_data) + return web.Response(text=ujson.dumps(user_data), status=200) async def delete_user(request): + logger.info('READING DATA...') # user_id_delete_request user_id = request.match_info.get('user_id') # check if _id exists user_data = await m_client.do_find_one(user_id) if not user_data: + logger.error('INVALID _id %s !', user_id) return web.HTTPNotFound(text='Invalid _id !') await m_client.do_del(user_data) + logger.info('DATA DELETED...') return web.Response(text='Deleted !', status=200) @@ -46,7 +62,7 @@ async def delete_user(request): async def update_user(request): # user_id_update_request user_id = request.match_info.get('user_id') - user_data = await request.json() + user_data = await request.ujson() data = dict(user_data) print(data) doc = await m_client.do_find_one(user_id) diff --git a/requirements.txt b/requirements.txt index aee9d99..f26e6ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ -aiohttp== 3.1.3 +ujson + motor==1.2.1 +aiohttp==3.1.3 diff --git a/server.py b/server.py index 551f16a..b1b31c1 100644 --- a/server.py +++ b/server.py @@ -1,11 +1,13 @@ #!/usr/bin/env python3 from aiohttp import web + from api import handler def run(): """ Starts the Server """ + app = web.Application() app.router.add_get('/api/user/{user_id}', handler.get_user) app.router.add_post('/api/user', handler.add_user) From 080c17d12159c50503ceb1a51d3acc5dfb1cc755 Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 25 May 2018 17:56:51 +0530 Subject: [PATCH 06/11] Logging_config/json Signed-off-by: Yaashika --- api/handler.py | 48 ++++++++++++++++++++++++++++-------------------- server.py | 3 +++ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/api/handler.py b/api/handler.py index e4e79e0..55de542 100644 --- a/api/handler.py +++ b/api/handler.py @@ -1,61 +1,69 @@ import ujson -from aiohttp import web - from aiohttp.web import Response from mongo import client as m_client -import logging -logging.basicConfig(level=logging.INFO, - format='%(asctime)s %(levelname)s %(message)s') +import logging.config + +import json + +from os import path, remove + +if path.isfile("logging_config.log"): + remove("logging_config.log") + +with open("app.json", 'r') as f: + config_dict = json.load(f) +logging.config.dictConfig(config_dict) logger = logging.getLogger(__name__) +logger.info("Logging!") async def add_user(request): - logger.info('READING THE DATA...') + logging.info('READING THE DATA...') # user_id_insert_request user_data = await request.json() user_id = user_data.get("_id") # check if _id already exists check_id = await m_client.do_find_one(user_id) if not check_id: - logger.info('ADDING RECORDS...') + logging.info('ADDING RECORDS...') inserted_data = await m_client.do_insert(user_data) - logger.info('INSERTED DATA...') + logging.info('INSERTED DATA...') # find the inserted_data find_data = await m_client.do_find_one(inserted_data) - logger.debug('RECORDS: %s', find_data) + logging.debug('RECORDS: %s', find_data) return Response(text=ujson.dumps(find_data), status=200) - logger.error('_ID ALREADY EXISTS...') + logging.error('_ID ALREADY EXISTS...') return Response(text='_id already exists !') async def get_user(request): - logger.info('READING THE _id...') + logging.info('READING THE _id...') # user_id_find_request user_id = request.match_info.get('user_id') # check if _id exists user_data = await m_client.do_find_one(user_id) if not user_data: logging.error('INVALID _id %s !', user_id) - return web.HTTPNotFound(text='Invalid _id !') - logger.debug('RECORDS: %s', user_data) - return web.Response(text=ujson.dumps(user_data), status=200) + return Response(text='Invalid _id !') + logging.debug('RECORDS: %s', user_data) + return Response(text=ujson.dumps(user_data), status=200) async def delete_user(request): - logger.info('READING DATA...') + logging.info('READING DATA...') # user_id_delete_request user_id = request.match_info.get('user_id') # check if _id exists user_data = await m_client.do_find_one(user_id) if not user_data: - logger.error('INVALID _id %s !', user_id) - return web.HTTPNotFound(text='Invalid _id !') + logging.error('INVALID _id %s !', user_id) + return Response(text='Invalid _id !') await m_client.do_del(user_data) - logger.info('DATA DELETED...') - return web.Response(text='Deleted !', status=200) + logging.info('DATA DELETED...') + return Response(text='Deleted !', status=200) # Not complete ! @@ -69,7 +77,7 @@ async def update_user(request): doc_data = doc.get('f_name', 'l_name') print(doc_data) if not doc: - return web.HTTPNotFound(text='Invalid _id !') + return Response(text='Invalid _id !') '''if f_name is None: f_name = doc.get("f_name") await m_client.do_update(user_id, f_name, l_name) diff --git a/server.py b/server.py index b1b31c1..737c7ea 100644 --- a/server.py +++ b/server.py @@ -9,6 +9,7 @@ def run(): """ Starts the Server """ app = web.Application() + app.router.add_get('/api/user/{user_id}', handler.get_user) app.router.add_post('/api/user', handler.add_user) app.router.add_put('/api/user/{user_id}', handler.update_user) @@ -18,3 +19,5 @@ def run(): run() + + From 2298fa10bc981272c60a150cab210c4266dd8a0d Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 25 May 2018 18:03:07 +0530 Subject: [PATCH 07/11] json_file --- app.json | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app.json diff --git a/app.json b/app.json new file mode 100644 index 0000000..8a1935f --- /dev/null +++ b/app.json @@ -0,0 +1,22 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "simple": { + "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + } + }, + "handlers": { + "file_handler": { + "class": "logging.FileHandler", + "level": "DEBUG", + "formatter": "simple", + "filename": "logging_config.log", + "encoding": "utf8" + } + }, + "root": { + "level": "DEBUG", + "handlers": ["file_handler"] + } +} From d9f9d0a1019280e155feafa395d4c88620f6fe8d Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 25 May 2018 18:03:59 +0530 Subject: [PATCH 08/11] sample_logging --- logging_config.log | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 logging_config.log diff --git a/logging_config.log b/logging_config.log new file mode 100644 index 0000000..f43da97 --- /dev/null +++ b/logging_config.log @@ -0,0 +1,4 @@ +2018-05-25 17:56:12,378 - api.handler - INFO - Logging! +2018-05-25 17:56:26,401 - root - INFO - READING THE _id... +2018-05-25 17:56:26,406 - root - ERROR - INVALID _id 8 ! +2018-05-25 17:56:26,408 - aiohttp.access - INFO - 127.0.0.1 [25/May/2018:12:26:26 +0000] "GET /api/user/8 HTTP/1.1" 200 164 "-" "curl/7.54.0" From d3a682f5d5e4ee32531712c2a7f3f744aac69773 Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 1 Jun 2018 13:58:20 +0530 Subject: [PATCH 09/11] SysLogging Signed-off-by: Yaashika --- api/handler.py | 15 +-------------- app.json | 21 +++++++++++++-------- logging_config.log | 4 ---- server.py | 4 +++- 4 files changed, 17 insertions(+), 27 deletions(-) delete mode 100644 logging_config.log diff --git a/api/handler.py b/api/handler.py index 55de542..023fe94 100644 --- a/api/handler.py +++ b/api/handler.py @@ -4,20 +4,7 @@ from mongo import client as m_client -import logging.config - -import json - -from os import path, remove - -if path.isfile("logging_config.log"): - remove("logging_config.log") - -with open("app.json", 'r') as f: - config_dict = json.load(f) -logging.config.dictConfig(config_dict) -logger = logging.getLogger(__name__) -logger.info("Logging!") +import logging async def add_user(request): diff --git a/app.json b/app.json index 8a1935f..59ac5b5 100644 --- a/app.json +++ b/app.json @@ -3,20 +3,25 @@ "disable_existing_loggers": false, "formatters": { "simple": { - "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s", + "date_fmt": "%m-%d-%Y %H:%M:%S" } }, "handlers": { - "file_handler": { - "class": "logging.FileHandler", - "level": "DEBUG", + "SysLog": { + "class": "logging.handlers.SysLogHandler", "formatter": "simple", - "filename": "logging_config.log", - "encoding": "utf8" + "address": "/var/run/syslog", + "level": "DEBUG", + "facility": "LOG_SYSLOG" } }, - "root": { + "loggers": { + "handlers": ["SysLog"], "level": "DEBUG", - "handlers": ["file_handler"] + "propagate": false } } + + + diff --git a/logging_config.log b/logging_config.log deleted file mode 100644 index f43da97..0000000 --- a/logging_config.log +++ /dev/null @@ -1,4 +0,0 @@ -2018-05-25 17:56:12,378 - api.handler - INFO - Logging! -2018-05-25 17:56:26,401 - root - INFO - READING THE _id... -2018-05-25 17:56:26,406 - root - ERROR - INVALID _id 8 ! -2018-05-25 17:56:26,408 - aiohttp.access - INFO - 127.0.0.1 [25/May/2018:12:26:26 +0000] "GET /api/user/8 HTTP/1.1" 200 164 "-" "curl/7.54.0" diff --git a/server.py b/server.py index 737c7ea..186c38b 100644 --- a/server.py +++ b/server.py @@ -3,13 +3,14 @@ from aiohttp import web from api import handler +from logger import MyLogger def run(): """ Starts the Server """ app = web.Application() - + MyLogger().setup() app.router.add_get('/api/user/{user_id}', handler.get_user) app.router.add_post('/api/user', handler.add_user) app.router.add_put('/api/user/{user_id}', handler.update_user) @@ -21,3 +22,4 @@ def run(): run() + From 86413c0c693e6f2b779c85f4920a834401a7222d Mon Sep 17 00:00:00 2001 From: Yaashika Date: Fri, 1 Jun 2018 14:01:25 +0530 Subject: [PATCH 10/11] MyLogger --- logger.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 logger.py diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..0ce2128 --- /dev/null +++ b/logger.py @@ -0,0 +1,32 @@ +import json +import logging.config +from logging.handlers import SysLogHandler + + +class MyLogger: + + @staticmethod + def setup(logger_name: str=None): + + # loading_data_from_json_file + with open("app.json", "r", encoding="utf-8") as fd: + data = json.load(fd) + + # LOG_SYSLOG + syslog_loaded = data["handlers"]["SysLog"] + syslog_address = syslog_loaded["address"] + syslog_level = syslog_loaded["level"] + formatter = logging.Formatter(fmt=data["formatters"]["simple"]["format"], + datefmt=data["formatters"]["simple"]["date_fmt"]) + propagate = data["loggers"]["propagate"] + + # accessing_the_loaded_json_file_SYSLOG + logger = logging.getLogger(logger_name) + handler = SysLogHandler(address=syslog_address) + handler.setFormatter(formatter) + logger.setLevel(syslog_level) + logger.propagate = propagate + logger.addHandler(handler) + + + From 8a2e2ede394ee9a12c9532e1efed777317525b77 Mon Sep 17 00:00:00 2001 From: Yaashika Date: Tue, 5 Jun 2018 13:33:11 +0530 Subject: [PATCH 11/11] Update_added Signed-off-by: Yaashika --- api/handler.py | 48 ++++++++++++++++++++++++------------------------ logger.py | 5 +++-- mongo/client.py | 11 +++++------ server.py | 2 +- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/api/handler.py b/api/handler.py index 023fe94..3dda286 100644 --- a/api/handler.py +++ b/api/handler.py @@ -1,9 +1,8 @@ -import ujson +#!/usr/bin/env python3 +import ujson from aiohttp.web import Response - from mongo import client as m_client - import logging @@ -30,49 +29,50 @@ async def get_user(request): logging.info('READING THE _id...') # user_id_find_request user_id = request.match_info.get('user_id') - # check if _id exists - user_data = await m_client.do_find_one(user_id) - if not user_data: - logging.error('INVALID _id %s !', user_id) - return Response(text='Invalid _id !') - logging.debug('RECORDS: %s', user_data) - return Response(text=ujson.dumps(user_data), status=200) + + # to get all documents in the collection + if user_id == '0': + data = await m_client.do_find_all() + logging.info(data) + return Response(text=ujson.dumps(data, indent=1), status=200) + # to get mentioned _id + else: + # check if _id exists + user_data = await m_client.do_find_one(user_id) + if not user_data: + logging.error('INVALID _id %s!', user_id) + return Response(text='Invalid _id !') + logging.debug('RECORDS: %s', user_data) + return Response(text=ujson.dumps(user_data), status=200) async def delete_user(request): - logging.info('READING DATA...') + logging.info('READING THE _id...') # user_id_delete_request user_id = request.match_info.get('user_id') # check if _id exists user_data = await m_client.do_find_one(user_id) if not user_data: - logging.error('INVALID _id %s !', user_id) + logging.error('INVALID _id %s!', user_id) return Response(text='Invalid _id !') await m_client.do_del(user_data) logging.info('DATA DELETED...') return Response(text='Deleted !', status=200) -# Not complete ! async def update_user(request): + logging.info('READING THE _id...') # user_id_update_request user_id = request.match_info.get('user_id') - user_data = await request.ujson() + user_data = await request.json() data = dict(user_data) - print(data) + # check if _id exists doc = await m_client.do_find_one(user_id) - doc_data = doc.get('f_name', 'l_name') - print(doc_data) if not doc: + logging.error('INVALID _id %s!', user_id) return Response(text='Invalid _id !') - '''if f_name is None: - f_name = doc.get("f_name") - await m_client.do_update(user_id, f_name, l_name) - elif l_name is None: - l_name = doc.get("l_name") - await m_client.do_update(user_id, f_name, l_name) - else:''' await m_client.do_update(user_id, data) + logging.info('DATA UPDATED...') return Response(text='Updated !', status=200) diff --git a/logger.py b/logger.py index 0ce2128..08afcb3 100644 --- a/logger.py +++ b/logger.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import json import logging.config from logging.handlers import SysLogHandler @@ -28,5 +30,4 @@ def setup(logger_name: str=None): logger.propagate = propagate logger.addHandler(handler) - - +# called in server.py diff --git a/mongo/client.py b/mongo/client.py index e32c7b3..fc7bc02 100644 --- a/mongo/client.py +++ b/mongo/client.py @@ -1,7 +1,7 @@ -import motor +#!/usr/bin/env python3 +import motor from motor.motor_asyncio import AsyncIOMotorClient - m_client = motor.motor_asyncio.AsyncIOMotorClient() db = m_client.m1 @@ -23,16 +23,15 @@ async def do_del(user_id): async def do_update(user_id, user_data): - await collection.update({"_id": user_id}, user_data) + await collection.update({"_id": user_id}, {'$set': user_data}) -""" async def do_find_all(): - doc = await collection.find({}) - print(doc) + doc = collection.find() return doc +""" async def do_count(): cnt = await collection.find().count() print('%s Count of documents ' % cnt) diff --git a/server.py b/server.py index 186c38b..d710b4f 100644 --- a/server.py +++ b/server.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 from aiohttp import web - from api import handler from logger import MyLogger @@ -11,6 +10,7 @@ def run(): app = web.Application() MyLogger().setup() + app.router.add_get('/api/user/{user_id}', handler.get_user) app.router.add_post('/api/user', handler.add_user) app.router.add_put('/api/user/{user_id}', handler.update_user)