-
Notifications
You must be signed in to change notification settings - Fork 0
/
brivo.py
31 lines (26 loc) · 1.33 KB
/
brivo.py
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
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session
import requests
TOKEN_URL = "https://auth.brivo.com/oauth/token"
USERS_URL = "https://api.brivo.com/v1/api/users"
class Brivo:
def __init__(self, username, password, client_id, client_secret, api_key):
# Connect and get an access token
client = LegacyApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
self._token = oauth.fetch_token(token_url=TOKEN_URL, username=username,
password=password, client_id=client_id, client_secret=client_secret)
# Set up the headers we'll need for each request
self._request_headers = {"api-key": api_key,
"Authorization": f"bearer {self._token['access_token']}",
"Content-type": "application/json"}
def users(self, **optional_fields):
"""
Returns the list of users. Each user is a dictionary of field/value items.
Use optional_fields to pass any of the optional querystring params.
See https://apidocs.brivo.com/#api-User-ListUsers
"""
r = requests.get(
USERS_URL, headers=self._request_headers, params=optional_fields)
r.raise_for_status()
return r.json()['data']