Skip to content

Commit ee8e828

Browse files
author
Plamen Milenkov
committed
Days 56 and 57. Working with uplink.
1 parent 89dd0ac commit ee8e828

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

days/55-57-uplink/my_demo/api.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
import uplink
2+
import requests
3+
from uplink_helpers import raise_for_status
24

3-
def test():
4-
pass
5+
6+
@raise_for_status
7+
@uplink.json
8+
class API(uplink.Consumer):
9+
def __init__(self):
10+
super().__init__(base_url='http://movie_service.talkpython.fm/')
11+
12+
13+
@uplink.get('/api/search/{keyword}')
14+
def search_by_keyword(self, keyword) -> requests.models.Response:
15+
''' Search by given keyword'''
16+
17+
18+
@uplink.get('/api/director/{director_name}')
19+
def get_director_by_name(self, director_name) -> requests.models.Response:
20+
''' Geti director by name '''
21+
22+
@uplink.get('/api/movie/{imdb_number}')
23+
def get_movie_by_imdb_number(self, imdb_number) -> requests.models.Response:
24+
''' Get movie by imdb number'''

days/55-57-uplink/my_demo/program.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
1-
import api
1+
2+
from api import API
3+
4+
5+
MENU_OPTIONS = ['Search By Keyword',
6+
'Get Director Info by Name',
7+
'Get Movie Info by IMDB ID']
8+
9+
MENU_OPTIONS_TO_FUNC_MAP = {
10+
1: {
11+
'function_name': 'search_by_keyword',
12+
'parameter_name': 'keyword'
13+
},
14+
2: {
15+
'function_name': 'get_director_by_name',
16+
'parameter_name': 'director name'
17+
},
18+
3: {
19+
'function_name': 'get_movie_by_imdb_number',
20+
'parameter_name': 'imdb number'
21+
}
22+
}
23+
24+
def display_menu():
25+
for id, option in enumerate(MENU_OPTIONS, start=1):
26+
print(f'[{id}]. {option}')
27+
28+
29+
def get_user_choice():
30+
user_selection = input('Select: ')
31+
try:
32+
user_selection = int(user_selection)
33+
except ValueError as e:
34+
print(e)
35+
exit(1)
36+
return user_selection
237

338
def main():
4-
api.test()
39+
display_menu()
40+
user_selection = get_user_choice()
41+
function_name = MENU_OPTIONS_TO_FUNC_MAP[user_selection]['function_name']
42+
parameter_name = MENU_OPTIONS_TO_FUNC_MAP[user_selection]['parameter_name']
43+
parameter_value = input(f'Please enter {parameter_name}: ')
44+
45+
api = API()
46+
function_to_call = api.__getattribute__(function_name)
47+
response = function_to_call(parameter_value)
48+
49+
if response.json() is None:
50+
print('Could not find anything with this information.')
51+
exit(1)
52+
# response = api.search_by_keyword('movie')
53+
for entry in response.json():
54+
print(f'{entry}')
55+
56+
# print()
57+
#
58+
# response = api.get_director_by_name('Cameron')
59+
# for entry in response.json()['hits']:
60+
# print(entry)
61+
#
62+
# print()
63+
#
64+
# response = api.get_movie_by_imdb_number('tt0499549')
65+
# movie_info = response.json()
66+
# print('{}'.format(entry['imdb_score']))
67+
# for entry in movie_info:
68+
69+
570

671

772
if __name__ == '__main__':
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
import uplink
3+
4+
@uplink.response_handler
5+
def raise_for_status(response: requests.models.Response):
6+
response.raise_for_status()
7+
return response

0 commit comments

Comments
 (0)