1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-12-21 00:51:15 +00:00

add caching to beerpay API

This commit is contained in:
Knut Ahlers 2016-07-06 00:45:11 +02:00
parent dc36e17aef
commit 4a7e67412c
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"time"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp" "golang.org/x/net/context/ctxhttp"
@ -30,25 +31,32 @@ func (s beerpayServiceHandler) Handle(ctx context.Context, params []string) (tit
return 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" title = "beerpay"
text = fmt.Sprintf("$%d", r.TotalAmount)
color = "red" color = "red"
cacheKey := fmt.Sprintf("%s::%s", params[0], params[1])
text, err = cacheStore.Get("beerpay", cacheKey)
if err != nil {
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
}
text = fmt.Sprintf("$%d", r.TotalAmount)
cacheStore.Set("beerpay", cacheKey, text, 5*time.Minute)
}
return return
} }