From d374239c6c72d445543ccf843857086d80146a23 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Thu, 1 Aug 2024 14:36:51 +0000 Subject: [PATCH] feature: extract to multipart function --- pkg/abuseipdb/client.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/pkg/abuseipdb/client.go b/pkg/abuseipdb/client.go index 8140a05..0426ebf 100644 --- a/pkg/abuseipdb/client.go +++ b/pkg/abuseipdb/client.go @@ -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"`