Skip to content

Commit

Permalink
change behaviour when base upload url is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsocha2 committed Jun 3, 2024
1 parent fe6267c commit c453d23
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
6 changes: 4 additions & 2 deletions boxsdk/object/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def create_upload_session(
The new name of the file version that will be uploaded.
:param use_upload_session_urls:
The parameter detrermining what urls to use to perform chunked upload.
If True, the urls returned by create_upload_session() endpoint response will be used.
If True, the urls returned by create_upload_session() endpoint response will be used,
unless a custom API.UPLOAD_URL was set in the config.
If False, the base upload url will be used.
:returns:
A :class:`UploadSession` object.
Expand Down Expand Up @@ -96,7 +97,8 @@ def get_chunked_uploader(
Indicates whether the file should be renamed or not.
:param use_upload_session_urls:
The parameter detrermining what urls to use to perform chunked upload.
If True, the urls returned by create_upload_session() endpoint response will be used.
If True, the urls returned by create_upload_session() endpoint response will be used,
unless a custom API.UPLOAD_URL was set in the config.
If False, the base upload url will be used.
:returns:
A :class:`ChunkedUploader` object.
Expand Down
6 changes: 4 additions & 2 deletions boxsdk/object/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def create_upload_session(self, file_size: int, file_name: str, use_upload_sessi
The name of the file that will be uploaded.
:param use_upload_session_urls:
The parameter detrermining what urls to use to perform chunked upload.
If True, the urls returned by create_upload_session() endpoint response will be used.
If True, the urls returned by create_upload_session() endpoint response will be used,
unless a custom API.UPLOAD_URL was set in the config.
If False, the base upload url will be used.
:returns:
A :class:`UploadSession` object.
Expand Down Expand Up @@ -160,7 +161,8 @@ def get_chunked_uploader(
If not specified, the name from the local system is used.
:param use_upload_session_urls:
The parameter detrermining what urls to use to perform chunked upload.
If True, the urls returned by create_upload_session() endpoint response will be used.
If True, the urls returned by create_upload_session() endpoint response will be used,
unless a custom API.UPLOAD_URL was set in the config.
If False, the base upload url will be used.
:returns:
A :class:`ChunkedUploader` object.
Expand Down
4 changes: 3 additions & 1 deletion boxsdk/object/upload_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from boxsdk.util.api_call_decorator import api_call
from boxsdk.util.chunked_uploader import ChunkedUploader
from boxsdk.session.session import Session
from boxsdk.config import API
from .base_object import BaseObject
from ..pagination.limit_offset_based_dict_collection import LimitOffsetBasedDictCollection

Expand All @@ -20,6 +21,7 @@
class UploadSession(BaseObject):
_item_type = 'upload_session'
_parent_item_type = 'file'
_default_upload_url = API.UPLOAD_URL

def __init__(
self, session: Session, object_id: str, response_object: dict = None, use_upload_session_urls: bool = True
Expand All @@ -32,7 +34,7 @@ def get_url(self, *args: Any, url_key: str = None) -> str:
Base class override. Endpoint is a little different - it's /files/upload_sessions.
"""
session_endpoints = getattr(self, 'session_endpoints', {})
if self._use_upload_session_urls and url_key in session_endpoints:
if self._use_upload_session_urls and url_key in session_endpoints and self.session.api_config.UPLOAD_URL == self._default_upload_url:
return session_endpoints[url_key]

return self._session.get_url(
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ API.OAUTH2_AUTHORIZE_URL = 'https://my-company.com/authorize'

### Upload URL
The default URL used when uploading files to Box can be changed by assigning a new value to the `API.UPLOAD_URL` field.
If this variable is ever changed from default value, the SDK will alwayse use this URL to upload files to Box,
even if `use_upload_session_urls` is set to `True` while creating an upload session for a chunked upload.

```python
from boxsdk.config import API
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ API.CHUNK_UPLOAD_THREADS = 6
The SDK provides a method of automatically handling a chunked upload. First get a folder you want to upload the file to.
Then call [`folder.get_chunked_uploader(file_path, rename_file=False, use_upload_session_urls=True)`][get_chunked_uploader_for_file]
to retrieve a [`ChunkedUploader`][chunked_uploader_class] object. Setting `use_upload_session_urls` to `True` inilializes
the uploader that utlizies urls returned by the `Create Upload Session` endpoint response. Setting `use_upload_session_urls`
to `False` inilializes the uploader that uses always base upload urls.
the uploader that utlizies urls returned by the `Create Upload Session` endpoint response unless a custom
API.UPLOAD_URL was set in the config. Setting `use_upload_session_urls` to `False` inilializes the uploader that uses always base upload urls.
Calling the method [`chunked_upload.start()`][start] will kick off the chunked upload process and return the [File][file_class]
object that was uploaded.

Expand Down
54 changes: 36 additions & 18 deletions test/unit/object/test_upload_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


@pytest.fixture()
def test_upload_session_using_upload_session_urls(mock_box_session):
def upload_session_using_upload_session_urls(mock_box_session):
upload_session_response_object = {
'part_size': 8,
'total_parts': 10,
Expand All @@ -40,7 +40,7 @@ def test_upload_session_using_upload_session_urls(mock_box_session):


@pytest.fixture()
def test_upload_session_not_using_upload_session_urls(mock_box_session):
def upload_session_not_using_upload_session_urls(mock_box_session):
upload_session_response_object = {
'part_size': 8,
'total_parts': 10,
Expand All @@ -57,8 +57,8 @@ def test_upload_session_not_using_upload_session_urls(mock_box_session):
@pytest.mark.parametrize(
'test_upload_session, expected_url',
[
(lazy_fixture('test_upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['list_parts']),
(lazy_fixture('test_upload_session_not_using_upload_session_urls'),
(lazy_fixture('upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['list_parts']),
(lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/parts')
])
def test_get_parts(mock_box_session, test_upload_session, expected_url):
Expand Down Expand Up @@ -86,8 +86,8 @@ def test_get_parts(mock_box_session, test_upload_session, expected_url):
@pytest.mark.parametrize(
'test_upload_session, expected_url',
[
(lazy_fixture('test_upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['abort']),
(lazy_fixture('test_upload_session_not_using_upload_session_urls'),
(lazy_fixture('upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['abort']),
(lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}')
])
def test_abort(mock_box_session, test_upload_session, expected_url):
Expand All @@ -100,8 +100,8 @@ def test_abort(mock_box_session, test_upload_session, expected_url):
@pytest.mark.parametrize(
'test_upload_session, expected_url',
[
(lazy_fixture('test_upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['upload_part']),
(lazy_fixture('test_upload_session_not_using_upload_session_urls'),
(lazy_fixture('upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['upload_part']),
(lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}')
])
def test_upload_part_bytes(mock_box_session, test_upload_session, expected_url):
Expand Down Expand Up @@ -134,8 +134,8 @@ def test_upload_part_bytes(mock_box_session, test_upload_session, expected_url):
@pytest.mark.parametrize(
'test_upload_session, expected_url',
[
(lazy_fixture('test_upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['commit']),
(lazy_fixture('test_upload_session_not_using_upload_session_urls'),
(lazy_fixture('upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['commit']),
(lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/commit')
])
def test_commit(mock_box_session, test_upload_session, expected_url):
Expand Down Expand Up @@ -193,12 +193,12 @@ def test_commit(mock_box_session, test_upload_session, expected_url):
'test_upload_session, expected_get_url, expected_commit_url',
[
(
lazy_fixture('test_upload_session_using_upload_session_urls'),
lazy_fixture('upload_session_using_upload_session_urls'),
SESSION_ENDPOINTS['list_parts'],
SESSION_ENDPOINTS['commit']
),
(
lazy_fixture('test_upload_session_not_using_upload_session_urls'),
lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/parts',
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/commit'
)
Expand Down Expand Up @@ -256,8 +256,8 @@ def test_commit_with_missing_params(mock_box_session, test_upload_session, expec
@pytest.mark.parametrize(
'test_upload_session, expected_url',
[
(lazy_fixture('test_upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['commit']),
(lazy_fixture('test_upload_session_not_using_upload_session_urls'),
(lazy_fixture('upload_session_using_upload_session_urls'), SESSION_ENDPOINTS['commit']),
(lazy_fixture('upload_session_not_using_upload_session_urls'),
f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/commit')
])
def test_commit_returns_none_when_202_is_returned(mock_box_session, test_upload_session, expected_url):
Expand Down Expand Up @@ -303,8 +303,8 @@ def test_commit_returns_none_when_202_is_returned(mock_box_session, test_upload_
@pytest.mark.parametrize(
'test_upload_session',
[
lazy_fixture('test_upload_session_using_upload_session_urls'),
lazy_fixture('test_upload_session_not_using_upload_session_urls'),
lazy_fixture('upload_session_using_upload_session_urls'),
lazy_fixture('upload_session_not_using_upload_session_urls'),
])
def test_get_chunked_uploader_for_stream(test_upload_session):
file_size = 197520
Expand All @@ -317,8 +317,8 @@ def test_get_chunked_uploader_for_stream(test_upload_session):
@pytest.mark.parametrize(
'test_upload_session',
[
lazy_fixture('test_upload_session_using_upload_session_urls'),
lazy_fixture('test_upload_session_not_using_upload_session_urls'),
lazy_fixture('upload_session_using_upload_session_urls'),
lazy_fixture('upload_session_not_using_upload_session_urls'),
])
def test_get_chunked_uploader(mock_content_response, mock_file_path, test_upload_session):
mock_file_stream = io.BytesIO(mock_content_response.content)
Expand All @@ -328,3 +328,21 @@ def test_get_chunked_uploader(mock_content_response, mock_file_path, test_upload
with patch('boxsdk.object.upload_session.open', return_value=mock_file_stream):
chunked_uploader = test_upload_session.get_chunked_uploader(mock_file_path)
assert isinstance(chunked_uploader, ChunkedUploader)


def test_get_url_do_not_use_session_urls_if_base_url_was_changed(upload_session_using_upload_session_urls):
old_base_upload_url = API.UPLOAD_URL
new_base_upload_url = 'https://new-upload.box.com/api/2.0'
API.UPLOAD_URL = new_base_upload_url
try:
url = upload_session_using_upload_session_urls.get_url('commit', url_key='commit')
assert url == f'{new_base_upload_url}/files/upload_sessions/{UPLOAD_SESSION_ID}/commit'
assert url != SESSION_ENDPOINTS['commit']
finally:
API.UPLOAD_URL = old_base_upload_url


def test_get_url_uses_session_urls_if_base_url_was_not_changed(upload_session_using_upload_session_urls):
url = upload_session_using_upload_session_urls.get_url('commit', url_key='commit')
assert url != f'{API.UPLOAD_URL}/files/upload_sessions/{UPLOAD_SESSION_ID}/commit'
assert url == SESSION_ENDPOINTS['commit']

0 comments on commit c453d23

Please sign in to comment.