Remember the movie search service we discussed when we first worked with HTTP services? It's back and you're going to build a proper API client for it using uplink
.
Today is mostly watching the corresponding videos from the course. Be sure to watch the videos first. Then:
- Create a new empty Python project with a virtual environment
- Reminder: Virtual environments are created using the commend
python3 -m venv .env
(use python rather than python3 for the command on Windows). - Activate the environment:
- macOS / Linux:
. .env/bin/activate
- Windows:
.env/scripts/activate
- macOS / Linux:
- Install
uplink
withpip
- Create a
program.py
Python file and supportingapi.py
file - Import
uplink
inside theapi.py
, import api inprogram.py
, and runprogram.py
to make sure it's wall hanging together.
Visit the movie search service: movieservice.talkpython.fm.
You'll see there are three RESTful operations.
Search movies
GET /api/search/{keyword}
Movies by director
GET /api/director/{director_name}
Movie by IMDB code
GET /api/movie/{imdb_number}
Your goal today will be to build an API client class in api.py
.
- Create a class (name it something like
MovieSearchClient
). - Indicate
uplink.Consumer
as the base class. - Add a
__init__
method to passhttps://movieservice.talkpython.fm/
as thebase_url
to the super class. - Add a method for each of the three HTTP endpoints
Recall that you define an endpoint method inside the class as:
class MyClass(uplink.Consumer):
@uplink.get('/path/to/api/with/{data}')
def call_api(data):
pass
# ...
Now that you have your API client, write a simple UI in program.py
that uses your class. You might give the user a chance to search by any of the three endpoints and then display the results.
Be sure to share your last couple of days work on Twitter or Facebook. Use the hashtag #100DaysOfCode.
Here are some examples to inspire you. Consider including @talkpython and @pybites in your tweets.
See a mistake in these instructions? Please submit a new issue or fix it and submit a PR.