ovrstat-api/ovrstat.go

42 lines
726 B
Go

package ovrstat
import (
"strings"
"encoding/json"
"net/http"
"fmt"
"net/url"
)
type OvrStatApi struct {
Client *http.Client
URL *url.URL
}
func New(apiUrl string) *OvrStatApi {
u, err := url.Parse(apiUrl)
if err != nil {
return nil
}
return &OvrStatApi{Client: &http.Client{}, URL: u}
}
func (s *OvrStatApi) Fetch(platform, region, battletag string) (*OverstatApiResponse, error) {
res, err := s.Client.Get(fmt.Sprintf("%s/v1/stats/%s/%s/%s", s.URL, platform, region, strings.Replace(battletag, "#", "-", -1)))
if err != nil {
return nil, err
}
defer res.Body.Close()
var r OverstatApiResponse
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return nil, err
}
return &r, nil
}