2016-07-05 22:30:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-07-05 22:45:11 +00:00
|
|
|
"time"
|
2016-07-05 22:30:12 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerServiceHandler("beerpay", beerpayServiceHandler{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type beerpayServiceHandler struct{}
|
|
|
|
|
|
|
|
func (s beerpayServiceHandler) GetDocumentation() serviceHandlerDocumentationList {
|
|
|
|
return serviceHandlerDocumentationList{{
|
|
|
|
ServiceName: "beerpay Total Amount",
|
|
|
|
DemoPath: "/beerpay/beerpay/beerpay.io",
|
|
|
|
Arguments: []string{"<user>", "<project>"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s beerpayServiceHandler) Handle(ctx context.Context, params []string) (title, text, color string, err error) {
|
|
|
|
if len(params) < 2 {
|
|
|
|
err = errors.New("You need to provide user and project")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-05 22:45:11 +00:00
|
|
|
title = "beerpay"
|
|
|
|
color = "red"
|
2016-07-05 22:30:12 +00:00
|
|
|
|
2016-07-05 22:45:11 +00:00
|
|
|
cacheKey := fmt.Sprintf("%s::%s", params[0], params[1])
|
|
|
|
text, err = cacheStore.Get("beerpay", cacheKey)
|
2016-07-05 22:30:12 +00:00
|
|
|
|
2016-07-05 22:45:11 +00:00
|
|
|
if err != nil {
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
apiURL := fmt.Sprintf("https://beerpay.io/api/v1/%s/projects/%s", params[0], params[1])
|
2018-06-01 20:42:37 +00:00
|
|
|
req, _ := http.NewRequest("GET", apiURL, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req.WithContext(ctx))
|
2016-07-05 22:45:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
r := struct {
|
|
|
|
TotalAmount int `json:"total_amount"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
text = fmt.Sprintf("$%d", r.TotalAmount)
|
|
|
|
cacheStore.Set("beerpay", cacheKey, text, 5*time.Minute)
|
2016-07-05 22:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|