-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·124 lines (96 loc) · 4.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Filename: app.py
# Author: Osei Seraphin
# Course: IST 440w
# Instructor: Professor Oakes
import sys
sys.path.append('..')
import subprocess
import jwt
from Token import web_token
from kerberos import kerberosAuthentication
from messageQueuing import ceRabbitMqPushMessage
from messageQueuing import clientRabbitMqPickupMessage
from ConstantValues.Constants import constantsclass
from apiData.computation import ComputationClass
from apiData import apiParsing
from queries import queryBuilder
from Validators import jishinValidator
from Errors import ValidationErrors
from jishinLogger import LoggingFinal as jishinLogging
class Engine:
def login(self, username, password):
try:
# Authenticate and authorize user
tokenhandler = web_token.tokenHandler()
kerberoshandler = kerberosAuthentication.kerberosHandler()
ticket = kerberoshandler.has_kerberos_ticket(username, password)
if ticket:
token = tokenhandler.create_token(username, ticket)
return token
else:
return constantsclass.INCORRECT_PASSWORD
except Exception as e:
jishinLogging.logger.error('Error Logging In: %s' % e)
def createPrediction(self, token, region, predictionType, date):
username = jwt.decode(token, 'secret', algorithms='HS256').get('username')
claims = jwt.decode(token, 'secret', algorithms='HS256').get('claim')
currentDate = jwt.decode(token, 'secret', algorithms='HS256').get('dateIssued')
region = region.upper()
predictionType = predictionType.upper()
if constantsclass.WEB_SERVICE in claims:
validator = jishinValidator.Input_Validator()
try:
# validates user input before passing it into query builder
validator.validate_date(currentDate, date)
validator.validate_region(region)
validator.validate_prediction_type(predictionType)
# Retrieve correct collection from db to make computation
query = queryBuilder.queryBuilder()
collection = query.retrieveCollection(region, predictionType)
try:
# computation
computationHandler = ComputationClass()
results = str(computationHandler.computationalCalculations(collection, predictionType, date))
# rabbitMq
messageQueue = ceRabbitMqPushMessage.messageQueue()
messageQueue.sendMessage(username, results, date, region, predictionType)
except Exception as e:
jishinLogging.logger.error('Computation %s' % e)
return True
except ValidationErrors.InputError as ve:
jishinLogging.logger.error('Validation Error %s' % ve)
for e in validator.errors:
jishinLogging.logger.error(e)
print ve.msg
return False
except Exception as e:
jishinLogging.logger.error('Error Creating Prediction %s' %e)
else:
return False
def receiveResults(self, token):
username = jwt.decode(token, 'secret', algorithms='HS256').get('username')
claims = jwt.decode(token, 'secret', algorithms='HS256').get('claim')
if constantsclass.WEB_SERVICE in claims:
try:
messageQueue = ''
messageQueue = clientRabbitMqPickupMessage.messageReceive()
results = messageQueue.getMessage(username)
jishinLogging.logger.info('Results Returned To %s' % username)
return results
except Exception as e:
jishinLogging.logger.error('Error Recieving Results: %s' % e)
else:
invalidUser = ['You are not authorized to use this service']
return invalidUser
def apiUpdate(self, token):
username = jwt.decode(token, 'secret', algorithms='HS256').get('username')
claims = jwt.decode(token, 'secret', algorithms='HS256').get('claim')
if constantsclass.API_UPDATE in claims:
try:
updater = apiParsing.APIParse()
updater.apiCollectionUpdate()
return True
except Exception as e:
jishinLogging.logger.error('Error Running Update: %s by User: %s' % (e, username))
else:
return False