-
Notifications
You must be signed in to change notification settings - Fork 402
/
partners_service.go
71 lines (58 loc) · 1.99 KB
/
partners_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package rewards
import (
"context"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/useragent"
)
var (
// ErrPartners is the default error class for partners package.
ErrPartners = errs.Class("partners")
// ErrPartnerNotExist is returned when a particular partner does not exist.
ErrPartnerNotExist = errs.Class("partner does not exist")
)
// 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 {
log *zap.Logger
db PartnersDB
}
// NewPartnersService returns a service for handling partner information.
func NewPartnersService(log *zap.Logger, db PartnersDB) *PartnersService {
return &PartnersService{
log: log,
db: db,
}
}
// ByName looks up partner by name.
func (service *PartnersService) ByName(ctx context.Context, name string) (PartnerInfo, error) {
return service.db.ByName(ctx, name)
}
// 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)
}
return service.db.ByUserAgent(ctx, info.Product.Name)
}
// All returns all partners.
func (service *PartnersService) All(ctx context.Context) ([]PartnerInfo, error) {
return service.db.All(ctx)
}