@@ -27,11 +27,13 @@ func (a *App) Requests(method string, url string, headers map[string]string, bod
2727
2828 req .Header = GetHeader (headers )
2929
30- runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
31- log .Printf ("Requests Canceled: %v %v" , method , url )
32- cancel ()
33- })
34- defer runtime .EventsOff (a .Ctx , options .CancelId )
30+ if options .CancelId != "" {
31+ runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
32+ log .Printf ("Requests Canceled: %v %v" , method , url )
33+ cancel ()
34+ })
35+ defer runtime .EventsOff (a .Ctx , options .CancelId )
36+ }
3537
3638 resp , err := client .Do (req )
3739 if err != nil {
@@ -47,23 +49,25 @@ func (a *App) Requests(method string, url string, headers map[string]string, bod
4749 return HTTPResult {true , resp .StatusCode , resp .Header , string (b )}
4850}
4951
50- func (a * App ) Download (url string , path string , headers map [string ]string , event string , options RequestOptions ) HTTPResult {
51- log .Printf ("Download: %v %v %v, % v" , url , path , headers , options )
52+ func (a * App ) Download (method string , url string , path string , headers map [string ]string , event string , options RequestOptions ) HTTPResult {
53+ log .Printf ("Download: %s %s %s %v %s % v" , method , url , path , headers , event , options )
5254
5355 client , ctx , cancel := withRequestOptionsClient (options )
5456
55- req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
57+ req , err := http .NewRequestWithContext (ctx , method , url , nil )
5658 if err != nil {
5759 return HTTPResult {false , 500 , nil , err .Error ()}
5860 }
5961
6062 req .Header = GetHeader (headers )
6163
62- runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
63- log .Printf ("Download Canceled: %v %v" , url , path )
64- cancel ()
65- })
66- defer runtime .EventsOff (a .Ctx , options .CancelId )
64+ if options .CancelId != "" {
65+ runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
66+ log .Printf ("Download Canceled: %v %v" , url , path )
67+ cancel ()
68+ })
69+ defer runtime .EventsOff (a .Ctx , options .CancelId )
70+ }
6771
6872 resp , err := client .Do (req )
6973 if err != nil {
@@ -84,12 +88,7 @@ func (a *App) Download(url string, path string, headers map[string]string, event
8488 }
8589 defer file .Close ()
8690
87- reader := io .TeeReader (resp .Body , & WriteTracker {
88- Total : resp .ContentLength ,
89- EmitThreshold : 128 * 1024 , // 128KB
90- ProgressChange : event ,
91- App : a ,
92- })
91+ reader := wrapWithProgress (resp .Body , resp .ContentLength , event , a )
9392
9493 _ , err = io .Copy (file , reader )
9594 if err != nil {
@@ -99,8 +98,8 @@ func (a *App) Download(url string, path string, headers map[string]string, event
9998 return HTTPResult {true , resp .StatusCode , resp .Header , "Success" }
10099}
101100
102- func (a * App ) Upload (url string , path string , headers map [string ]string , event string , options RequestOptions ) HTTPResult {
103- log .Printf ("Upload: %v %v % v %v" , url , path , headers , options )
101+ func (a * App ) Upload (method string , url string , path string , headers map [string ]string , event string , options RequestOptions ) HTTPResult {
102+ log .Printf ("Upload: %s %s %s % v %s % v" , method , url , path , headers , event , options )
104103
105104 path = GetPath (path )
106105
@@ -123,12 +122,7 @@ func (a *App) Upload(url string, path string, headers map[string]string, event s
123122 return HTTPResult {false , 500 , nil , err .Error ()}
124123 }
125124
126- reader := io .TeeReader (file , & WriteTracker {
127- Total : fileStat .Size (),
128- EmitThreshold : 128 * 1024 , // 128KB
129- ProgressChange : event ,
130- App : a ,
131- })
125+ reader := wrapWithProgress (file , fileStat .Size (), event , a )
132126
133127 _ , err = io .Copy (part , reader )
134128 if err != nil {
@@ -142,13 +136,15 @@ func (a *App) Upload(url string, path string, headers map[string]string, event s
142136
143137 client , ctx , cancel := withRequestOptionsClient (options )
144138
145- runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
146- log .Printf ("Upload Canceled: %v %v" , url , path )
147- cancel ()
148- })
149- defer runtime .EventsOff (a .Ctx , options .CancelId )
139+ if options .CancelId != "" {
140+ runtime .EventsOn (a .Ctx , options .CancelId , func (data ... any ) {
141+ log .Printf ("Upload Canceled: %v %v" , url , path )
142+ cancel ()
143+ })
144+ defer runtime .EventsOff (a .Ctx , options .CancelId )
145+ }
150146
151- req , err := http .NewRequestWithContext (ctx , "POST" , url , body )
147+ req , err := http .NewRequestWithContext (ctx , method , url , body )
152148 if err != nil {
153149 return HTTPResult {false , 500 , nil , err .Error ()}
154150 }
@@ -174,10 +170,6 @@ func (wt *WriteTracker) Write(p []byte) (n int, err error) {
174170 n = len (p )
175171 wt .Progress += int64 (n )
176172
177- if wt .ProgressChange == "" {
178- return n , nil
179- }
180-
181173 shouldEmit := wt .Total <= 0 || wt .Progress - wt .LastEmitted >= wt .EmitThreshold || wt .Progress == wt .Total
182174 if shouldEmit {
183175 runtime .EventsEmit (wt .App .Ctx , wt .ProgressChange , wt .Progress , wt .Total )
@@ -187,6 +179,18 @@ func (wt *WriteTracker) Write(p []byte) (n int, err error) {
187179 return n , nil
188180}
189181
182+ func wrapWithProgress (r io.Reader , size int64 , event string , a * App ) io.Reader {
183+ if event == "" {
184+ return r
185+ }
186+ return io .TeeReader (r , & WriteTracker {
187+ Total : size ,
188+ EmitThreshold : 128 * 1024 ,
189+ ProgressChange : event ,
190+ App : a ,
191+ })
192+ }
193+
190194func withRequestOptionsClient (options RequestOptions ) (* http.Client , context.Context , context.CancelFunc ) {
191195 client := & http.Client {
192196 Timeout : GetTimeout (options .Timeout ),
0 commit comments