Creating the server endpoints

Our web service will need three new endpoints—one to add a friend, one to block a friend, and one to grab all friends of a user.

Let's start with the endpoint to add a friend:

@app.route("/add_friend", methods=["POST"])
def add_friend():
data = request.form
user_one = data['user_one']
user_two = data['user_two']

if database.user_exists(user_two) and database.user_exists(user_one):
database.add_friend(user_one, user_two)
success = True
else:
success = False

return jsonify({
"success": success
})

This endpoint will be supplied with the two usernames that we want added to the friends table. Before we can add them, we need to check that they both exist in the users table already.

If they do, we call our database's add_friend method and return a successful response. If not, we will not add them to the friends table and, instead, we will return a negative success response.

Now onto retrieving a user's friends:

@app.route("/get_friends/<username>")
def get_friends(username):
friends = database.get_friends(username)

if len(friends):
all_friends = database.get_users_by_usernames(friends)
else:
all_friends = []

return jsonify({
"friends": all_friends
})

This method requires a username, then passes that over to the database's get_friends method. If we have any results come back, we pass them over to the get_users_by_usernames method in order to grab their real name and avatar. Otherwise, we return an empty list:

@app.route("/block_friend", methods=["POST"])
def block_friend():
data = request.form
user_one = data['user_one']
user_two = data['user_two']

database.block_friend(user_one, user_two)

return jsonify({
"success": True
})

Finally, when blocking a user, we pass the two usernames over to the database's block_friend method and return True.

With the endpoints taken care of, we now need to update our Requester to pass them data:

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

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

When adding a friend, we POST the two usernames over to our endpoint and let it take care of the rest:

def get_friends(self, username):
endpoint = f"/get_friends/{username}"

friends = self.request("GET", endpoint)

return json.loads(friends)

When retrieving friends, we send a GET request to the relevant endpoint, then use loads to parse the returned JSON as a dictionary:

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

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

return True

Much like adding a friend, we can just supply the two usernames to the endpoint when blocking a friend.

That's all of the groundwork taken care of. Now, we can tie this new functionality back into our chat application.

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

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