@@ -10,6 +10,7 @@ import (
1010 "net/textproto"
1111 "os"
1212 "path/filepath"
13+ "strings"
1314 "sync"
1415 "time"
1516
@@ -422,3 +423,68 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger)
422423 }
423424 }
424425}
426+
427+ // DoImageMultiPartUpload performs a multipart request with a specifically formatted payload.
428+ // This is designed for APIs that expect a very specific multipart format, where the payload
429+ // needs to be constructed manually rather than using the standard multipart writer.
430+ func (c * Client ) DoImageMultiPartUpload (method , endpoint string , fileName string , base64Data string , customBoundary string , out interface {}) (* http.Response , error ) {
431+ if method != http .MethodPost && method != http .MethodPut {
432+ c .Sugar .Error ("HTTP method not supported for multipart request" , zap .String ("method" , method ))
433+ return nil , fmt .Errorf ("unsupported HTTP method: %s" , method )
434+ }
435+
436+ // Format the multipart payload with the specified boundary
437+ payload := fmt .Sprintf ("%s\r \n " +
438+ "Content-Disposition: form-data; name=\" file\" ; filename=\" %s\" \r \n " +
439+ "Content-Type: image/png\r \n \r \n " +
440+ "data:image/png;name=%s;base64,%s\r \n " +
441+ "%s--" ,
442+ customBoundary ,
443+ fileName ,
444+ fileName ,
445+ base64Data ,
446+ customBoundary )
447+
448+ url := (* c .Integration ).GetFQDN () + endpoint
449+
450+ // Create the request with the formatted payload
451+ req , err := http .NewRequest (method , url , strings .NewReader (payload ))
452+ if err != nil {
453+ c .Sugar .Errorw ("Failed to create request" , zap .Error (err ))
454+ return nil , fmt .Errorf ("failed to create request: %v" , err )
455+ }
456+
457+ req .Header .Add ("Accept" , "application/json" )
458+ req .Header .Add ("Content-Type" , fmt .Sprintf ("multipart/form-data; boundary=%s" , strings .TrimPrefix (customBoundary , "---" )))
459+
460+ (* c .Integration ).PrepRequestParamsAndAuth (req )
461+
462+ c .Sugar .Infow ("Sending custom multipart request" ,
463+ zap .String ("method" , method ),
464+ zap .String ("url" , url ),
465+ zap .String ("filename" , fileName ))
466+
467+ startTime := time .Now ()
468+ resp , err := c .http .Do (req )
469+ duration := time .Since (startTime )
470+
471+ if err != nil {
472+ c .Sugar .Errorw ("Failed to send request" ,
473+ zap .String ("method" , method ),
474+ zap .String ("endpoint" , endpoint ),
475+ zap .Error (err ))
476+ return nil , fmt .Errorf ("failed to send request: %v" , err )
477+ }
478+
479+ c .Sugar .Debugw ("Request sent successfully" ,
480+ zap .String ("method" , method ),
481+ zap .String ("endpoint" , endpoint ),
482+ zap .Int ("status_code" , resp .StatusCode ),
483+ zap .Duration ("duration" , duration ))
484+
485+ if resp .StatusCode >= 200 && resp .StatusCode < 300 {
486+ return resp , response .HandleAPISuccessResponse (resp , out , c .Sugar )
487+ }
488+
489+ return resp , response .HandleAPIErrorResponse (resp , c .Sugar )
490+ }
0 commit comments