Skip to content
Snippets Groups Projects
Commit 0171f5bd authored by Marco Malavolti's avatar Marco Malavolti
Browse files

Enriched API

parent 16a64574
No related branches found
No related tags found
No related merge requests found
...@@ -4,27 +4,97 @@ from flask import Flask, request ...@@ -4,27 +4,97 @@ from flask import Flask, request
from flask_restful import Resource, Api from flask_restful import Resource, Api
from json import dumps from json import dumps
from flask import jsonify from flask import jsonify
from pathlib import PurePath
import logging
from logging.handlers import RotatingFileHandler
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
def getLogger(filename,log_level="DEBUG",path="./"):
logger = logging.getLogger(filename)
ch = logging.FileHandler(path+filename,'w','utf-8')
if (log_level == "DEBUG"):
logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
elif (log_level == "INFO"):
logger.setLevel(logging.INFO)
ch.setLevel(logging.INFO)
elif (log_level == "WARN"):
logger.setLevel(logging.WARN)
ch.setLevel(logging.WARN)
elif (log_level == "ERROR"):
logger.setLevel(logging.ERROR)
ch.setLevel(logging.ERROR)
elif (log_level == "CRITICAL"):
logger.setLevel(logging.CRITICAL)
ch.setLevel(logging.CRITICAL)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
class Test(Resource): class Test(Resource):
def get(self): def get(self):
return {'test': 'ciao'} # Fetches first column that is Employee ID app.logger.info("Test Superato!")
return {'test':'It Works!'}
class EccsCheck(Resource):
def get(self, idp):
result = { 'sp':'https://sp24-test.garr.it/secure',
'idp':idp,
'check_result':'OK',
'date':'19-02-2020'
}
return jsonify(result)
api.add_resource(Test, '/test') # Route_1 class AllChecks(Resource):
api.add_resource(EccsCheck, '/eccs/<idp>') # Route_3 def get(self):
app.logger.info("Richiesta 'AllChecks'")
file_path = "logs/eccs2checks_2020-02-19.log"
fo = open(file_path, "r")
result = []
date = PurePath(file_path).parts[-1].split('_')[1].split('.')[0]
lines = fo.readlines()
for line in lines:
check = line.split(";")
idp = check[0]
sp = check[1]
check_result = check[2]
result.append( { 'sp' : sp,
'idp' : idp,
'check_result' : check_result.rstrip("\n\r"),
'date': date
} )
return jsonify(result)
class ChecksByStatus(Resource):
def get(self,status):
file_path = "logs/eccs2checks_2020-02-19.log"
fo = open(file_path, "r")
result = []
date = PurePath(file_path).parts[-1].split('_')[1].split('.')[0]
lines = fo.readlines()
for line in lines:
check_status = line.split(';')[2].rstrip("\n\r")
if (status == check_status):
check = line.split(";")
idp = check[0]
sp = check[1]
check_result = check[2]
result.append( { 'sp' : sp,
'idp' : idp,
'check_result' : check_result.rstrip("\n\r"),
'date': date
} )
return jsonify(result)
api.add_resource(Test, '/eccs/test') # Route_1
api.add_resource(AllChecks, '/eccs/checks/all') # Route_2
api.add_resource(ChecksByStatus, '/eccs/checks/<status>') # Route_3
if __name__ == '__main__': if __name__ == '__main__':
app.run(port='5002')
app.logger = getLogger("logs/eccs2api.log","INFO")
app.run(port='5002')
...@@ -6,6 +6,7 @@ from selenium.webdriver.support.ui import Select ...@@ -6,6 +6,7 @@ from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import TimeoutException
from datetime import date
import logging import logging
...@@ -128,8 +129,8 @@ def getLogger(filename,log_level="DEBUG",path="./"): ...@@ -128,8 +129,8 @@ def getLogger(filename,log_level="DEBUG",path="./"):
if __name__=="__main__": if __name__=="__main__":
eccs2log = getLogger("logs/eccs2.log","INFO") eccs2log = getLogger("logs/eccs2_"+date.today().isoformat()+".log","INFO")
eccs2checks = getLogger("logs/eccs2checks.log","INFO") eccs2checksLog = getLogger("logs/eccs2checks_"+date.today().isoformat()+".log","INFO")
driver = setup() driver = setup()
...@@ -148,7 +149,7 @@ if __name__=="__main__": ...@@ -148,7 +149,7 @@ if __name__=="__main__":
for idp in listIdPs: for idp in listIdPs:
result = [] result = []
for sp in sps: for sp in sps:
result.append(checkIdP(driver,sp,idp,eccs2checks)) result.append(checkIdP(driver,sp,idp,eccs2checksLog))
# se tutti e 2 i check sono andati bene, allora l'IdP è OK, altrimenti no. # se tutti e 2 i check sono andati bene, allora l'IdP è OK, altrimenti no.
if (result[0] == result[1] == "OK"): if (result[0] == result[1] == "OK"):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment