Skip to content

Commit

Permalink
remove unused function
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed Apr 21, 2024
1 parent b43d269 commit ab02894
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pkg/c8y/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func (c *Client) SendRequest(ctx context.Context, options RequestOptions) (*Resp
// the authentication required for the request
u, _ := url.Parse(c.BaseURL.String())
u.Path = path.Join(u.Path, currentPath)
req, err = prepareMultipartRequestWithBuffer(options.Method, u.String(), options.FormData)
req, err = prepareMultipartRequest(options.Method, u.String(), options.FormData)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/c8y/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *EventService) CreateBinary(ctx context.Context, filename string, ID str
u, _ := url.Parse(client.BaseURL.String())
u.Path = path.Join(u.Path, "/event/events/"+ID+"/binaries")

req, err := prepareMultipartRequestWithBuffer("POST", u.String(), values)
req, err := prepareMultipartRequest("POST", u.String(), values)
if err != nil {
err = errors.Wrap(err, "Could not create binary upload request object")
zap.S().Error(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/c8y/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (s *InventoryService) CreateBinary(ctx context.Context, binaryFile binary.M
u, _ := url.Parse(client.BaseURL.String())
u.Path = path.Join(u.Path, "/inventory/binaries")

req, err := prepareMultipartRequestWithBuffer("POST", u.String(), values)
req, err := prepareMultipartRequest("POST", u.String(), values)
if err != nil {
err = errors.Wrap(err, "Could not create binary upload request object")
zap.S().Error(err)
Expand Down
67 changes: 1 addition & 66 deletions pkg/c8y/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,8 @@ import (
"strings"
)

func prepareMultipartRequest(method string, url string, values map[string]io.Reader) (req *http.Request, err error) {
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)

// Sort formdata keys
keys := make([]string, 0, len(values))
for key := range values {
keys = append(keys, key)
}
sort.Strings(keys)

for _, key := range keys {
r := values[key]
if key == "filename" {
// Ignore filename as it is used to name the uploaded file
continue
}

var fw io.Writer
if x, ok := r.(io.Closer); ok {
defer x.Close()
}
// Add an image file
if x, ok := r.(*os.File); ok {

// Check if manual filename field was provided, otherwise use the basename
filename := filepath.Base(x.Name())
if manual_filename, ok := values["filename"]; ok {
if b, rErr := io.ReadAll(manual_filename); rErr == nil {
filename = string(b)
} else {
err = rErr
}
}
if fw, err = w.CreateFormFile(key, filename); err != nil {
return
}
} else {
// Add other fields
if fw, err = w.CreateFormField(key); err != nil {
return
}
}
if _, err = io.Copy(fw, r); err != nil {
return nil, err
}

}
// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()

// Now that you have a form, you can submit it to your handler.
req, err = http.NewRequest(method, url, &b)
if err != nil {
return
}
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Accept", "application/json")

return req, nil
}

// Prepare multipart form-data request which uses io.Pipe to buffer reading the message to ensure files won't be read entirely into memory
func prepareMultipartRequestWithBuffer(method string, url string, values map[string]io.Reader) (req *http.Request, err error) {
func prepareMultipartRequest(method string, url string, values map[string]io.Reader) (req *http.Request, err error) {
pr, pw := io.Pipe()

// Prepare a form that you will submit to that URL.
Expand Down

0 comments on commit ab02894

Please sign in to comment.