11from typing import Callable , Optional , List , Tuple , Any , Dict
22from io import BytesIO
3- from datetime import datetime
43from asyncio import Future
5- from datetime import datetime
6- from dateutil .relativedelta import relativedelta
4+ from datetime import datetime , timedelta
75
86from kiota_abstractions .serialization .parsable import Parsable
97from kiota_abstractions .method import Method
@@ -19,32 +17,34 @@ class LargeFileUploadTask:
1917
2018 def __init__ (
2119 self ,
22- upload_session : Parsable ,
20+ upload_session : LargeFileUploadSession ,
2321 request_adapter : RequestAdapter ,
2422 stream : BytesIO , # counter check this
2523 max_chunk_size : int = 1024 # 4 * 1024 * 1024 - use smaller chnuks for testing
2624 ):
27- self .upload_session = upload_session
28- self .request_adapter = request_adapter
25+ self ._upload_session = upload_session
26+ self ._request_adapter = request_adapter
2927 self .stream = stream
3028 self .file_size = stream .getbuffer ().nbytes
3129 self .max_chunk_size = max_chunk_size
3230 cleaned_value = self .check_value_exists (
3331 upload_session , 'get_next_expected_range' , ['next_expected_range' , 'NextExpectedRange' ]
3432 )
3533 self .next_range = cleaned_value [0 ]
36- self .chunks = int ((self .file_size / max_chunk_size ) + 0.5 )
34+ self ._chunks = int ((self .file_size / max_chunk_size ) + 0.5 )
3735 self .on_chunk_upload_complete : Optional [Callable [[int , int ], None ]] = None
3836
39- def get_upload_session (self ) -> Parsable :
40- return self .upload_session
37+ @property
38+ def upload_session (self ) -> Parsable :
39+ return self ._upload_session
4140
4241 @staticmethod
4342 async def create_upload_session (request_adapter : RequestAdapter , request_body , url : str ):
4443 request_information = RequestInformation ()
4544 base_url = request_adapter .base_url .rstrip ('/' )
4645 path = url .lstrip ('/' )
4746 new_url = f"{ base_url } /{ path } "
47+ print (f"New url { new_url } " )
4848 request_information .url = new_url
4949 request_information .http_method = Method .POST
5050 request_information .set_content_from_parsable (
@@ -56,34 +56,33 @@ async def create_upload_session(request_adapter: RequestAdapter, request_body, u
5656 request_information , LargeFileUploadSession .create_from_discriminator_value , error_map
5757 )
5858
59- def get_adapter (self ) -> RequestAdapter :
60- return self .request_adapter
59+ @property
60+ def request_adapter (self ) -> RequestAdapter :
61+ return self ._request_adapter
6162
62- def get_chunks (self ) -> int :
63- return self .chunks
63+ @property
64+ def chunks (self ) -> int :
65+ return self ._chunks
6466
65- def upload_session_expired (self , upload_session = None ):
67+ def upload_session_expired (self , upload_session : LargeFileUploadSession = None ) -> bool :
6668 now = datetime .now ()
67-
68- if upload_session is None :
69- upload_session = self .upload_session
70-
71- if not hasattr (upload_session , 'expiration_date_time' ):
72- raise Exception ('The upload session does not contain an expiry datetime.' )
73-
69+ upload_session = upload_session or self ._upload_session
70+ if not hasattr (upload_session , "expiration_date_time" ):
71+ raise ValueError ("Upload session does not have an expiration date time" )
7472 expiry = upload_session .expiration_date_time
75- print (f"Expiry { expiry } " )
76-
73+ print (expiry )
7774 if expiry is None :
78- raise ValueError ('The upload session does not contain a valid expiry date.' )
79-
75+ raise ValueError ("Expiry is None" )
8076 if isinstance (expiry , str ):
8177 then = datetime .strptime (expiry , "%Y-%m-%dT%H:%M:%S" )
82- else :
78+ elif isinstance ( expiry , datetime ) :
8379 then = expiry
84- interval = relativedelta (now , then )
85-
86- if interval .days < 0 or (interval .days == 0 and interval .seconds < 0 ):
80+ else :
81+ raise ValueError ("Expiry is not a string or datetime" )
82+ interval = now - then
83+ if not isinstance (interval , timedelta ):
84+ raise ValueError ("Interval is not a timedelta" )
85+ if interval .total_seconds () <= 0 :
8786 return True
8887 return False
8988
@@ -103,6 +102,7 @@ async def upload(self, after_chunk_upload=None):
103102 range_parts = self .next_range [0 ].split ("-" ) if self .next_range else ['0' , '0' ]
104103 end = min (int (range_parts [0 ]) + self .max_chunk_size - 1 , self .file_size )
105104 uploaded_range = [range_parts [0 ], end ]
105+ print (f"File size { self .file_size } " )
106106 while self .chunks > 0 :
107107 session = process_next
108108 future = Future ()
0 commit comments