Adding server endpoints

We will need two endpoints to make full use of our avatars – one to upload a new one, and one to fetch an avatar for a particular user.

Open up server.py and add the following functions:

@app.route("/update_avatar/<username>", methods=["POST"])
def update_avatar(username):
img_b64 = request.form.get("img_b64")
database.update_avatar(username, img_b64)

return jsonify({
"success": True
})

This first endpoint will store the base64 encoded image data in the database against the username provided in the URL:

@app.route("/get_user_avatar/<username>")
def get_avatar(username):

avatar_b64 = database.get_user_avatar(username)['avatar']

return jsonify({
"avatar": avatar_b64
})

The second endpoint will get the user's base64 encoded avatar from the database and return it in JSON format.

With the web service all set up, our AvatarWindow should be ready to upload new avatars! However, since it relies on the requester instance of our FriendsList, it will not function fully until we integrate it.

Let's finish off our AvatarWindow by creating a menu option for it in the FriendsList class.

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

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