Psuedocode:

  • What is Psuedocode

    • A notation resembling a simplified programming language, used in program design.
  • It is important to be fluent in the style of psuedocode that college board uses (by this I mean the way they use psuedocode, I understand psuedocode isn’t a universal language) because they test you using psuedocode.

  • They use it in to move objects or to represent code from a different language and usually ask you to find errors.

from flask import Blueprint, request, jsonify  
from flask_restful import Api, Resource 
from flask_cors import CORS
import requests  
from model.chats import *

chat_api = Blueprint('chat_api', __name__,
            url_prefix='/api/chats')
api = Api(chat_api)

CORS(chat_api, resources={r"/api/chats/create": {"origins": "https://eshaank1.github.io"}})

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.stu.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")
MODULE CHAT_API
    DECLARE CHAT_DATA

    FUNCTION _TEST
        DISPLAY {"Connection Test": "Successfully connected to backend!"}

    FUNCTION _CREATE
        DISPLAY {"message": "This is the GET request for _Create"}

        INPUT REQUEST
            DECLARE DATA
            DATA  REQUEST.JSON
            APPEND DATA TO CHAT_DATA
            DISPLAY {"message": "Data stored successfully!"}

    FUNCTION _READ
        DISPLAY CHAT_DATA

    END FUNCTION

    ADD RESOURCE _CREATE AT '/create'
    ADD RESOURCE _READ AT '/read'
    ADD RESOURCE _TEST AT '/test'

END MODULE

IF __NAME__ EQUALS "__MAIN__"
    DECLARE SERVER, URL, RESPONSES, SAMPLE_DATA, CREATE_RESPONSE, READ_RESPONSE

    SERVER  'https://chat.stu.nighthawkcodingsociety.com'
    URL  SERVER + "/api/chats"
    RESPONSES  []

    SAMPLE_DATA  {"message": "Hello, this is a test message!"}
    CREATE_RESPONSE  POST REQUEST TO URL + "/create" WITH JSON SAMPLE_DATA
    APPEND CREATE_RESPONSE TO RESPONSES

    READ_RESPONSE  GET REQUEST TO URL + "/read"
    APPEND READ_RESPONSE TO RESPONSES

    FOR EACH RESPONSE IN RESPONSES
        DISPLAY RESPONSE
        TRY
            DISPLAY RESPONSE.JSON
        EXCEPT
            DISPLAY "unknown error"
        END TRY
    END FOR
END IF