Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP 202 Assignment
Question 1: Paying It Forward (40 points)
An Application Programming Interface, or API for short, is a way for two or more computer programs to communicate with each other. An API is comprised of a formal specification, describing how the communication is done, and an implementation, which implements the communication process in code.
APIs are not designed for regular users of a computer program. They are designed for use by programmers, who want their computer program to be able to interface with another.
A common type of API is called a web API. A web API is used by a computer program to retrieve information from a website. Most popular websites have their own web API. For example, YouTube,Facebook, Reddit, and Twitter (while it still exists) all have a web API. These APIs allow a computer program to connect to the website and retrieve data, e.g., tweets from Twitter, or posts from Reddit.
The data can then be used in the computer program.
Here are some examples of APIs:
Using an API
It is relatively simple to use a web API. Our code must connect to a website at a special API URL and retrieve data from it.
First, we need to know how to retrieve data from a website in Python. To do this, we will need to install the requests module into Thonny, following the usual steps. (You may already have it installed if you have tried out the Virtual World.)
With the requests module installed, we can use the following code to retrieve data from a given URL.
This example assumes that the website will send the information to us in dictionary form (which is not always the case – it depends on the particular API).
result_dict = requests.get(url=‘url-here’).json()
The resulting dictionary will contain the data requested from the API and/or other information about the status of the request.
Each API has several endpoints. An endpoint is a ‘point of entry’ into the API. Each different request you can make (e.g., posting a comment on a YouTube video, searching for a video, liking or disliking a video) will have its own endpoint. Each endpoint also has its own URL (just like each different webpage on a website).
After the URL, we must also include certain data to make our request.id=VIDEOID’1 .
The COMP202COIN API
In this question, we are going to write some code to interface with a web API. Not just any web API – the COMP202COIN API, which was designed specifically for this question.
The COMP202COIN API will let you perform certain operations with respect to your coin balance. So that only you can access your own balance, we introduce a security measure called a token, which you can think of as a password to your account.
– email: your McGill email address used to sign up to our Slack workspace
– token: your coin account token
– withdrawal_email: your McGill email address used to sign up to our Slack workspace
– token: your coin account token
– deposit_email: the McGill email address of the student to which you want to transfer coins
– amount: the number of coins you want to transfer to the other student
To construct an API request given an endpoint and the needed data, we write the API URL, followed by the endpoint name and a ‘?’, then each piece of data in the form ‘name=value’, each separated with an ampersand ‘&’.
E.g., if I wanted to get my balance and my token was ABC, then I would construct the following URL:
>>> result = requests.get(url=request_url).json()
>>> result
{‘message’: ‘The token in the API request did not match the token that was sent over Slack.’,
‘status’: ‘error’}