-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtask-2.py
More file actions
63 lines (57 loc) · 1.9 KB
/
task-2.py
File metadata and controls
63 lines (57 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
2. Изучить список открытых API (https://www.programmableweb.com/category/all/apis).
Найти среди них любое, требующее авторизацию (любого типа). Выполнить запросы к нему, пройдя авторизацию.
Ответ сервера записать в файл.
"""
from urllib.parse import urlencode
import requests
import json
class ApiClient:
base_url = 'https://api.thecatapi.com/v1/'
def __init__(self, api_key):
"""
Конструктор
:param api_key: Ключ доступа к апи
"""
self.api_key = api_key
def search(self,page:int=1,limit:int=10) -> dict:
"""
Постраничный поиск фото
https://api.thecatapi.com/v1/images/search?limit=&page
:param page:
:param limit:
:return:
"""
return self.__send('images/search', {
'limit': limit,
'page': page
})
def __send(self, method, params):
"""
Отправка API запроса
:param method:
:param params:
:return:
"""
url = self.__build(method, params)
response = requests.get(url)
if response.status_code != 200:
raise requests.ConnectionError
return response.json()
def __build(self, method, params):
"""
Построение ссылки
:param method:
:param params:
:return:
"""
params['api_key'] = self.api_key
return self.base_url+method+'?'+urlencode(params)
API_KEY = 'live_oNsVsaL8TAcRbmZzJvaWrPK0KJUlaVSF1Gu90EQ4Ldj1sJPirxXrFx8VhpaGwhaz'
client = ApiClient(API_KEY)
try:
with open('./out.json','w') as f:
json.dump(client.search(), f)
print('Готово')
except requests.ConnectionError:
print('Ошибка!')