This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
forked from kmanley/freshdesk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
freshdesk.go
147 lines (126 loc) · 3 KB
/
freshdesk.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
package freshdesk
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type (
// Source of the ticket
Source int
// Status of the ticket
Status int
// Priority of the ticket
Priority int
// TicketType corresponds to the type of the ticket
TicketType string
)
const (
// Email ...
Email Source = 1 + iota
// Portal ...
Portal
// Phone ...
Phone
// Forum ...
Forum
// Twitter ...
Twitter
// Facebook ...
Facebook
// Chat ...
Chat
)
const (
// Open ...
Open Status = 2 + iota
// Pending ...
Pending
// Resolved ...
Resolved
// Closed ...
Closed
)
const (
// Low ...
Low Priority = 1 + iota
// Medium ...
Medium
// High ...
High
// Urgent ...
Urgent
)
const (
// Question ...
Question TicketType = "Question"
// Incident ...
Incident = "Incident"
// Problem ...
Problem = "Problem"
// FeatureRequest ...
FeatureRequest = "Feature Request"
// Lead ...
Lead = "Lead"
)
// API corresponds to the FreshdeskAPI. It can be used for mock
// implementations for testing.
type API interface {
CreateTicket(ticket *Ticket) (*Ticket, error)
}
// Client is a freshdesk client that allows access to the freshdesk
// API. It is save to use the client from different goroutines.
type Client struct {
Subdomain string
API string
httpClient *http.Client
}
// Ticket represents a single helpdesk ticket
type Ticket struct {
Email string `conform:"email" json:"email"`
Name string `conform:"name" json:"name"`
Subject string `conform:"trim,title" json:"subject"`
Description string `conform:"trim" json:"description"`
Type TicketType `conform:"trim" json:"type"`
Tags []string `json:"tags,omitempty"`
CustomFields map[string]string `json:"custom_fields,omitempty"`
Status Status `json:"status,omitempty"`
Priority Priority `json:"priority,omitempty"`
Source Source `json:"source,omitempty"`
}
// NewClient returns a new freshdesk client that uses the
// http.DefaultClient under the hood.
func NewClient(subdomain, api string) (*Client, error) {
return &Client{Subdomain: subdomain, API: api, httpClient: http.DefaultClient}, nil
}
// CreateTicket creates a new ticket.
func (c *Client) CreateTicket(ticket *Ticket) (*Ticket, error) {
var ret Ticket
b, err := json.Marshal(&ticket)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s.freshdesk.com/api/v2/tickets", c.Subdomain), bytes.NewReader(b))
if err != nil {
return nil, err
}
req.SetBasicAuth(c.API, "")
req.Header.Add("Content-type", "application/json")
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode >= 400 {
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("freshdesk server didn't like the request (status code: %d): %s", res.StatusCode, bs)
}
if err = json.NewDecoder(res.Body).Decode(&ret); err != nil {
return nil, err
}
return &ret, nil
}