2019-11-05 12:58:09 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rewards
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
2019-11-26 11:12:37 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/useragent"
|
2019-11-05 12:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-06 18:37:53 +00:00
|
|
|
// ErrPartners is the default error class for partners package.
|
|
|
|
ErrPartners = errs.Class("partners")
|
2019-11-05 12:58:09 +00:00
|
|
|
|
2019-11-06 18:37:53 +00:00
|
|
|
// ErrPartnerNotExist is returned when a particular partner does not exist.
|
|
|
|
ErrPartnerNotExist = errs.Class("partner does not exist")
|
2019-11-05 12:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PartnersDB allows access to partners database.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type PartnersDB interface {
|
|
|
|
// All returns all partners.
|
|
|
|
All(ctx context.Context) ([]PartnerInfo, error)
|
|
|
|
// ByName returns partner definitions for a given name.
|
|
|
|
ByName(ctx context.Context, name string) (PartnerInfo, error)
|
|
|
|
// ByID returns partner definition corresponding to an id.
|
|
|
|
ByID(ctx context.Context, id string) (PartnerInfo, error)
|
|
|
|
// ByUserAgent returns partner definition corresponding to an user agent string.
|
|
|
|
ByUserAgent(ctx context.Context, agent string) (PartnerInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartnersService allows manipulating and accessing partner information.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type PartnersService struct {
|
2021-04-09 23:25:25 +01:00
|
|
|
log *zap.Logger
|
|
|
|
db PartnersDB
|
2019-11-05 12:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPartnersService returns a service for handling partner information.
|
2021-04-09 23:25:25 +01:00
|
|
|
func NewPartnersService(log *zap.Logger, db PartnersDB) *PartnersService {
|
2019-11-05 12:58:09 +00:00
|
|
|
return &PartnersService{
|
2021-04-09 23:25:25 +01:00
|
|
|
log: log,
|
|
|
|
db: db,
|
2019-11-05 12:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// ByName looks up partner by name.
|
|
|
|
func (service *PartnersService) ByName(ctx context.Context, name string) (PartnerInfo, error) {
|
2019-11-05 12:58:09 +00:00
|
|
|
return service.db.ByName(ctx, name)
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// ByUserAgent looks up partner by user agent.
|
|
|
|
func (service *PartnersService) ByUserAgent(ctx context.Context, userAgentString string) (PartnerInfo, error) {
|
|
|
|
info, err := useragent.Parse(userAgentString)
|
|
|
|
if err != nil {
|
|
|
|
return PartnerInfo{}, ErrPartners.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:23:17 +01:00
|
|
|
return service.db.ByUserAgent(ctx, info.Product.Name)
|
2019-11-26 11:12:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
// All returns all partners.
|
|
|
|
func (service *PartnersService) All(ctx context.Context) ([]PartnerInfo, error) {
|
|
|
|
return service.db.All(ctx)
|
|
|
|
}
|