forked from ProtonMail/go-proton-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_import_types.go
61 lines (47 loc) · 1.12 KB
/
message_import_types.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
package proton
import (
"encoding/json"
"github.com/ProtonMail/gluon/rfc822"
"github.com/go-resty/resty/v2"
)
type ImportReq struct {
Metadata ImportMetadata
Message []byte
}
type namedImportReq struct {
ImportReq
Name string
}
type ImportMetadata struct {
AddressID string
LabelIDs []string
Unread Bool
Flags MessageFlag
}
type ImportRes struct {
APIError
MessageID string
}
func buildImportReqFields(req []namedImportReq) ([]*resty.MultipartField, error) {
var fields []*resty.MultipartField
metadata := make(map[string]ImportMetadata, len(req))
for _, req := range req {
metadata[req.Name] = req.Metadata
fields = append(fields, &resty.MultipartField{
Param: req.Name,
FileName: req.Name + ".eml",
ContentType: string(rfc822.MessageRFC822),
Stream: resty.NewByteMultipartStream(append(req.Message, "\r\n"...)),
})
}
b, err := json.Marshal(metadata)
if err != nil {
return nil, err
}
fields = append(fields, &resty.MultipartField{
Param: "Metadata",
ContentType: "application/json",
Stream: resty.NewByteMultipartStream(b),
})
return fields, nil
}