-
Notifications
You must be signed in to change notification settings - Fork 461
/
payload.go
50 lines (39 loc) · 856 Bytes
/
payload.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
package http
import (
"bytes"
"fmt"
"net/url"
"strconv"
"howett.net/plist"
)
type Payload interface {
data() ([]byte, error)
}
type XMLPayload struct {
Content map[string]interface{}
}
type URLPayload struct {
Content map[string]interface{}
}
func (p *XMLPayload) data() ([]byte, error) {
buffer := new(bytes.Buffer)
err := plist.NewEncoder(buffer).Encode(p.Content)
if err != nil {
return nil, fmt.Errorf("failed to encode plist: %w", err)
}
return buffer.Bytes(), nil
}
func (p *URLPayload) data() ([]byte, error) {
params := url.Values{}
for key, val := range p.Content {
switch t := val.(type) {
case string:
params.Add(key, val.(string))
case int:
params.Add(key, strconv.Itoa(val.(int)))
default:
return nil, fmt.Errorf("value type is not supported (%s)", t)
}
}
return []byte(params.Encode()), nil
}