from flask import Blueprint, request, jsonify
from flask_restful import Api, Resource
from flask_cors import CORS
import requests
import random
import os
from model.chats import *
chat_api = Blueprint('chat_api', __name__,
url_prefix='/api/chats')
api = Api(chat_api)
CORS(chat_api, resources={r"/api/*": {"origins": "*"}})
chat_data = []
class ChatAPI:
class _Test(Resource):
def get(self):
response = jsonify({"Connection Test": "Successfully connected to backend!"})
return response
class _Create(Resource):
def get(self):
return jsonify({"message": "This is the GET request for _Create"})
def post(self):
data = request.json
chat_data.append(data)
return jsonify({"message": "Data stored successfully!"})
class _Read(Resource):
def get(self):
return jsonify(chat_data)
api.add_resource(ChatAPI._Create, '/create')
api.add_resource(ChatAPI._Read, '/read')
api.add_resource(ChatAPI._Test, '/test')
if __name__ == "__main__":
server = "http://127.0.0.1:8987" # run local
# server = 'https://chat.nighthawkcodingsociety.com' # Update with your server URL
url = server + "/api/chats"
responses = []
# Simulate sending data to the chat API
sample_data = {"message": "Hello, this is a test message!"}
create_response = requests.post(url+"/create", json=sample_data)
responses.append(create_response)
# Retrieve stored data from the chat API
read_response = requests.get(url+"/read")
responses.append(read_response)
# Display responses
for response in responses:
print(response)
try:
print(response.json())
except:
print("unknown error")
Review of Code
-
The imports at the top import the needed libraries for the api
-
From model.chats import * imports the chats.py from the model folder
-
List called chat_data stores data
-
Within the class ChatApi, there is multiple different nested resource classes
-
Test: This class defines a resource for testing the connection to the backend. Responds with a JSON message to indicate a successful connection
-
Create: This class defines a resource for creating chat messages. It supports both GET and POST requests. When accessed using POST it adds a chat message to the list
-
Read: This class defines a resource for reading the chat messages stored in the chat_data when accessed using GET it lists chat_data
-
-
Then it creates three resource classes and gives them a url
-
Sets the server url to http://127.0.0.1:8987 for local or https://chat.nighthawkcodingsociety.com
-
It sends a POST request to the ‘/create’ endpoint with a sample message and stores the response in create_response.
-
It sends a GET request to the ‘/read’ endpoint and stores the response in read_response.