-
Notifications
You must be signed in to change notification settings - Fork 0
/
zammad.go
78 lines (56 loc) · 1.45 KB
/
zammad.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
package zammad
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Zammad struct {
Client *http.Client
Key string
Server string
}
func NewDefaultClient(server string, key string) (*Zammad, error) {
return &Zammad{Key: key, Server: server, Client: &http.Client{Transport: zammadAgentTransport{
apikey: key,
base: http.DefaultTransport,
}}}, nil
}
func (z *Zammad) RawNewTicket(body []byte) ([]byte, error) {
url := fmt.Sprintf("%v/api/v1/tickets", z.Server)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
resp, err := z.Client.Do(req)
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
func (z *Zammad) AddTagToTicket(id int, tag string) error {
reqBody := map[string]interface{}{
"item": tag,
"object": "Ticket",
"o_id": id,
}
reqBodyBytes, err := json.Marshal(reqBody)
if err != nil {
return err
}
url := fmt.Sprintf("%v/api/v1/tags/add", z.Server)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(reqBodyBytes))
req.Header.Set("Content-Type", "application/json")
_, err = z.Client.Do(req)
if err != nil {
return err
}
return nil
}
type zammadAgentTransport struct {
apikey string
base http.RoundTripper
}
func (t zammadAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Token token=%+v", t.apikey))
return t.base.RoundTrip(req)
}