Creating the Requester class

Make yourself a new file in the same folder as your friendslist.py file named requester.py:

import json
import requests

class Requester:
def __init__(self):
self.url = "http://127.0.0.1:5000"

As the name implies, the Requester class will be making use of the requests module to communicate with our web service. We will also need to use the json module to read any data which is returned.

In our __init__, we just need to keep a reference to the URL at which our web service operates. Keeping it here means that, if we change it for any reason, we only have one place in this class to update.

Since our web service uses both GET and POST endpoints, we can generalize our requesting by extracting it to a method:

def request(self, method, endpoint, params=None):
url = self.url + endpoint

if method == "GET":
r = requests.get(url, params=params)
return r.text
else:
r = requests.post(url, data=params)

return r.json()

This method will take the request method (GET or POST), the endpoint (which should match that in the relevant app.route decorator in our server file) and any parameters which need to be passed.

Based on the request method, it will send our parameters using a different keyword argument to the method provided by requests  and return different parts of the response.

If it was a GET request, we will return r.text, which contains any text returned from the server. If it was a POST request instead, our server will have returned JSON, so we need to use the json method to convert it to a Python dictionary.

Now that we have this method in place, we can begin implementing our login and account creation calls.

When logging in, we just need to check that the provided username and real name exist in our database, so we have to call the /user_exists endpoint:

def login(self, username, real_name):
endpoint = "/user_exists"
params = {"username": username}

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

return user_exists["exists"]

We pass the endpoint the provided username and return the value of the exists key in the  JSON returned by the server.

When creating an account, we should also first check whether the user exists and only create the account if they do not:

def create_account(self, username, real_name):
endpoint = "/user_exists"
params = {"username": username}
exists = self.request("POST", endpoint, params)

if exists["exists"]:
return False

endpoint = "/add_user"
params["real_name"] = real_name

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

return True

Here, we see much the same code as before, except if the user does already exist, we want to return False. If they do not, we then call the /add_user endpoint, providing the real name as well, then return True.

Now that our Requester is in place, we can import it in our FriendsList and connect it to the web service.

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

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