2019-11-05 12:58:09 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rewards
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PartnerList defines a json struct for defining partners.
|
|
|
|
type PartnerList struct {
|
|
|
|
Partners []PartnerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartnerInfo contains information about a partner.
|
|
|
|
type PartnerInfo struct {
|
|
|
|
Name string
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2019-11-06 18:37:53 +00:00
|
|
|
// UserAgent returns canonical user agent.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (p *PartnerInfo) UserAgent() string { return p.Name }
|
|
|
|
|
|
|
|
// CanonicalUserAgentProduct returns canonicalizes the user agent product, which is suitable for lookups.
|
|
|
|
func CanonicalUserAgentProduct(product string) string { return strings.ToLower(product) }
|
|
|
|
|
|
|
|
// PartnersListFromJSONFile loads a json definition of partners.
|
|
|
|
func PartnersListFromJSONFile(path string) (*PartnerList, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2019-11-06 18:37:53 +00:00
|
|
|
return nil, ErrPartners.Wrap(err)
|
2019-11-05 12:58:09 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
2019-11-06 18:37:53 +00:00
|
|
|
err = errs.Combine(err, ErrPartners.Wrap(file.Close()))
|
2019-11-05 12:58:09 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var list PartnerList
|
|
|
|
err = json.NewDecoder(file).Decode(&list)
|
2019-11-06 18:37:53 +00:00
|
|
|
return &list, ErrPartners.Wrap(err)
|
2019-11-05 12:58:09 +00:00
|
|
|
}
|