Skip to content

Commit

Permalink
Merge pull request #10 from apivideo/python-upload-generic-file-type
Browse files Browse the repository at this point in the history
chore(python) allow more generic file type in uploads
  • Loading branch information
bot-api-video authored Jun 25, 2021
2 parents 2c5f508 + 4c33ca1 commit 156ab10
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ jobs:
pip install -e ./
- name: Test
run: cd test && python -m unittest
env:
API_KEY: ${{ secrets.API_KEY }}
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.10"
__version__ = "0.0.11"

# import ApiVideoClient
from apivideo.auth_api_client import AuthenticatedApiClient
Expand Down
9 changes: 7 additions & 2 deletions 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.10; )"'
self.user_agent = '"api.video client (python; v:0.0.11; )"'

def __enter__(self):
return self
Expand Down Expand Up @@ -538,7 +538,12 @@ def files_parameters(self, files: typing.Optional[typing.Dict[str, typing.List[i
"Cannot read a closed file. The passed in file_type "
"for %s must be open." % param_name
)
filename = os.path.basename(file_instance.name)

if file_instance.name is None or isinstance(file_instance.name, (int, )):
filename = 'no_name'
else:
filename = os.path.basename(file_instance.name)

filedata = self.get_file_data_and_close_file(file_instance)
mimetype = (mimetypes.guess_type(filename)[0] or
'application/octet-stream')
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.10".\
"SDK Package Version: 0.0.11".\
format(env=sys.platform, pyversion=sys.version)

def get_host_settings(self):
Expand Down
31 changes: 27 additions & 4 deletions apivideo/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""


import abc
from datetime import date, datetime # noqa: F401
import inspect
import io
Expand All @@ -25,8 +26,29 @@
)

none_type = type(None)
file_type = io.IOBase

class GenericFile(metaclass=abc.ABCMeta):
@classmethod
def __subclasshook__(cls, subclass):
return (hasattr(subclass, 'seek') and
callable(subclass.seek) and
hasattr(subclass, 'read') and
callable(subclass.read) and
hasattr(subclass, 'tell') and
callable(subclass.tell) or
NotImplemented)

@abc.abstractmethod
def seek(self, *args):
raise NotImplementedError

@abc.abstractmethod
def tell(self, *args):
raise NotImplementedError

@abc.abstractmethod
def read(self, *args):
raise NotImplementedError

class cached_property(object):
# this caches the result of the function call for fn with no inputs
Expand All @@ -45,6 +67,7 @@ def __get__(self, instance, cls=None):
setattr(self, self.result_key, result)
return result

file_type = GenericFile

PRIMITIVE_TYPES = (list, float, int, bool, datetime, date, str, file_type)

Expand Down Expand Up @@ -546,7 +569,7 @@ def __eq__(self, other):
ModelComposed: 0,
ModelNormal: 1,
ModelSimple: 2,
none_type: 3, # The type of 'None'.
none_type: 3, # The type of 'None'.
list: 4,
dict: 5,
float: 6,
Expand All @@ -555,7 +578,7 @@ def __eq__(self, other):
datetime: 9,
date: 10,
str: 11,
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
}

# these are used to limit what type conversions we try to do
Expand Down Expand Up @@ -611,7 +634,7 @@ def __eq__(self, other):
(str, date),
# (int, str),
# (float, str),
(str, file_type)
(str, file_type),
),
}

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.10"
VERSION = "0.0.11"
# To install the library, run the following
#
# python setup.py install
Expand Down
2 changes: 1 addition & 1 deletion test/test_integration_raw_statistics_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setUp(self):
def tearDown(self) -> None:
self.client.close()

@unittest.skipIf(os.getenv("API_KEY") is None, "No API key")
@unittest.skip
def test_list_video_sessions_metadata(self):
video = self.videoApi.create(video_creation_payload=VideoCreationPayload(
title='session',
Expand Down
14 changes: 14 additions & 0 deletions test/test_integration_videos_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from pprint import pprint
from unittest import TestCase
import tempfile
import unittest
import apivideo
import string
Expand Down Expand Up @@ -61,6 +62,19 @@ def test_upload(self):
file.close()
self.api.delete(video.video_id)

@unittest.skipIf(os.getenv("API_KEY") is None, "No API key")
def test_upload_temporary_file(self):
video = self.api.create(video_creation_payload=VideoCreationPayload(
title='upload',
public=True,
tags=["bunny"]))

file = open("sample.mp4", "rb")
with tempfile.SpooledTemporaryFile(mode="wb") as temp:
temp.write(file.read())
self.api.upload(video.video_id, temp, _request_timeout=20)
file.close()
self.api.delete(video.video_id)

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

0 comments on commit 156ab10

Please sign in to comment.