mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-11-08 13:20:02 +00:00
add service "beerpay"
This commit is contained in:
parent
e10c432a34
commit
dc36e17aef
1 changed files with 54 additions and 0 deletions
54
service_beerpay.go
Normal file
54
service_beerpay.go
Normal file
|
@ -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{"<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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Loading…
Reference in a new issue