feature: extract to multipart function
This commit is contained in:
parent
e6b9839e42
commit
d374239c6c
1 changed files with 18 additions and 13 deletions
|
@ -544,22 +544,12 @@ func (c *Client) BulkReport(ctx context.Context, data *BulkReportDatas) (*BulkRe
|
||||||
return nil, errors.New("bulk report: no csv data")
|
return nil, errors.New("bulk report: no csv data")
|
||||||
}
|
}
|
||||||
|
|
||||||
var requestBody bytes.Buffer
|
requestBody, contentType := toMultipartCsv(csvData)
|
||||||
writer := multipart.NewWriter(&requestBody)
|
req, err := c.NewRequest(http.MethodGet, endpoint, nil, requestBody)
|
||||||
formFile, err := writer.CreateFormFile("csv", "data.csv")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(formFile, csvData)
|
req.Header.Add("Content-Type", contentType)
|
||||||
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())
|
|
||||||
|
|
||||||
var result *BulkReportResult
|
var result *BulkReportResult
|
||||||
err = c.Do(ctx, req, &result)
|
err = c.Do(ctx, req, &result)
|
||||||
|
@ -569,6 +559,21 @@ func (c *Client) BulkReport(ctx context.Context, data *BulkReportDatas) (*BulkRe
|
||||||
return result, nil
|
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 {
|
type ClearAddressData struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
NumReportsDeleted int `json:"numReportsDeleted"`
|
NumReportsDeleted int `json:"numReportsDeleted"`
|
||||||
|
|
Loading…
Reference in a new issue