|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
|
4
|
|
|
# |
|
5
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
# in the Software without restriction, including without limitation the rights |
|
8
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
# furnished to do so, subject to the following conditions: |
|
11
|
|
|
# |
|
12
|
|
|
# The above copyright notice and this permission notice shall be included |
|
13
|
|
|
# in all copies or substantial portions of the Software. |
|
14
|
|
|
# |
|
15
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
# SOFTWARE. |
|
22
|
1 |
|
"""This module contains a object that represents a Habrahabr Auth.""" |
|
23
|
|
|
|
|
24
|
1 |
|
import re |
|
25
|
1 |
|
from .errors import AuthHandlerError |
|
26
|
|
|
|
|
27
|
1 |
|
try: |
|
28
|
|
|
# python3 |
|
29
|
1 |
|
from urllib.parse import urlencode |
|
30
|
|
|
except ImportError: # pragma: no cover |
|
31
|
|
|
# python2 |
|
32
|
|
|
from urllib import urlencode |
|
33
|
|
|
|
|
34
|
1 |
|
API_HOST = 'https://api.habrahabr.ru/v1' |
|
35
|
1 |
|
API_AUTH_HOST = 'https://habrahabr.ru/auth/o/login/' |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
1 |
|
class Auth(object): |
|
39
|
|
|
"""Этот объект содержит Habrahabr Auth. |
|
40
|
|
|
|
|
41
|
|
|
Args: |
|
42
|
|
|
client (Optional[str]): Habrahabr OAuth Client. |
|
43
|
|
|
token (Optional[str]): Habrahabr OAuth Token. |
|
44
|
|
|
api_key (Optional[str]): Habrahabr Api Key. |
|
45
|
|
|
""" |
|
46
|
|
|
|
|
47
|
1 |
|
def __init__(self, client=None, token=None, api_key=None): |
|
48
|
|
|
"""Конструктор Auth. |
|
49
|
|
|
|
|
50
|
|
|
:param client: Habrahabr OAuth Client. |
|
51
|
|
|
:param token: Habrahabr OAuth Token. |
|
52
|
|
|
:param api_key: Habrahabr Api Key. |
|
53
|
|
|
:returns: |
|
54
|
|
|
""" |
|
55
|
1 |
|
self._client = None |
|
56
|
1 |
|
self._token = None |
|
57
|
1 |
|
self._api_key = None |
|
58
|
1 |
|
self._endpoint = API_HOST |
|
59
|
|
|
|
|
60
|
1 |
|
if (client is not None) and (token is not None): |
|
61
|
1 |
|
self.set_request_client(client) |
|
62
|
1 |
|
self.set_request_token(token) |
|
63
|
1 |
|
elif api_key is not None: |
|
64
|
1 |
|
self.set_request_apikey(api_key) |
|
65
|
|
|
|
|
66
|
1 |
|
def __str__(self): |
|
67
|
|
|
"""Возвращает OAuth Endpoint для доступа к Habrahabr API.""" |
|
68
|
|
|
return '%s' % self._endpoint |
|
69
|
|
|
|
|
70
|
1 |
|
def __repr__(self): |
|
71
|
|
|
"""Возвращает строковое представление объекта.""" |
|
72
|
|
|
return '%s(%r)' % (self.__class__, self.__dict__) |
|
73
|
|
|
|
|
74
|
1 |
|
@property |
|
75
|
|
|
def endpoint(self): |
|
76
|
|
|
"""Получить OAuth Endpoint для доступа к Habrahabr API.""" |
|
77
|
1 |
|
return self._endpoint |
|
78
|
|
|
|
|
79
|
1 |
|
@property |
|
80
|
|
|
def headers(self): |
|
81
|
|
|
"""Получить Request Headers для доступа к Habrahabr API.""" |
|
82
|
1 |
|
headers = {} |
|
83
|
1 |
|
if (self._client is not None) and (self._token is not None): |
|
84
|
1 |
|
headers = {'client': self._client, 'token': self._token} |
|
85
|
1 |
|
elif self._api_key is not None: |
|
86
|
1 |
|
headers = {'apikey': self._api_key} |
|
87
|
|
|
|
|
88
|
1 |
|
return headers |
|
89
|
|
|
|
|
90
|
1 |
|
def set_request_client(self, client): |
|
91
|
|
|
"""Установить OAuth Client для доступа к Habrahabr API. |
|
92
|
|
|
|
|
93
|
|
|
:param client: OAuth Client для доступа к Habrahabr API. |
|
94
|
|
|
:returns: |
|
95
|
|
|
""" |
|
96
|
1 |
|
if not re.match(r'([a-z0-9]+)\.([a-z0-9]+)', client): |
|
97
|
|
|
raise AuthHandlerError('Incorrect API Client: ' + client) |
|
98
|
1 |
|
self._client = client |
|
99
|
|
|
|
|
100
|
1 |
|
def set_request_token(self, token): |
|
101
|
|
|
"""Установить OAuth Token для доступа к Habrahabr API. |
|
102
|
|
|
|
|
103
|
|
|
:param token: OAuth Token для доступа к Habrahabr API. |
|
104
|
|
|
:returns: |
|
105
|
|
|
""" |
|
106
|
1 |
|
if not re.match(r'([a-z0-9]+)', token): |
|
107
|
|
|
raise AuthHandlerError('Incorrect API Token: ' + token) |
|
108
|
1 |
|
self._token = token |
|
109
|
|
|
|
|
110
|
1 |
|
def set_request_apikey(self, api_key): |
|
111
|
|
|
"""Установить ключ для доступа к не персонализированным данным API. |
|
112
|
|
|
|
|
113
|
|
|
:param api_key: Ключ для доступа к не персонализированным данным API. |
|
114
|
|
|
:returns: |
|
115
|
|
|
""" |
|
116
|
1 |
|
if not re.match(r'([a-z0-9]+)', api_key): |
|
117
|
|
|
raise AuthHandlerError('Incorrect API Key: ' + api_key) |
|
118
|
1 |
|
self._api_key = api_key |
|
119
|
|
|
|
|
120
|
1 |
|
def get_authorization_url(self, redirect_uri, response_type='code'): |
|
121
|
|
|
"""Возращает URL для OAuth авторизации Habrahabr Api. |
|
122
|
|
|
|
|
123
|
|
|
:param redirect_uri: OAuth Redirect URL. |
|
124
|
|
|
:param response_type: OAuth Response type. |
|
125
|
|
|
:returns: OAuth authorization URL. |
|
126
|
|
|
:rtype: str |
|
127
|
|
|
""" |
|
128
|
1 |
|
if self._client is None: |
|
129
|
|
|
raise AuthHandlerError('Incorrect API Client') |
|
130
|
|
|
|
|
131
|
1 |
|
payload = { |
|
132
|
|
|
'response_type': response_type, |
|
133
|
|
|
'client_id': self._client, |
|
134
|
|
|
'redirect_uri': redirect_uri |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return API_AUTH_HOST + '?' + urlencode(payload) |
|
138
|
|
|
|