Skip to content

Commit

Permalink
Merge pull request #11 from apivideo/python-upload-progress
Browse files Browse the repository at this point in the history
chore(python) add progress listener on upload methods
  • Loading branch information
bot-api-video authored Jul 1, 2021
2 parents a283e98 + f5c704e commit ec15b37
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apivideo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""


__version__ = "0.0.11"
__version__ = "0.0.12"

# import ApiVideoClient
from apivideo.auth_api_client import AuthenticatedApiClient
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/captions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/chapters_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/live_streams_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/player_themes_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/raw_statistics_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/upload_tokens_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
30 changes: 24 additions & 6 deletions apivideo/api/videos_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down Expand Up @@ -894,6 +896,9 @@ def upload_with_upload_token(
number provided, it will be total request timeout. It can also
be a pair (tuple) of (connection, read) timeouts.
Default is None.
_progress_listener (method): method called each time a chunk is uploaded. Takes 2 parameters:
the first one is the number of bytes uploaded, the second one is the total number of bytes.
Default is None.
async_req (bool): execute request asynchronously
Returns:
Expand Down Expand Up @@ -925,7 +930,8 @@ def upload_with_upload_token(
'async_req',
'_preload_content',
'_request_timeout',
'_return_http_data_only'
'_return_http_data_only',
'_progress_listener',
],
'required': [
'token',
Expand All @@ -951,7 +957,8 @@ def upload_with_upload_token(
'async_req': (bool,),
'_preload_content': (bool,),
'_request_timeout': (none_type, int, (int,), [int]),
'_return_http_data_only': (bool,)
'_return_http_data_only': (bool,),
'_progress_listener': (none_type, MethodType, FunctionType),
}
attribute_map = {
'token': 'token',
Expand Down Expand Up @@ -988,7 +995,10 @@ def upload_with_upload_token(
self._validate_inputs(kwargs, params_map, allowed_values, validations, openapi_types)
params = self._gather_params(kwargs, location_map, attribute_map, openapi_types, collection_format_map)
res = None
for content_range, chunk, isLast in self._chunk_file(params['file']):
progress_listener = kwargs.get('_progress_listener', None)
for content_range, chunk, isLast, offset, file_size in self._chunk_file(params['file']):
if progress_listener is not None:
progress_listener(offset, file_size)
res = self.api_client.call_api(
"/upload",
"POST",
Expand Down Expand Up @@ -1162,6 +1172,9 @@ def upload(
number provided, it will be total request timeout. It can also
be a pair (tuple) of (connection, read) timeouts.
Default is None.
_progress_listener (method): method called each time a chunk is uploaded. Takes 2 parameters:
the first one is the number of bytes uploaded, the second one is the total number of bytes.
Default is None.
async_req (bool): execute request asynchronously
Returns:
Expand Down Expand Up @@ -1193,7 +1206,8 @@ def upload(
'async_req',
'_preload_content',
'_request_timeout',
'_return_http_data_only'
'_return_http_data_only',
'_progress_listener',
],
'required': [
'video_id',
Expand All @@ -1219,7 +1233,8 @@ def upload(
'async_req': (bool,),
'_preload_content': (bool,),
'_request_timeout': (none_type, int, (int,), [int]),
'_return_http_data_only': (bool,)
'_return_http_data_only': (bool,),
'_progress_listener': (none_type, MethodType, FunctionType),
}
attribute_map = {
'video_id': 'videoId',
Expand Down Expand Up @@ -1256,7 +1271,10 @@ def upload(
self._validate_inputs(kwargs, params_map, allowed_values, validations, openapi_types)
params = self._gather_params(kwargs, location_map, attribute_map, openapi_types, collection_format_map)
res = None
for content_range, chunk, isLast in self._chunk_file(params['file']):
progress_listener = kwargs.get('_progress_listener', None)
for content_range, chunk, isLast, offset, file_size in self._chunk_file(params['file']):
if progress_listener is not None:
progress_listener(offset, file_size)
res = self.api_client.call_api(
"/videos/{videoId}/source",
"POST",
Expand Down
2 changes: 2 additions & 0 deletions apivideo/api/webhooks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os # noqa: F401
import re # noqa: F401
import sys # noqa: F401
from types import MethodType
from types import FunctionType

from apivideo.api_client import ApiClient
from apivideo.endpoint import EndPoint as _EndPoint
Expand Down
2 changes: 1 addition & 1 deletion apivideo/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
self.default_headers[header_name] = header_value
self.cookie = cookie
# Set default User-Agent.
self.user_agent = '"api.video client (python; v:0.0.11; )"'
self.user_agent = '"api.video client (python; v:0.0.12; )"'

def __enter__(self):
return self
Expand Down
2 changes: 1 addition & 1 deletion apivideo/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def to_debug_report(self):
"OS: {env}\n"\
"Python Version: {pyversion}\n"\
"Version of the API: 1\n"\
"SDK Package Version: 0.0.11".\
"SDK Package Version: 0.0.12".\
format(env=sys.platform, pyversion=sys.version)

def get_host_settings(self):
Expand Down
2 changes: 1 addition & 1 deletion apivideo/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ def _chunk_file(self, file_info):

for chunk in iter(partial(file.read, self.api_client.configuration.chunk_size), b''):
offset = index + len(chunk)
yield 'bytes {}-{}/{}'.format(index, offset - 1, file_size), {file_name: [ChunkIO(chunk, file.name)]}, offset == file_size
yield 'bytes {}-{}/{}'.format(index, offset - 1, file_size), {file_name: [ChunkIO(chunk, file.name)]}, offset == file_size, offset, file_size
index = offset
4 changes: 4 additions & 0 deletions apivideo/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import pprint
import re
import tempfile
from types import MethodType
from types import FunctionType

from dateutil.parser import parse

Expand Down Expand Up @@ -579,6 +581,8 @@ def __eq__(self, other):
date: 10,
str: 11,
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
MethodType: 13,
FunctionType: 14,
}

# these are used to limit what type conversions we try to do
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from setuptools import setup, find_packages # noqa: H301

NAME = "api.video"
VERSION = "0.0.11"
VERSION = "0.0.12"
# To install the library, run the following
#
# python setup.py install
Expand Down
10 changes: 8 additions & 2 deletions test/test_integration_videos_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ def test_metadata(self):
self.api.delete(video_with_metadata.video_id)
self.api.delete(video_without_metadata.video_id)



@unittest.skipIf(os.getenv("API_KEY") is None, "No API key")
def test_upload(self):

def listener(uploaded, total):
print('Progress: {}/{}'.format(uploaded, total))

video = self.api.create(video_creation_payload=VideoCreationPayload(
title='upload',
public=True,
tags=["bunny"]))

file = open("sample.mp4", "rb")
self.api.upload(video.video_id, file, _request_timeout=20)
file = open("sample-mp4-file.mp4", "rb")
self.api.upload(video.video_id, file, _request_timeout=20, _progress_listener=listener)
file.close()
self.api.delete(video.video_id)

Expand Down

0 comments on commit ec15b37

Please sign in to comment.