Skip to content

Commit

Permalink
Use keep-alive connections & describe external functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Miranda committed Apr 10, 2018
1 parent b4ea6e4 commit a66eb27
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

// Podium is a struct that represents a podium API application
type Podium struct {
httpClient *http.Client
Config *viper.Viper
URL string
baseLeaderboard string
Expand All @@ -22,6 +23,7 @@ type Podium struct {
// NewPodium returns a new podium API application
func NewPodium(config *viper.Viper) *Podium {
app := &Podium{
httpClient: &http.Client{},
Config: config,
URL: config.GetString("podium.url"),
baseLeaderboard: config.GetString("leaderboards.globalLeaderboard"),
Expand All @@ -30,7 +32,7 @@ func NewPodium(config *viper.Viper) *Podium {
return app
}

func sendTo(method, url string, payload map[string]interface{}) (int, string, error) {
func (app *Podium) sendTo(method, url string, payload map[string]interface{}) (int, string, error) {
payloadJSON, err := json.Marshal(payload)
if err != nil {
return -1, "", err
Expand All @@ -52,8 +54,7 @@ func sendTo(method, url string, payload map[string]interface{}) (int, string, er

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

client := &http.Client{}
resp, err := client.Do(req)
resp, err := app.httpClient.Do(req)
defer resp.Body.Close()
if err != nil {
return -1, "", err
Expand Down Expand Up @@ -113,77 +114,87 @@ func (app *Podium) buildHealthcheckURL() string {
return app.buildURL(pathname)
}

// external functions:
// GetBaseLeaderboards shows the global leaderboard
func (app *Podium) GetBaseLeaderboards() string {
return app.baseLeaderboard
}

// GetLocalizedLeaderboard receives a locale and returns its leaderboard
func (app *Podium) GetLocalizedLeaderboard(locale string) string {
localeLeaderboard := app.localeLeaderboard
result := strings.Replace(localeLeaderboard, "%{locale}", locale, -1)
return result
}

// GetTop returns the top players for this leaderboard. Page is 1-index
func (app *Podium) GetTop(leaderboard string, page int, pageSize int) (int, string, error) {
route := app.buildGetTopURL(leaderboard, page, pageSize)
status, body, err := sendTo("GET", route, nil)
status, body, err := app.sendTo("GET", route, nil)
return status, body, err
}

// GetTopPercent returns the top x% of players in a leaderboard
func (app *Podium) GetTopPercent(leaderboard string, percentage int) (int, string, error) {
route := app.buildGetTopPercentURL(leaderboard, percentage)
status, body, err := sendTo("GET", route, nil)
status, body, err := app.sendTo("GET", route, nil)
return status, body, err
}

// UpdateScore updates the score of a particular player in a leaderboard
func (app *Podium) UpdateScore(leaderboard string, playerID string, score int) (int, string, error) {
route := app.buildUpdateScoreURL(leaderboard, playerID)
payload := map[string]interface{}{
"score": score,
}
status, body, err := sendTo("PUT", route, payload)
status, body, err := app.sendTo("PUT", route, payload)
return status, body, err
}

// IncrementScore increments the score of a particular player in a leaderboard
func (app *Podium) IncrementScore(leaderboard string, playerID string, increment int) (int, string, error) {
route := app.buildIncrementScoreURL(leaderboard, playerID)
payload := map[string]interface{}{
"increment": increment,
}
status, body, err := sendTo("PATCH", route, payload)
status, body, err := app.sendTo("PATCH", route, payload)
return status, body, err
}

// UpdateScores updates the score of a player in more than one leaderboard
func (app *Podium) UpdateScores(leaderboards []string, playerID string, score int) (int, string, error) {
route := app.buildUpdateScoresURL(playerID)
payload := map[string]interface{}{
"score": score,
"leaderboards": leaderboards,
}
status, body, err := sendTo("PUT", route, payload)
status, body, err := app.sendTo("PUT", route, payload)
return status, body, err
}

// RemoveMemberFromLeaderboard removes a player from a leaderboard
func (app *Podium) RemoveMemberFromLeaderboard(leaderboard string, member string) (int, string, error) {
route := app.buildRemoveMemberFromLeaderboardURL(leaderboard, member)
status, body, err := sendTo("DELETE", route, nil)
status, body, err := app.sendTo("DELETE", route, nil)
return status, body, err
}

// GetPlayer shows score and rank of a particular player in a leaderboard
func (app *Podium) GetPlayer(leaderboard string, playerID string) (int, string, error) {
route := app.buildGetPlayerURL(leaderboard, playerID)
status, body, err := sendTo("GET", route, nil)
status, body, err := app.sendTo("GET", route, nil)
return status, body, err
}

// Healthcheck verifies if podium is still up
func (app *Podium) Healthcheck(leaderboard string, playerID string) (int, string, error) {
route := app.buildHealthcheckURL()
status, body, err := sendTo("GET", route, nil)
status, body, err := app.sendTo("GET", route, nil)
return status, body, err
}

// DeleteLeaderboard deletes the leaderboard from podium
func (app *Podium) DeleteLeaderboard(leaderboard string) (int, string, error) {
route := app.buildDeleteLeaderboardURL(leaderboard)
status, body, err := sendTo("DELETE", route, nil)
status, body, err := app.sendTo("DELETE", route, nil)
return status, body, err
}

0 comments on commit a66eb27

Please sign in to comment.