1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Clean up the Zendesk module's API credentials loading

This commit is contained in:
Chris Cummer 2018-07-31 15:34:42 -07:00
parent 2fe8164e20
commit b6036a3eae
6 changed files with 58 additions and 34 deletions

View File

@ -14,15 +14,6 @@ Displays all Trello cards on specified lists.
wtf/trello/ wtf/trello/
``` ```
## Required ENV Variables
<span class="caption">Key:</span> `WTF_TRELLO_APP_KEY` <br />
<span class="caption">Value:</span> Your Trello App Key. <br />
<span class="caption">Key:</span> `WTF_TRELLO_ACCESS_TOKEN` <br />
<span class="caption">Value:</span> Your Trello Access Token. <br />
_You can get your API key at: trello.com/app-key._
## Keyboard Commands ## Keyboard Commands
None. None.
@ -33,6 +24,8 @@ None.
```yaml ```yaml
trello: trello:
accessToken: "7b8b14f8743a408a93276d7155dd9ee2"
apiKey: "3276d7155dd9ee27b8b14f8743a408a9"
board: Main board: Main
enabled: true enabled: true
list: "Todo" list: "Todo"
@ -52,6 +45,8 @@ configuration (note the difference in `list`):
```yaml ```yaml
trello: trello:
accessToken: "7b8b14f8743a408a93276d7155dd9ee2"
apiKey: "3276d7155dd9ee27b8b14f8743a408a9"
board: Main board: Main
enabled: true enabled: true
list: ["Todo", "Done"] list: ["Todo", "Done"]
@ -66,6 +61,12 @@ trello:
### Attributes ### Attributes
`accessToken` <br />
Value: Your Trello access token.
`apiKey` <br />
Value: Your Trello API key.
`board` <br /> `board` <br />
The name of the Trello board. <br /> The name of the Trello board. <br />

View File

@ -14,14 +14,6 @@ Displays tickets in the "New" status - i.e. have not yet been assigned.
wtf/zendesk/ wtf/zendesk/
``` ```
## Required ENV Variables
<span class="caption">Key:</span> `ZENDESK_API` <br />
<span class="caption">Value:</span> Your Zendesk API Token
<span class="caption">Key:</span> `ZENDESK_DOMAIN` <br />
<span class="caption">Value:</span> Your Zendesk subdomain
## Keyboard Commands ## Keyboard Commands
<span class="caption">Key:</span> `[return]` <br /> <span class="caption">Key:</span> `[return]` <br />
@ -43,6 +35,7 @@ wtf/zendesk/
```yaml ```yaml
zendesk: zendesk:
apiKey: "3276d7155dd9ee27b8b14f8743a408a9"
enabled: true enabled: true
position: position:
top: 0 top: 0
@ -50,11 +43,15 @@ zendesk:
height: 1 height: 1
width: 1 width: 1
status: "new" status: "new"
subdomain: "your_domain"
username: "your_email@acme.com" username: "your_email@acme.com"
``` ```
### Attributes ### Attributes
`apiKey` <br />
Value: Your Zendesk API token.
`enabled` <br /> `enabled` <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br /> Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: `true`, `false`. Values: `true`, `false`.
@ -66,6 +63,9 @@ Defines where in the grid this module's widget will be displayed. <br />
The status of tickets you want to retrieve. The status of tickets you want to retrieve.
Values: `new`, `open`, `pending`, `hold`. Values: `new`, `open`, `pending`, `hold`.
`subdomain` <br />
Value: Your Zendesk subdomain.
`username` <br /> `username` <br />
Your Zendesk username Your Zendesk username
Values: A valid Zendesk username (usually an email address). Values: A valid Zendesk username (usually an email address).

View File

@ -23,7 +23,10 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
client := trello.NewClient(os.Getenv("WTF_TRELLO_APP_KEY"), os.Getenv("WTF_TRELLO_ACCESS_TOKEN")) client := trello.NewClient(
widget.apiKey(),
widget.accessToken(),
)
// Get the cards // Get the cards
searchResult, err := GetCards(client, getLists()) searchResult, err := GetCards(client, getLists())
@ -50,6 +53,21 @@ func (widget *Widget) Refresh() {
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) accessToken() string {
return wtf.Config.UString(
"wtf.mods.trello.accessToken",
os.Getenv("WTF_TRELLO_ACCESS_TOKEN"),
)
}
func (widget *Widget) apiKey() string {
return wtf.Config.UString(
"wtf.mods.trello.apiKey",
os.Getenv("WTF_TRELLO_APP_KEY"),
)
}
func (widget *Widget) contentFrom(searchResult *SearchResult) string { func (widget *Widget) contentFrom(searchResult *SearchResult) string {
str := "" str := ""

View File

@ -16,9 +16,19 @@ type Resource struct {
Raw string Raw string
} }
var a = os.Getenv("ZENDESK_API") func apiKey() string {
var subdomain = os.Getenv("ZENDESK_SUBDOMAIN") return wtf.Config.UString(
var baseURL = fmt.Sprintf("https://%v.zendesk.com/api/v2", subdomain) "wtf.mods.zendesk.apiKey",
os.Getenv("ZENDESK_API"),
)
}
func subdomain() string {
return wtf.Config.UString(
"wtf.mods.zendesk.apiKey",
os.Getenv("ZENDESK_API"),
)
}
func errHandler(err error) { func errHandler(err error) {
if err != nil { if err != nil {
@ -26,20 +36,15 @@ func errHandler(err error) {
} }
} }
func buildUrl(baseURL string) string {
ticketURL := baseURL + "/tickets.json?sort_by=status"
return ticketURL
}
func api(key string, meth string, path string, params string) (*Resource, error) { func api(key string, meth string, path string, params string) (*Resource, error) {
trn := &http.Transport{} trn := &http.Transport{}
client := &http.Client{ client := &http.Client{
Transport: trn, Transport: trn,
} }
var URL = buildUrl(baseURL) baseURL := fmt.Sprintf("https://%v.zendesk.com/api/v2", subdomain())
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))
if err != nil { if err != nil {

View File

@ -55,7 +55,7 @@ func listTickets(pag ...string) (*TicketArray, error) {
} else { } else {
path = pag[0] path = pag[0]
} }
resource, err := api(a, "GET", path, "") resource, err := api(apiKey(), "GET", path, "")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -103,7 +103,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", subdomain(), issue.Id)
wtf.OpenFile(ticketUrl) wtf.OpenFile(ticketUrl)
} }
} }