-
Notifications
You must be signed in to change notification settings - Fork 28
/
crm_imports_start.go
103 lines (89 loc) · 2.86 KB
/
crm_imports_start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package hubspot
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/textproto"
)
type CrmImportConfig struct {
Name string `json:"name"`
MarketableContactImport bool `json:"marketableContactImport"`
ImportOperations map[string]string `json:"importOperations"`
Files []CrmImportFileConfig `json:"files"`
}
type CrmImportFilePageConfig struct {
HasHeader bool `json:"hasHeader"`
ColumnMappings []CrmImportColumnMapping `json:"columnMappings"`
}
type CrmImportFileConfig struct {
FileName string `json:"fileName"`
FileFormat string `json:"fileFormat"`
DateFormat string `json:"dateFormat"`
FileImportPage CrmImportFilePageConfig `json:"fileImportPage"`
// Data is the CSV or Spreadsheet data for this file.
Data io.Reader `json:"-"`
}
type CrmImportColumnMapping struct {
ColumnObjectTypeId string `json:"columnObjectTypeId"`
ColumnName string `json:"columnName"`
PropertyName string `json:"propertyName"`
IdColumnType string `json:"idColumnType,omitempty"`
}
func addJSONtoMultipart(writer *multipart.Writer, importRequest *CrmImportConfig) error {
data, err := json.Marshal(importRequest)
if err != nil {
return err
}
header := textproto.MIMEHeader{}
header.Set("Content-Disposition", "form-data; name=\"importRequest\"")
part, err := writer.CreatePart(header)
if err != nil {
return err
}
if _, err := part.Write(data); err != nil {
return err
}
return nil
}
func addFilesToMultipart(writer *multipart.Writer, importRequest *CrmImportConfig) error {
for _, fileDef := range importRequest.Files {
csvHeader := textproto.MIMEHeader{}
csvHeader.Set("Content-Disposition", fmt.Sprintf("form-data; name=\"files\"; filename=\"%s\"", fileDef.FileName))
csvPart, err := writer.CreatePart(csvHeader)
if err != nil {
return err
}
fileData, err := io.ReadAll(fileDef.Data)
if err != nil {
return err
}
if _, err := csvPart.Write(fileData); err != nil {
return err
}
}
return nil
}
func (s *CrmImportsServiceOp) Start(importRequest *CrmImportConfig) (interface{}, error) {
resource := make(map[string]interface{})
// Body is our final result that we pass to postMultipart
body := &bytes.Buffer{}
// Write the importRequest to the multipart message
writer := multipart.NewWriter(body)
// Write a part for the JSON metadata.
if err := addJSONtoMultipart(writer, importRequest); err != nil {
return nil, err
}
// Write file data to multipart.
if err := addFilesToMultipart(writer, importRequest); err != nil {
return nil, err
}
if err := writer.Close(); err != nil {
return nil, err
}
if err := s.client.PostMultipart(s.crmImportsPath, writer.Boundary(), body.Bytes(), &resource); err != nil {
return nil, err
}
return resource, nil
}