5
5
#
6
6
7
7
import logging
8
- from urllib .request import urlopen
9
- from urllib .error import URLError
10
8
import logging .handlers
11
- from flask import Flask , jsonify , request
12
- from marshmallow import Schema , fields , ValidationError
13
9
from redis_benchmarks_specification import __version__
14
- from json import dumps , loads
15
10
import redis
16
11
import argparse
17
- from flask_httpauth import HTTPBasicAuth
18
12
13
+ from redis_benchmarks_specification .__api__ .app import create_app
19
14
from redis_benchmarks_specification .__common__ .env import (
20
- STREAM_KEYNAME_GH_EVENTS_COMMIT ,
21
15
GH_REDIS_SERVER_HOST ,
22
16
GH_REDIS_SERVER_PORT ,
23
17
GH_REDIS_SERVER_AUTH ,
32
26
get_version_string ,
33
27
)
34
28
35
- auth = HTTPBasicAuth ()
36
-
37
- app = Flask (__name__ )
38
- conn = None
39
-
40
-
41
- @auth .verify_password
42
- def verify_password (username , password ):
43
- result = False
44
- try :
45
- auth_server_conn = redis .StrictRedis (
46
- host = REDIS_AUTH_SERVER_HOST ,
47
- port = REDIS_AUTH_SERVER_PORT ,
48
- decode_responses = True ,
49
- username = username ,
50
- password = password ,
51
- )
52
- auth_server_conn .ping ()
53
- result = True
54
- except redis .exceptions .ResponseError :
55
- result = False
56
- except redis .exceptions .AuthenticationError :
57
- result = False
58
- return result
59
-
60
-
61
- def commit_schema_to_stream (json_str : str , conn : redis .StrictRedis ):
62
- """ uses to the provided JSON dict of fields and pushes that info to the corresponding stream """
63
- fields = loads (json_str )
64
- reply_fields = loads (json_str )
65
- result = False
66
- error_msg = None
67
- if "git_hash" not in fields :
68
- error_msg = "Missing required 'git_hash' field"
69
- else :
70
- github_url = "https://github.com/redis/redis/archive/{}.zip" .format (
71
- fields ["git_hash" ]
72
- )
73
- try :
74
- response = urlopen (github_url , timeout = 5 )
75
- content = response .read ()
76
- fields ["zip_archive" ] = bytes (content )
77
- fields ["zip_archive_len" ] = len (bytes (content ))
78
- reply_fields ["archived_zip" ] = True
79
- result = True
80
- except URLError as e :
81
- error_msg = "Catched URLError while fetching {} content. Error {}" .format (
82
- github_url , e .__str__ ()
83
- )
84
- logging .error (error_msg )
85
- result = False
86
-
87
- if result is True :
88
- id = conn .xadd (STREAM_KEYNAME_GH_EVENTS_COMMIT .encode (), fields )
89
- reply_fields ["id" ] = id
90
-
91
- return result , reply_fields , error_msg
92
-
93
-
94
- class CommitSchema (Schema ):
95
- git_branch = fields .String (required = False )
96
- git_tag = fields .String (required = False )
97
- git_hash = fields .String (required = True )
98
-
99
-
100
- @app .route ("/api/gh/redis/redis/commits" , methods = ["POST" ])
101
- @auth .login_required
102
- def base ():
103
- # Get Request body from JSON
104
- request_data = request .json
105
- schema = CommitSchema ()
106
- try :
107
- # Validate request body against schema data types
108
- result = schema .load (request_data )
109
- except ValidationError as err :
110
- # Return a nice message if validation fails
111
- return jsonify (err .messages ), 400
112
-
113
- # Convert request body back to JSON str
114
- data_now_json_str = dumps (result )
115
-
116
- result , response_data , err_message = commit_schema_to_stream (
117
- data_now_json_str , conn
118
- )
119
- if result is False :
120
- return jsonify (err_message ), 400
121
-
122
- # Send data back as JSON
123
- return jsonify (response_data ), 200
124
-
125
29
126
30
def main ():
127
31
_ , _ , project_version = populate_with_poetry_data ()
@@ -146,7 +50,13 @@ def main():
146
50
REDIS_AUTH_SERVER_HOST , REDIS_AUTH_SERVER_PORT
147
51
)
148
52
)
149
-
53
+ conn = redis .StrictRedis (
54
+ host = GH_REDIS_SERVER_HOST ,
55
+ port = GH_REDIS_SERVER_PORT ,
56
+ decode_responses = True ,
57
+ password = GH_REDIS_SERVER_AUTH ,
58
+ )
59
+ app = create_app (conn )
150
60
if args .logname is not None :
151
61
print ("Writting log to {}" .format (args .logname ))
152
62
handler = logging .handlers .RotatingFileHandler (
@@ -169,13 +79,6 @@ def main():
169
79
GH_REDIS_SERVER_HOST , GH_REDIS_SERVER_PORT
170
80
)
171
81
)
172
- conn = redis .StrictRedis (
173
- host = GH_REDIS_SERVER_HOST ,
174
- port = GH_REDIS_SERVER_PORT ,
175
- decode_responses = True ,
176
- password = GH_REDIS_SERVER_AUTH ,
177
- )
178
-
179
82
app .run (host = "0.0.0.0" )
180
83
181
84
0 commit comments