mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 Zendesk extracted to new config format
This commit is contained in:
parent
cfdfb044da
commit
4b3f957c61
3
main.go
3
main.go
@ -313,7 +313,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
|
|||||||
settings := weather.NewSettingsFromYAML(wtf.Config)
|
settings := weather.NewSettingsFromYAML(wtf.Config)
|
||||||
widget = weather.NewWidget(app, pages, settings)
|
widget = weather.NewWidget(app, pages, settings)
|
||||||
case "zendesk":
|
case "zendesk":
|
||||||
widget = zendesk.NewWidget(app)
|
settings := zendesk.NewSettingsFromYAML(wtf.Config)
|
||||||
|
widget = zendesk.NewWidget(app, settings)
|
||||||
default:
|
default:
|
||||||
widget = unknown.NewWidget(app, widgetName)
|
widget = unknown.NewWidget(app, widgetName)
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/wtfutil/wtf/wtf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
@ -16,34 +13,20 @@ type Resource struct {
|
|||||||
Raw string
|
Raw string
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiKey() string {
|
|
||||||
return wtf.Config.UString(
|
|
||||||
"wtf.mods.zendesk.apiKey",
|
|
||||||
os.Getenv("ZENDESK_API"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func subdomain() string {
|
|
||||||
return wtf.Config.UString(
|
|
||||||
"wtf.mods.zendesk.subdomain",
|
|
||||||
os.Getenv("ZENDESK_SUBDOMAIN"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func errHandler(err error) {
|
func errHandler(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func api(key string, meth string, path string, params string) (*Resource, error) {
|
func (widget *Widget) api(meth string, path string, params string) (*Resource, error) {
|
||||||
trn := &http.Transport{}
|
trn := &http.Transport{}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: trn,
|
Transport: trn,
|
||||||
}
|
}
|
||||||
|
|
||||||
baseURL := fmt.Sprintf("https://%v.zendesk.com/api/v2", subdomain())
|
baseURL := fmt.Sprintf("https://%v.zendesk.com/api/v2", widget.settings.subdomain)
|
||||||
URL := baseURL + "/tickets.json?sort_by=status"
|
URL := baseURL + "/tickets.json?sort_by=status"
|
||||||
|
|
||||||
req, err := http.NewRequest(meth, URL, bytes.NewBufferString(params))
|
req, err := http.NewRequest(meth, URL, bytes.NewBufferString(params))
|
||||||
@ -53,9 +36,8 @@ func api(key string, meth string, path string, params string) (*Resource, error)
|
|||||||
|
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
username := wtf.Config.UString("wtf.mods.zendesk.username")
|
apiUser := fmt.Sprintf("%v/token", widget.settings.username)
|
||||||
apiUser := fmt.Sprintf("%v/token", username)
|
req.SetBasicAuth(apiUser, widget.settings.apiKey)
|
||||||
req.SetBasicAuth(apiUser, key)
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -70,5 +52,4 @@ func api(key string, meth string, path string, params string) (*Resource, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Resource{Response: &resp, Raw: string(data)}, nil
|
return &Resource{Response: &resp, Raw: string(data)}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
32
modules/zendesk/settings.go
Normal file
32
modules/zendesk/settings.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package zendesk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/olebedev/config"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
common *cfg.Common
|
||||||
|
|
||||||
|
apiKey string
|
||||||
|
status string
|
||||||
|
subdomain string
|
||||||
|
username string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
|
||||||
|
localConfig, _ := ymlConfig.Get("wtf.mods.zendesk")
|
||||||
|
|
||||||
|
settings := Settings{
|
||||||
|
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
|
||||||
|
|
||||||
|
apiKey: localConfig.UString("apiKey", os.Getenv("ZENDESK_API")),
|
||||||
|
status: localConfig.UString("status"),
|
||||||
|
subdomain: localConfig.UString("subdomain", os.Getenv("ZENDESK_SUBDOMAIN")),
|
||||||
|
username: localConfig.UString("username"),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &settings
|
||||||
|
}
|
@ -45,7 +45,7 @@ type Ticket struct {
|
|||||||
Fields interface{} `json:"fields"`
|
Fields interface{} `json:"fields"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func listTickets(pag ...string) (*TicketArray, error) {
|
func (widget *Widget) listTickets(pag ...string) (*TicketArray, error) {
|
||||||
|
|
||||||
TicketStruct := &TicketArray{}
|
TicketStruct := &TicketArray{}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ func listTickets(pag ...string) (*TicketArray, error) {
|
|||||||
} else {
|
} else {
|
||||||
path = pag[0]
|
path = pag[0]
|
||||||
}
|
}
|
||||||
resource, err := api(apiKey(), "GET", path, "")
|
resource, err := widget.api("GET", path, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -66,14 +66,14 @@ func listTickets(pag ...string) (*TicketArray, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTickets(ticketStatus string) (*TicketArray, error) {
|
func (widget *Widget) newTickets() (*TicketArray, error) {
|
||||||
newTicketArray := &TicketArray{}
|
newTicketArray := &TicketArray{}
|
||||||
tickets, err := listTickets()
|
tickets, err := widget.listTickets(widget.settings.apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, Ticket := range tickets.Tickets {
|
for _, Ticket := range tickets.Tickets {
|
||||||
if Ticket.Status == ticketStatus && Ticket.Status != "closed" && Ticket.Status != "solved" {
|
if Ticket.Status == widget.settings.status && Ticket.Status != "closed" && Ticket.Status != "solved" {
|
||||||
newTicketArray.Tickets = append(newTicketArray.Tickets, Ticket)
|
newTicketArray.Tickets = append(newTicketArray.Tickets, Ticket)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,11 +14,14 @@ type Widget struct {
|
|||||||
|
|
||||||
result *TicketArray
|
result *TicketArray
|
||||||
selected int
|
selected int
|
||||||
|
settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget(app *tview.Application) *Widget {
|
func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: wtf.NewTextWidget(app, "Zendesk", "zendesk", true),
|
TextWidget: wtf.NewTextWidget(app, "Zendesk", "zendesk", true),
|
||||||
|
|
||||||
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||||
@ -28,8 +31,7 @@ func NewWidget(app *tview.Application) *Widget {
|
|||||||
|
|
||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
ticketStatus := wtf.Config.UString("wtf.mods.zendesk.status")
|
ticketArray, err := widget.newTickets()
|
||||||
ticketArray, err := newTickets(ticketStatus)
|
|
||||||
ticketArray.Count = len(ticketArray.Tickets)
|
ticketArray.Count = len(ticketArray.Tickets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -102,7 +104,7 @@ func (widget *Widget) openTicket() {
|
|||||||
sel := widget.selected
|
sel := widget.selected
|
||||||
if sel >= 0 && widget.result != nil && sel < len(widget.result.Tickets) {
|
if sel >= 0 && widget.result != nil && sel < len(widget.result.Tickets) {
|
||||||
issue := &widget.result.Tickets[widget.selected]
|
issue := &widget.result.Tickets[widget.selected]
|
||||||
ticketUrl := fmt.Sprintf("https://%s.zendesk.com/agent/tickets/%d", subdomain(), issue.Id)
|
ticketUrl := fmt.Sprintf("https://%s.zendesk.com/agent/tickets/%d", widget.settings.subdomain, issue.Id)
|
||||||
wtf.OpenFile(ticketUrl)
|
wtf.OpenFile(ticketUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user