Skip to content

Commit 8332b79

Browse files
michaeljguarinosjkaliski
authored andcommitted
Python fio client streaming/additional endpoints (#13)
* Python fio client streaming/additional endpoints Add a utility to stream list methods via a python generator, also added endpoints to update assets, get collabs/pending collabs, and get project, team, etc. * Better pagination * Bump version
1 parent 2840277 commit 8332b79

3 files changed

Lines changed: 102 additions & 9 deletions

File tree

frameioclient/client.py

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
from .upload import FrameioUploader
22
import requests
33

4+
class PaginatedResponse(object):
5+
def __init__(results, page=0, page_size=0, total=0, total_pages=0):
6+
self.results = results
7+
self.page = int(page)
8+
self.page_size = int(page_size)
9+
self.total = int(total)
10+
self.total_pages = int(total_pages)
11+
12+
def __iter__(self):
13+
return iter(self.results)
14+
415
class FrameioClient(object):
516
def __init__(self, token, host='https://api.frame.io'):
617
self.token = token
@@ -21,6 +32,14 @@ def _api_call(self, method, endpoint, payload={}):
2132
)
2233

2334
if r.ok:
35+
if r.headers.get('page-number'):
36+
return PaginatedResponse(r.json(),
37+
page=r.headers['page-number'],
38+
page_size=r.headers['page-size'],
39+
total_pages=r.headers['total-pages'],
40+
total=r.headers['total']
41+
)
42+
2443
return r.json()
2544
return r.raise_for_status()
2645

@@ -30,7 +49,7 @@ def get_me(self):
3049
"""
3150
return self._api_call('get', '/me')
3251

33-
def get_teams(self, account_id):
52+
def get_teams(self, account_id, **kwargs):
3453
"""
3554
Get teams owned by the account.
3655
(To return all teams, use get_all_teams())
@@ -39,27 +58,67 @@ def get_teams(self, account_id):
3958
account_id (string): The account id.
4059
"""
4160
endpoint = '/accounts/{}/teams'.format(account_id)
61+
return self._api_call('get', endpoint, kwargs)
62+
63+
def get_team(self, team_id):
64+
"""
65+
Get's a team by id
66+
67+
:Args:
68+
team_id (string): the team's id
69+
"""
70+
endpoint = '/teams/{}'.format(team_id)
4271
return self._api_call('get', endpoint)
4372

44-
def get_all_teams(self):
73+
def get_all_teams(self, **kwargs):
4574
"""
4675
Get all teams for the authenticated user.
4776
4877
:Args:
4978
account_id (string): The account id.
5079
"""
5180
endpoint = '/teams'
52-
return self._api_call('get', endpoint)
81+
return self._api_call('get', endpoint, kwargs)
5382

54-
def get_projects(self, team_id):
83+
def get_projects(self, team_id, **kwargs):
5584
"""
5685
Get projects owned by the team.
5786
5887
:Args:
5988
team_id (string): The team id.
6089
"""
6190
endpoint = '/teams/{}/projects'.format(team_id)
91+
return self._api_call('get', endpoint, kwargs)
92+
93+
def get_project(self, project_id):
94+
"""
95+
Get an individual project
96+
97+
:Args:
98+
project_id (string): the project's id
99+
"""
100+
endpoint = '/projects/{}'.format(project_id)
62101
return self._api_call('get', endpoint)
102+
103+
def get_collaborators(self, project_id, **kwargs):
104+
"""
105+
Get collaborators for a project
106+
107+
:Args:
108+
project_id (string): the project's id
109+
"""
110+
endpoint = "/projects/{}/collaborators".format(project_id)
111+
return self._api_call('get', endpoint, kwargs)
112+
113+
def get_pending_collaborators(self, project_id, **kwargs):
114+
"""
115+
Get pending collaborators for a project
116+
117+
:Args:
118+
project_id (string): the project's id
119+
"""
120+
endpoint = "/projects/{}/pending_collaborators".format(project_id)
121+
return self._api_call('get', endpoint, kwargs)
63122

64123
def create_project(self, team_id, **kwargs):
65124
"""
@@ -100,15 +159,15 @@ def get_asset(self, asset_id):
100159
endpoint = '/assets/{}'.format(asset_id)
101160
return self._api_call('get', endpoint)
102161

103-
def get_asset_children(self, asset_id):
162+
def get_asset_children(self, asset_id, **kwargs):
104163
"""
105164
Get an asset's children.
106165
107166
:Args:
108167
asset_id (string): The asset id.
109168
"""
110169
endpoint = '/assets/{}/children'.format(asset_id)
111-
return self._api_call('get', endpoint)
170+
return self._api_call('get', endpoint, kwargs)
112171

113172
def create_asset(self, parent_asset_id, **kwargs):
114173
"""
@@ -131,6 +190,21 @@ def create_asset(self, parent_asset_id, **kwargs):
131190
"""
132191
endpoint = '/assets/{}/children'.format(parent_asset_id)
133192
return self._api_call('post', endpoint, payload=kwargs)
193+
194+
def update_asset(self, asset_id, **kwargs):
195+
"""
196+
Updates an asset
197+
198+
:Args:
199+
asset_id (string): the asset's id
200+
:Kwargs:
201+
the fields to update
202+
203+
Example::
204+
client.update_asset("adeffee123342", name="updated_filename.mp4")
205+
"""
206+
endpoint = '/assets/{}/children'.format(asset_id)
207+
return self._api_call('put', endpoint, kwargs)
134208

135209
def upload(self, asset, file):
136210
"""
@@ -147,15 +221,15 @@ def upload(self, asset, file):
147221
uploader = FrameioUploader(asset, file)
148222
uploader.upload()
149223

150-
def get_comments(self, asset_id):
224+
def get_comments(self, asset_id, **kwargs):
151225
"""
152226
Get an asset's comments.
153227
154228
:Args:
155229
asset_id (string): The asset id.
156230
"""
157231
endpoint = '/assets/{}/comments'.format(asset_id)
158-
return self._api_call('get', endpoint)
232+
return self._api_call('get', endpoint, **kwargs)
159233

160234
def create_comment(self, asset_id, **kwargs):
161235
"""

frameioclient/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def stream(func, page=1, page_size=20):
2+
"""
3+
Accepts a lambda of a call to a client list method, and streams the results until
4+
the list has been exhausted
5+
6+
:Args:
7+
fun (function): A 1-arity function to apply during the stream
8+
9+
Example::
10+
stream(lambda pagination: client.get_collaborators(project_id, **pagination))
11+
"""
12+
total_pages = page
13+
while page <= total_pages:
14+
result_list = func(page=page, page_size=page_size)
15+
total_pages = result_list.total_pages
16+
for res in result_list:
17+
yield res
18+
19+
page += 1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='frameioclient',
8-
version='0.4.0',
8+
version='0.5.0',
99
description='Client library for the Frame.io API',
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)