Skip to content

Commit

Permalink
Duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Sep 25, 2023
1 parent ab7f2ca commit 8c208ff
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions leaderboard/enriching/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (e *enricherImpl) Enrich(
return members, nil
}

l := e.logger.With(
zap.String("method", "Enrich"),
zap.String("tenantID", tenantID),
zap.String("leaderboardID", leaderboardID),
)

tenantUrl, webHookExists := e.config.webhookUrls[tenantID]
cloudSaveDisabled := e.config.cloudSave.disabled[tenantID]

Expand All @@ -67,7 +73,8 @@ func (e *enricherImpl) Enrich(
members, err := e.enrichWithWebhook(ctx, tenantID, leaderboardID, members)

if err != nil {
return nil, err
l.Error("could not enrich with webhook", zap.Error(err))
return nil, fmt.Errorf("could not enrich with webhook: %w", err)
}

return members, nil
Expand All @@ -76,32 +83,37 @@ func (e *enricherImpl) Enrich(
members, err := e.enrichWithCloudSave(ctx, tenantID, members)

if err != nil {
return nil, err
l.Error("could not enrich with cloud save", zap.Error(err))
return nil, fmt.Errorf("could not enrich with cloud save: %w", err)
}

return members, nil
}

l.Debug(fmt.Sprintf("no webhook configured for tentantID '%s' and cloud save disabled. Skipping enrichment.", tenantID))

return members, nil
}

func (e *enricherImpl) enrichWithWebhook(
ctx context.Context,
tenantID,
url,
leaderboardID string,
members []*model.Member,
) ([]*model.Member, error) {
e.logger.Debug(fmt.Sprintf("calling webhook for tenantID '%s'.", tenantID))

tenantUrl := e.config.webhookUrls[tenantID]
l := e.logger.With(
zap.String("url", url),
zap.String("leaderboardID", leaderboardID),
zap.String("method", "enrichWithWebhook"),
)

body := membersModelToProto(leaderboardID, members)
jsonData, err := json.Marshal(podium_leaderboard_webhooks_v1.EnrichLeaderboardsRequest{Members: body})
if err != nil {
return nil, fmt.Errorf("could not marshal request: %w", errors.Join(err, ErrEnrichmentInternal))
}

webhookUrl, err := buildUrl(tenantUrl, enrichWebhookEndpoint)
webhookUrl, err := buildUrl(url, enrichWebhookEndpoint)
if err != nil {
return nil, fmt.Errorf("could not build webhook URL: %w", errors.Join(err, ErrEnrichmentInternal))
}
Expand All @@ -114,7 +126,7 @@ func (e *enricherImpl) enrichWithWebhook(

req.Header.Set("Content-Type", "application/json")

e.logger.Debug(fmt.Sprintf("calling enrichment webhook '%s' for tenantID '%s'", webhookUrl, tenantID))
l.Debug(fmt.Sprintf("calling enrichment webhook '%s'", webhookUrl))
resp, err := e.client.Do(req)
if err != nil {
return nil, fmt.Errorf("could not complete request to webhook: %w", errors.Join(err, ErrEnrichmentCall))
Expand Down Expand Up @@ -145,6 +157,11 @@ func (e *enricherImpl) enrichWithWebhook(
}

func (e *enricherImpl) enrichWithCloudSave(ctx context.Context, tenantID string, members []*model.Member) ([]*model.Member, error) {
l := e.logger.With(
zap.String("method", "enrichWithCloudSave"),
zap.String("tenantID", tenantID),
)

if e.config.cloudSave.disabled[tenantID] {
e.logger.Debug(fmt.Sprintf("cloud save enrich disabled for tenant %s. Skipping enrichment.", tenantID))
return members, nil
Expand All @@ -155,7 +172,7 @@ func (e *enricherImpl) enrichWithCloudSave(ctx context.Context, tenantID string,
return members, nil
}

e.logger.Debug(fmt.Sprintf("calling cloud save for tenantID '%s'", tenantID))
l.Debug(fmt.Sprintf("calling cloud save for tenantID '%s'", tenantID))

ids := make([]string, len(members))
for i, m := range members {
Expand Down

0 comments on commit 8c208ff

Please sign in to comment.