/
analytics_impl.go
105 lines (82 loc) · 2.86 KB
/
analytics_impl.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package internal
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/confluence"
"net/http"
"net/url"
"strings"
)
func NewAnalyticsService(client service.Connector) *AnalyticsService {
return &AnalyticsService{
internalClient: &internalAnalyticsServiceImpl{c: client},
}
}
type AnalyticsService struct {
internalClient confluence.AnalyticsConnector
}
// Get gets the total number of views a piece of content has.
//
// GET /wiki/rest/api/analytics/content/{contentId}/views
//
// https://docs.go-atlassian.io/confluence-cloud/analytics#get-views
func (a *AnalyticsService) Get(ctx context.Context, contentId, fromDate string) (*model.ContentViewScheme, *model.ResponseScheme, error) {
return a.internalClient.Get(ctx, contentId, fromDate)
}
// Distinct get the total number of distinct viewers a piece of content has.
//
// GET /wiki/rest/api/analytics/content/{contentId}/viewers
//
// https://docs.go-atlassian.io/confluence-cloud/analytics#get-viewers
func (a *AnalyticsService) Distinct(ctx context.Context, contentId, fromDate string) (*model.ContentViewScheme, *model.ResponseScheme, error) {
return a.internalClient.Distinct(ctx, contentId, fromDate)
}
type internalAnalyticsServiceImpl struct {
c service.Connector
}
func (i *internalAnalyticsServiceImpl) Get(ctx context.Context, contentId, fromDate string) (*model.ContentViewScheme, *model.ResponseScheme, error) {
if contentId == "" {
return nil, nil, model.ErrNoContentIDError
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/analytics/content/%v/views", contentId))
if fromDate != "" {
query := url.Values{}
query.Add("fromDate", fromDate)
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil)
if err != nil {
return nil, nil, err
}
views := new(model.ContentViewScheme)
response, err := i.c.Call(request, views)
if err != nil {
return nil, response, err
}
return views, response, nil
}
func (i *internalAnalyticsServiceImpl) Distinct(ctx context.Context, contentId, fromDate string) (*model.ContentViewScheme, *model.ResponseScheme, error) {
if contentId == "" {
return nil, nil, model.ErrNoContentIDError
}
var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("wiki/rest/api/analytics/content/%v/viewers", contentId))
if fromDate != "" {
query := url.Values{}
query.Add("fromDate", fromDate)
endpoint.WriteString(fmt.Sprintf("?%v", query.Encode()))
}
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil)
if err != nil {
return nil, nil, err
}
views := new(model.ContentViewScheme)
response, err := i.c.Call(request, views)
if err != nil {
return nil, response, err
}
return views, response, nil
}