1
0
Fork 0

feature: extract to multipart function

This commit is contained in:
Seraphim Strub 2024-08-01 14:36:51 +00:00
parent e6b9839e42
commit d374239c6c

View file

@ -544,22 +544,12 @@ func (c *Client) BulkReport(ctx context.Context, data *BulkReportDatas) (*BulkRe
return nil, errors.New("bulk report: no csv data")
}
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
formFile, err := writer.CreateFormFile("csv", "data.csv")
requestBody, contentType := toMultipartCsv(csvData)
req, err := c.NewRequest(http.MethodGet, endpoint, nil, requestBody)
if err != nil {
return nil, err
}
_, err = io.Copy(formFile, csvData)
if err != nil {
return nil, err
}
writer.Close()
req, err := c.NewRequest(http.MethodGet, endpoint, nil, &requestBody)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
req.Header.Add("Content-Type", contentType)
var result *BulkReportResult
err = c.Do(ctx, req, &result)
@ -569,6 +559,21 @@ func (c *Client) BulkReport(ctx context.Context, data *BulkReportDatas) (*BulkRe
return result, nil
}
func toMultipartCsv(data io.Reader) (io.Reader, string) {
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
formFile, err := writer.CreateFormFile("csv", "data.csv")
if err != nil {
return nil, ""
}
_, err = io.Copy(formFile, data)
if err != nil {
return nil, ""
}
writer.Close()
return &requestBody, writer.FormDataContentType()
}
type ClearAddressData struct {
Data struct {
NumReportsDeleted int `json:"numReportsDeleted"`