Skip to content

updated function comments and updated documentation #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type UploadState struct {
func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]string, formDataFields map[string]string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, encodingType string, out interface{}) (*http.Response, error) {
if encodingType != "byte" && encodingType != "base64" {
c.Sugar.Errorw("Invalid encoding type specified", zap.String("encodingType", encodingType))
return nil, fmt.Errorf("invalid encoding type: %s. Must be 'raw' or 'base64'", encodingType)
return nil, fmt.Errorf("invalid encoding type: %s. Must be 'byte' for rawBytes or 'base64' for base64 encoded content", encodingType)
}

if method != http.MethodPost && method != http.MethodPut {
Expand Down Expand Up @@ -215,7 +215,17 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel
}

// addFilePartWithEncoding adds a file part to the multipart writer with specified encoding.
// Supports both raw file content and base64 encoding based on encodingType parameter.
// Parameters:
// - writer: The multipart writer used to construct the request body
// - fieldName: The name of the form field for this file part
// - filePath: Path to the file to be uploaded
// - fileContentTypes: Map of content types for each file field
// - formDataPartHeaders: Map of custom headers for each form field
// - encodingType: The encoding to use ('byte' for raw bytes or 'base64' for base64 encoding)
// - sugar: Logger for progress and debug information
//
// Returns:
// - error: Any error encountered during the file part creation or upload process
func addFilePartWithEncoding(writer *multipart.Writer, fieldName, filePath string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, encodingType string, sugar *zap.SugaredLogger) error {
file, err := os.Open(filePath)
if err != nil {
Expand Down Expand Up @@ -264,6 +274,15 @@ func addFilePartWithEncoding(writer *multipart.Writer, fieldName, filePath strin
}

// createFilePartHeader creates the MIME header for a file part with the specified encoding type.
// Parameters:
// - fieldname: The name of the form field
// - filename: The name of the file being uploaded
// - contentType: The content type of the file
// - customHeaders: Additional headers to include in the part
// - encodingType: The encoding being used ('byte' or 'base64')
//
// Returns:
// - textproto.MIMEHeader: The constructed MIME header for the file part
func createFilePartHeader(fieldname, filename, contentType string, customHeaders http.Header, encodingType string) textproto.MIMEHeader {
header := textproto.MIMEHeader{}
header.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filepath.Base(filename)))
Expand Down
Loading