From dc36e17aefac371e00dd2a4433237f03807c9b6f Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 6 Jul 2016 00:30:12 +0200 Subject: [PATCH] add service "beerpay" --- service_beerpay.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 service_beerpay.go diff --git a/service_beerpay.go b/service_beerpay.go new file mode 100644 index 0000000..8feeabd --- /dev/null +++ b/service_beerpay.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" + + "golang.org/x/net/context" + "golang.org/x/net/context/ctxhttp" +) + +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{"", ""}, + }} +} + +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 + } + + var resp *http.Response + + apiURL := fmt.Sprintf("https://beerpay.io/api/v1/%s/projects/%s", params[0], params[1]) + resp, err = ctxhttp.Get(ctx, http.DefaultClient, apiURL) + 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 + } + + title = "beerpay" + text = fmt.Sprintf("$%d", r.TotalAmount) + color = "red" + return +}