-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentinel.py
157 lines (140 loc) · 5.4 KB
/
sentinel.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import urlparse
import json
import os
from requests.exceptions import ConnectionError, Timeout
from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import NotImplemented
from endpoint.utils import (dispatch_request, send_notification, make_request,
render_response)
from endpoint.settings import SMTP_STATUS, SPECS_PATH
from endpoint.readers.yaml_reader import YamlReader
class Sentinel(object):
"""
Sentinel will be in charge of making requests using the
endpoints defined in the .yml file.
"""
def __init__(self):
self.url_map = Map([
Rule('/', endpoint='invalid_request'),
Rule('/<spec>/', endpoint='get_batch_of_responses'),
Rule('/<spec>/<int:id>', endpoint='get_response'),
Rule('/<spec>/<alias>', endpoint='get_response')
])
def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response)
def wsgi_app(self, environ, start_response):
request = Request(environ)
response = dispatch_request(self, request)
return response(environ, start_response)
def is_valid_url(self, endpoint):
"""
Check is the passed url is valid
"""
parts = urlparse.urlparse(endpoint)
return parts.scheme in ('http','https')
def should_retry(self, endpoint, counter):
"""
Check if the sentinel should continue trying to
get a proper response from the endpoint. This value
is passed in the .yml file.
"""
return counter == endpoint['config']['retries']
def verify_status_code(self, endpoint, response):
"""
Verify if the status code is the right one.
"""
result = False
if response.status_code == endpoint['asserts']['status-code']:
result = True
return result
def process_call(self, endpoint):
"""
Process a single request and build a response.
"""
if self.is_valid_url(endpoint['url']):
try:
response = make_request(endpoint)
except ConnectionError:
return render_response(error='SERVER_UNREACHABLE',
status_code=500,
obj=endpoint)
except NotImplemented:
return render_response(error='NOT_IMPLEMENTED',
status_code=501,
obj=endpoint)
except Timeout:
return render_response(error='REQUEST_TIMEOUT',
status_code=408,
obj=endpoint)
else:
tests_passed = False
counter = 0
while True:
if self.verify_status_code(endpoint, response):
tests_passed = True
break
elif self.should_retry(endpoint, counter):
counter += 1
else:
if not self.test_mode and SMTP_STATUS:
send_notification()
break
else:
return render_response(error='BAD_REQUEST',
status_code=400,
obj=endpoint)
return render_response(status_code=response.status_code,
response=response,
tests_passed=tests_passed)
def get_batch_of_responses(self, request, **values):
"""
Get a batch of responses from the respective requests.
"""
cube = []
limit = None
try:
reader = YamlReader(values['spec'])
except IOError:
pass
else:
endpoints = reader.get_calls()
if 'limit' in dict(request.args).keys():
limit = int(request.args['limit'])
for endpoint in endpoints:
if limit != None:
limit = limit - 1
if limit == -1:
break
cube.append(self.process_call(endpoint))
return Response(json.dumps(cube))
def get_response(self, request, **values):
"""
Get a single response from a single request.
"""
block = {}
counter = 0
try:
reader = YamlReader(values['spec'])
except IOError:
pass
else:
endpoints = reader.get_calls()
if 'id' in values and values.get('id', 0) > 0:
try:
endpoint = endpoints[values['id'] - 1]
except IndexError:
pass
else:
block = self.process_call(endpoint)
elif 'alias' in values:
while counter < len(endpoints):
endpoint = endpoints[counter]
if endpoint['alias'] == values['alias']:
block = self.process_call(endpoint)
break
counter += 1
return Response(json.dumps(block))
def invalid_request(self, request, **values):
block = {}
return Response(json.dumps(block))