69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package ovrstat
|
|
|
|
import (
|
|
"strings"
|
|
"encoding/json"
|
|
"net/http"
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
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{Timeout: 10 * time.Second}, URL: u}
|
|
}
|
|
|
|
func (s *OvrStatApi) FetchProfile(platform, region, battletag string) (*OwApiResponse, error) {
|
|
res, err := s.Client.Get(fmt.Sprintf("%s/v1/stats/%s/%s/%s/profile", s.URL, platform, region, strings.Replace(battletag, "#", "-", -1)))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
var r OwApiResponse
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &r, nil
|
|
}
|
|
|
|
func (s *OvrStatApi) FetchHeroStats(platform, region, battletag string, heroes []string) (*OwApiResponse, error) {
|
|
res, err := s.Client.Get(fmt.Sprintf("%s/v1/stats/%s/%s/%s/heroes/%s", s.URL, platform, region, strings.Replace(battletag, "#", "-", -1), strings.Join(heroes, ",")))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
var r OwApiResponse
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &r, nil
|
|
}
|
|
|
|
func ValidPlatform(platform string) bool {
|
|
return platform == "pc" || platform == "xbl" || platform == "psn"
|
|
}
|
|
|
|
func ValidRegion(region string) bool {
|
|
return region == "us" || region == "eu" || region == "kr"
|
|
} |