-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
182 lines (148 loc) · 4.87 KB
/
file.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package go_hubspot
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
)
type IHubspotFileAPI interface {
GetPageURL() string
MakeFilePublic(fileId string) (string, error)
UploadFile(file []byte, folderPath, fileName string) (string, error)
}
// HubspotFileAPI is the structure to interact with Hubspot File API
type HubspotFileAPI struct {
URLTemplate string
APIKey string
PortalID string
httpClient IHTTPClient
}
// FileUploadResponse response of the file API
type FileUploadResponse struct {
Id string `json:"id"`
Url string `json:"url"`
}
// NewHubspotFileAPI creates new HubspotFileAPI and API key
func NewHubspotFileAPI(apiKey string, portalId string) HubspotFileAPI {
return HubspotFileAPI{
URLTemplate: "https://api.hubapi.com/files/v3/files%s",
APIKey: apiKey,
PortalID: portalId,
httpClient: HTTPClient{},
}
}
// GetPageURL gets query URL for a page of results
func (api HubspotFileAPI) GetPageURL() string {
return fmt.Sprintf(
api.URLTemplate,
"",
)
}
func (api HubspotFileAPI) GetPageUrlById(fileId string) string {
return fmt.Sprintf(
api.URLTemplate,
"/"+fileId,
)
}
type FileUploadOptions struct {
Access string `json:"access,omitempty"`
Overwrite bool `json:"overwrite,omitempty"`
DuplicateValidationStrategy string `json:"duplicateValidationStrategy,omitempty"`
DuplicateValidationScope string `json:"duplicateValidationScope,omitempty"`
}
func (api HubspotFileAPI) MakeFilePublic(fileId string) (string, error) {
options, err := json.Marshal(FileUploadOptions{Access: "PUBLIC_NOT_INDEXABLE"})
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPatch, api.GetPageUrlById(fileId), bytes.NewBuffer(options))
if err != nil {
return "", errors.New(fmt.Sprintf("Error whuke marshling options: %s", err.Error()))
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", api.APIKey))
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while making a request: %s", err.Error()))
}
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("Request to HubSpot File API failed: %s", resp.Status))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while reading response body: %s", err.Error()))
}
var hubspotResp FileUploadResponse
err = json.Unmarshal(body, &hubspotResp)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while unmarshaling HubSpot response: %s", err.Error()))
}
return hubspotResp.Url, nil
}
func (api HubspotFileAPI) UploadFile(file []byte, folderPath, fileName string) (string, error) {
var data bytes.Buffer
w := multipart.NewWriter(&data)
fileWriter, err := w.CreateFormFile("file", fileName)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while creating a file writer: %s", err.Error()))
}
_, err = fileWriter.Write(file)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while writing a file: %s", err.Error()))
}
err = w.WriteField("folderPath", folderPath)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while writing folder name: %s", err.Error()))
}
options := FileUploadOptions{
Access: "PUBLIC_NOT_INDEXABLE",
Overwrite: true,
DuplicateValidationStrategy: "NONE",
DuplicateValidationScope: "EXACT_FOLDER",
}
optionsBytes, err := json.Marshal(options)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while marshaling options: %s", err.Error()))
}
err = w.WriteField("options", string(optionsBytes))
if err != nil {
return "", errors.New(fmt.Sprintf("Error while writing options: %s", err.Error()))
}
err = w.Close()
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", api.GetPageURL(), &data)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while constructing a request: %s", err.Error()))
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", api.APIKey))
req.Header.Set("Content-Type", w.FormDataContentType())
resp, err := api.httpClient.Do(req)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while making a request: %s", err.Error()))
}
if resp.StatusCode != 201 {
return "", errors.New(fmt.Sprintf("Request to HubSpot File API failed: %s", resp.Status))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while reading response body: %s", err.Error()))
}
var hubspotResp FileUploadResponse
err = json.Unmarshal(body, &hubspotResp)
if err != nil {
return "", errors.New(fmt.Sprintf("Error while unmarshaling HubSpot response: %s", err.Error()))
}
url := fmt.Sprintf(
"https://app.hubspot.com/file-preview/%s/file/%s",
api.PortalID,
hubspotResp.Id,
)
return url, nil
}