forked from vtemian/buffpy
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi.py
More file actions
102 lines (72 loc) · 3 KB
/
api.py
File metadata and controls
102 lines (72 loc) · 3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
import urllib
from rauth import OAuth2Session, OAuth2Service
from buffpy.response import ResponseObject
BASE_URL = 'https://api.bufferapp.com/1/%s'
PATHS = {
'INFO': 'info/configuration.json'
}
AUTHORIZE_URL = 'https://bufferapp.com/oauth2/authorize'
ACCESS_TOKEN = 'https://api.bufferapp.com/1/oauth2/token.json'
class API(object):
'''
Small and clean class that embrace all basic
operations with the buffer app
'''
def __init__(self, client_id, client_secret, access_token=None):
self.session = OAuth2Session(client_id=client_id,
client_secret=client_secret,
access_token=access_token)
@property
def access_token(self):
return self.session.access_token
@access_token.setter
def access_token(self, value):
self.session.access_token = value
def get(self, url, parser=None):
if parser is None:
parser = json.loads
if not self.session.access_token:
raise ValueError('Please set an access token first!')
response = self.session.get(url=BASE_URL % url)
return parser(response.content)
def post(self, url, parser=None, **params):
if parser is None:
parser = json.loads
if not self.session.access_token:
raise ValueError('Please set an access token first!')
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = self.session.post(url=BASE_URL % url, headers=headers, **params)
return parser(response.content)
@property
def info(self):
'''
Returns an object with the current configuration that Buffer is using,
including supported services, their icons and the varying limits of
character and schedules.
The services keys map directly to those on profiles and updates so that
you can easily show the correct icon or calculate the correct character
length for an update.
'''
response = self.get(url=PATHS['INFO'])
return ResponseObject(response)
class AuthService(object):
def __init__(self, client_id, client_secret, redirect_uri):
self.outh_service = OAuth2Service(client_id=client_id,
client_secret=client_secret,
name='buffer',
authorize_url=AUTHORIZE_URL,
access_token_url=ACCESS_TOKEN,
base_url=BASE_URL % '')
self.redirect_uri = redirect_uri
def create_session(self, access_token=None):
return self.outh_service.get_session(access_token)
def get_access_token(self, auth_code):
auth_code = urllib.unquote(auth_code).decode('utf8')
data = {'code': auth_code,
'grant_type': 'authorization_code',
'redirect_uri': self.redirect_uri}
return self.outh_service.get_access_token(data=data, decoder=json.loads)
@property
def authorize_url(self):
return self.outh_service.get_authorize_url(response_type='code', redirect_uri=self.redirect_uri)