Added security for natrual endpoint
This commit is contained in:
@@ -294,21 +294,37 @@ def request_semantic_search(query):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.connect((HOST, PORT))
|
||||
s.sendall(data_string)
|
||||
print(f"sent {data_string}")
|
||||
data = b''
|
||||
while True:
|
||||
part = s.recv(1024)
|
||||
if not part:
|
||||
break
|
||||
print(part)
|
||||
data += part
|
||||
return json.loads(data.decode())
|
||||
|
||||
@app.get("/semantic/<key>/<query>")
|
||||
def do_semantic_search(key:str, query:str):
|
||||
if key != SECRET_API_KEY:
|
||||
@app.get("/natural")
|
||||
def explain_natural():
|
||||
res = 'Submit a POST request to this endpoint with the following params in the body:\n'
|
||||
res += '{"token": "mytokenstring", "query" : "myquerystring"}'
|
||||
return res
|
||||
|
||||
@app.post("/natural") # needs valid token and query in request body
|
||||
def do_semantic_search():
|
||||
request_data = request.get_json()
|
||||
if request_data is None:
|
||||
# Handle error: No JSON data sent
|
||||
return jsonify({'error': 'Invalid request format'}), 400
|
||||
|
||||
token_string = request_data.get('token')
|
||||
query_string = request_data.get('query')
|
||||
if token_string is None:
|
||||
# Handle error: Missing token in request
|
||||
return jsonify({'error': 'Missing token in request body'}), 401
|
||||
|
||||
if token_string != SECRET_API_KEY:
|
||||
return jsonify({'error': "Invalid Authorization"})
|
||||
result = request_semantic_search(query=query)
|
||||
|
||||
result = request_semantic_search(query=query_string)
|
||||
return jsonify(result)
|
||||
|
||||
@app.get("/ref/<ref>") # find database verse_id by english scripture reference (1John5:7)
|
||||
|
||||
Reference in New Issue
Block a user