1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/zendesk/client.go
Chris Cummer 3a388fba23
20191215 code improvements (#790)
* Upgrade godo to latest
* Fix a bunch of issues found by
* Running staticcheck on a codebase for the first time is a sobering experience
* go mod tidy
* More static improvements

Signed-off-by: Chris Cummer <chriscummer@me.com>
2019-12-16 20:25:29 -08:00

49 lines
942 B
Go

package zendesk
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
type Resource struct {
Response interface{}
Raw string
}
func (widget *Widget) api(meth string, path string, params string) (*Resource, error) {
trn := &http.Transport{}
client := &http.Client{
Transport: trn,
}
baseURL := fmt.Sprintf("https://%v.zendesk.com/api/v2", widget.settings.subdomain)
URL := baseURL + "/tickets.json?sort_by=status"
req, err := http.NewRequest(meth, URL, bytes.NewBufferString(params))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
apiUser := fmt.Sprintf("%v/token", widget.settings.username)
req.SetBasicAuth(apiUser, widget.settings.apiKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &Resource{Response: &resp, Raw: string(data)}, nil
}