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...