1313import tempfile
1414from asyncio import run
1515from contextlib import suppress
16- from datetime import datetime
1716from pathlib import Path
1817from time import sleep
1918from typing import TYPE_CHECKING
@@ -124,8 +123,9 @@ async def check() -> None:
124123
125124@pytest .mark .spot
126125@pytest .mark .spot_auth
127- @pytest .mark .timeout ( 120 )
126+ @pytest .mark .parametrize ( "report" , [ "trades" , "ledgers" ] )
128127def test_spot_rest_async_client_post_report (
128+ report : str ,
129129 spot_api_key : str ,
130130 spot_secret_key : str ,
131131) -> None :
@@ -137,95 +137,67 @@ def test_spot_rest_async_client_post_report(
137137 async def check () -> None :
138138 client = SpotAsyncClient (spot_api_key , spot_secret_key )
139139
140- first_of_current_month = int (datetime .now ().replace (day = 1 ).timestamp ())
141140 try :
142- for report in ("trades" , "ledgers" ):
143- if report == "trades" :
144- fields = [
145- "ordertxid" ,
146- "time" ,
147- "ordertype" ,
148- "price" ,
149- "cost" ,
150- "fee" ,
151- "vol" ,
152- "margin" ,
153- "misc" ,
154- "ledgers" ,
155- ]
156- else :
157- fields = [
158- "refid" ,
159- "time" ,
160- "type" ,
161- "aclass" ,
162- "asset" ,
163- "amount" ,
164- "fee" ,
165- "balance" ,
166- ]
167-
168- export_descr = f"{ report } -export-{ random .randint (0 , 10000 )} "
169- response = await client .request (
170- "POST" ,
171- "/0/private/AddExport" ,
172- params = {
173- "format" : "CSV" ,
174- "fields" : fields ,
175- "report" : report ,
176- "description" : export_descr ,
177- "endtm" : first_of_current_month + 100 * 100 ,
178- },
179- timeout = 30 ,
180- )
181- assert is_not_error (response )
141+ export_descr = f"{ report } -export-{ random .randint (0 , 10000 )} "
142+ response = await client .request (
143+ "POST" ,
144+ "/0/private/AddExport" ,
145+ params = {
146+ "report" : report ,
147+ "description" : export_descr ,
148+ },
149+ )
150+ assert is_not_error (response )
151+ assert "id" in response
152+ sleep (2 )
153+
154+ status = await client .request (
155+ "POST" ,
156+ "/0/private/ExportStatus" ,
157+ params = {"report" : report },
158+ )
159+ assert isinstance (status , list )
160+ sleep (5 )
161+
162+ result = await client .request (
163+ "POST" ,
164+ "/0/private/RetrieveExport" ,
165+ params = {"id" : response ["id" ]},
166+ timeout = 30 ,
167+ return_raw = True ,
168+ )
169+
170+ with tempfile .TemporaryDirectory () as tmp_dir :
171+ file_path = Path (tmp_dir ) / f"{ export_descr } .zip"
172+
173+ with file_path .open ("wb" ) as file :
174+ async for chunk in result .content .iter_chunked (1024 ):
175+ file .write (chunk )
176+
177+ status = await client .request (
178+ "POST" ,
179+ "/0/private/ExportStatus" ,
180+ params = {"report" : report },
181+ )
182+ assert isinstance (status , list )
183+ for response in status :
184+ if response .get ("delete" ):
185+ # ignore already deleted reports
186+ continue
182187 assert "id" in response
188+ with suppress (Exception ):
189+ assert isinstance (
190+ await client .request (
191+ "POST" ,
192+ "/0/private/RemoveExport" ,
193+ params = {
194+ "id" : response ["id" ],
195+ "type" : "delete" ,
196+ },
197+ ),
198+ dict ,
199+ )
183200 sleep (2 )
184-
185- status = await client .request (
186- "POST" ,
187- "/0/private/ExportStatus" ,
188- params = {"report" : report },
189- )
190- assert isinstance (status , list )
191- sleep (5 )
192-
193- result = await client .request (
194- "POST" ,
195- "/0/private/RetrieveExport" ,
196- params = {"id" : response ["id" ]},
197- timeout = 30 ,
198- return_raw = True ,
199- )
200-
201- with tempfile .TemporaryDirectory () as tmp_dir :
202- file_path = Path (tmp_dir ) / f"{ export_descr } .zip"
203-
204- with file_path .open ("wb" ) as file :
205- async for chunk in result .content .iter_chunked (1024 ):
206- file .write (chunk )
207-
208- status = await client .request (
209- "POST" ,
210- "/0/private/ExportStatus" ,
211- params = {"report" : report },
212- )
213- assert isinstance (status , list )
214- for response in status :
215- assert "id" in response
216- with suppress (Exception ):
217- assert isinstance (
218- await client .request (
219- "POST" ,
220- "/0/private/RemoveExport" ,
221- params = {
222- "id" : response ["id" ],
223- "type" : "delete" ,
224- },
225- ),
226- dict ,
227- )
228- sleep (2 )
229201 finally :
230202 await client .close ()
231203
0 commit comments