-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_api_test.py
More file actions
70 lines (56 loc) · 2.17 KB
/
web_api_test.py
File metadata and controls
70 lines (56 loc) · 2.17 KB
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
from flask import Flask
from flask_restful import Api, Resource, reqparse, abort
from dotenv import load_dotenv
import os
from ecdsa import SigningKey
load_dotenv()
port = os.getenv('FLASK_RUN_PORT')
app = Flask(__name__)
api = Api(app)
@app.route('/')
def hello():
return 'Hello, Karma Tester!'
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0', port=port)
def validate_request(request):
auth_header = request.headers.get('Authorization')
if not auth_header:
abort(401, message='Unauthorized')
account = auth_header.split(' ')[-1]
if not account:
abort(401, message='Unauthorized')
if request.headers.get('Content-Type') != 'application/json':
abort(400, message='Bad Request')
json_data = request.get_json()
if not json_data:
abort(400, message='Bad Request')
if 'chatbotName' not in json_data:
abort(400, message='Bad Request')
signature = json_data.get('signature', '')
if not signature:
abort(400, message='Bad Request')
data = {k: v for k, v in json_data.items() if k != 'signature'}
data_str = json.dumps(data, sort_keys=True)
vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(account), curve=ecdsa.SECP256k1)
try:
vk.verify(bytes.fromhex(signature), data_str.encode('utf-8'))
except ecdsa.BadSignatureError:
abort(401, message='Unauthorized')
class MessageBot(Resource):
def post(self):
print(request.get_json())
api.add_resource(MessageBot, '/api/v1/chat')
# class CreateChatbot(Resource):
# def post(self):
# # validate_request(request)
# parser = reqparse.RequestParser()
# parser.add_argument('chatbotName', type=str, required=True)
# parser.add_argument('sourceText', type=str)
# args = parser.parse_args()
# chatbot_name = args['chatbotName']
# source_text = args.get('sourceText', '')
# # call api.py create_chatbot
# # result = create_chatbot(chatbot_name, source_text)
# # TBD:result validation
# return {'message': f'Chatbot "{chatbot_name}" created: "{result}"'}
# api.add_resource(CreateChatbot, '/api/v1/create-chatbot')