Adding the new endpoints to our Requester

When the user opens  up a chat window with a friend, our Requester will need to contact the web server to retrieve the conversation history. If there is no conversation history to be obtained, then we will want to initialize the database to prevent errors.

Since initializing the database when it already exists will do nothing, we are safe to call the endpoint even if a database already exists.

We can take advantage of this in our Requester, so that we can skip adding a separate method for initialization and, instead, handle it all when getting the history:

def prepare_conversation(self, user_one, user_two):
endpoint = "/create_conversation_db"
params = {"user_one": user_one, "user_two": user_two}

self.request("POST", endpoint, params)

endpoint = "/get_message_history"
history = self.request("POST", endpoint, params)

return history

The first method to add to our Requester is prepare_conversation. We can call this each time the user opens a new ChatWindow instance. It will create a conversation database if one does not already exist and get the content if it does.

The JSON returned from the web service will just be returned so that the calling class has access to it.

We will also need a method to add a message to the database:

def send_message(self, author, friend_name, message):
endpoint = f"/send_message/{friend_name}"
params = {
"author": author,
"message": message,
}

self.request("POST", endpoint, params)

return True

The send_message method will take the author name, friend name, and message, and then send the appropriate POST request to our web service. Since we don't have any useful information returned from the web service, we can just return True here.

Since the username of the person we are messaging is extracted from the URL, we use a format string to insert the friend_name variable into the endpoint URL.

That concludes the changes we need to make to our Requester. Now, everything is finally set up ready for us to  connect our ChatWindow class!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset