Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication in analytics.go #393

Closed
theckman opened this issue Nov 16, 2021 · 1 comment
Closed

Reduce code duplication in analytics.go #393

theckman opened this issue Nov 16, 2021 · 1 comment

Comments

@theckman
Copy link
Collaborator

The three methods in analytics.go are like 99% the same, minus the last part of the API URL. This code is ripe for some DRYing by creating an unexported method, that accepts the final piece of as an argument and appends it to the URL.

go-pagerduty/analytics.go

Lines 61 to 115 in 2f646c3

// GetAggregatedIncidentData gets the aggregated incident analytics for the requested data.
func (c *Client) GetAggregatedIncidentData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}
resp, err := c.post(ctx, "/analytics/metrics/incidents/all", analytics, h)
if err != nil {
return AnalyticsResponse{}, err
}
var analyticsResponse AnalyticsResponse
if err = c.decodeJSON(resp, &analyticsResponse); err != nil {
return AnalyticsResponse{}, err
}
return analyticsResponse, nil
}
// GetAggregatedServiceData gets the aggregated service analytics for the requested data.
func (c *Client) GetAggregatedServiceData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}
resp, err := c.post(ctx, "/analytics/metrics/incidents/services", analytics, h)
if err != nil {
return AnalyticsResponse{}, err
}
var analyticsResponse AnalyticsResponse
if err = c.decodeJSON(resp, &analyticsResponse); err != nil {
return AnalyticsResponse{}, err
}
return analyticsResponse, nil
}
// GetAggregatedTeamData gets the aggregated team analytics for the requested data.
func (c *Client) GetAggregatedTeamData(ctx context.Context, analytics AnalyticsRequest) (AnalyticsResponse, error) {
h := map[string]string{
"X-EARLY-ACCESS": "analytics-v2",
}
resp, err := c.post(ctx, "/analytics/metrics/incidents/teams", analytics, h)
if err != nil {
return AnalyticsResponse{}, err
}
var analyticsResponse AnalyticsResponse
if err = c.decodeJSON(resp, &analyticsResponse); err != nil {
return AnalyticsResponse{}, err
}
return analyticsResponse, nil
}

@theckman theckman added this to the v1.6.0 milestone Nov 16, 2021
t-junjie pushed a commit to t-junjie/go-pagerduty that referenced this issue Nov 16, 2021
@theckman theckman modified the milestones: v1.6.0, v1.5.0 Nov 16, 2021
@theckman
Copy link
Collaborator Author

Fixed via #397.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant