Skip to content

Commit

Permalink
Prefer path.Join for creating URLs
Browse files Browse the repository at this point in the history
Prefer `path.Join` to `fmt.Sprintf`/`+=` for creating consistent URLs
  • Loading branch information
Lennart Goedhart committed Aug 21, 2019
1 parent 0fb64a8 commit c7e3d51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
8 changes: 4 additions & 4 deletions clients/gatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package clients
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"path"

"github.com/tidepool-org/go-common/clients/disc"
"github.com/tidepool-org/go-common/clients/status"
Expand Down Expand Up @@ -97,7 +97,7 @@ func (client *gatekeeperClient) UserInGroup(userID, groupID string) (Permissions
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
host.Path += fmt.Sprintf("access/%s/%s", groupID, userID)
host.Path = path.Join(host.Path, "access", groupID, userID)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", client.tokenProvider.TokenProvide())
Expand Down Expand Up @@ -127,7 +127,7 @@ func (client *gatekeeperClient) UsersInGroup(groupID string) (UsersPermissions,
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
host.Path += fmt.Sprintf("access/%s", groupID)
host.Path = path.Join(host.Path, "access", groupID)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", client.tokenProvider.TokenProvide())
Expand Down Expand Up @@ -157,7 +157,7 @@ func (client *gatekeeperClient) SetPermissions(userID, groupID string, permissio
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
host.Path += fmt.Sprintf("access/%s/%s", groupID, userID)
host.Path = path.Join(host.Path, "access", groupID, userID)

if jsonPerms, err := json.Marshal(permissions); err != nil {
log.Println(err)
Expand Down
9 changes: 5 additions & 4 deletions clients/highwater/highwater.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"log"
"net/http"
"net/url"
"path"
"strings"

"github.com/tidepool-org/go-common/clients/disc"
)

//Generic client interface that we will implement and mock
// Client interface that we will implement and mock
type Client interface {
PostServer(eventName, token string, params map[string]string)
PostThisUser(eventName, token string, params map[string]string)
Expand Down Expand Up @@ -134,7 +135,7 @@ func (client *HighwaterClient) PostServer(eventName, token string, params map[st
return
}

host.Path += "/server/" + client.config.Name + "/" + client.adjustEventName(eventName)
host.Path = path.Join(host.Path, "server", client.config.Name, client.adjustEventName(eventName))

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)
Expand All @@ -157,7 +158,7 @@ func (client *HighwaterClient) PostThisUser(eventName, token string, params map[
return
}

host.Path += "/thisuser/" + client.adjustEventName(eventName)
host.Path = path.Join(host.Path, "thisuser", client.adjustEventName(eventName))

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)
Expand All @@ -180,7 +181,7 @@ func (client *HighwaterClient) PostWithUser(userId, eventName, token string, par
return
}

host.Path += "/user/" + userId + "/" + client.adjustEventName(eventName)
host.Path = path.Join(host.Path, "user", userId, client.adjustEventName(eventName))

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)
Expand Down
6 changes: 3 additions & 3 deletions clients/seagull.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package clients

import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"path"

"github.com/tidepool-org/go-common/clients/disc"
"github.com/tidepool-org/go-common/clients/status"
Expand Down Expand Up @@ -76,7 +76,7 @@ func (client *seagullClient) GetPrivatePair(userID, hashName, token string) *Pri
if host == nil {
return nil
}
host.Path += fmt.Sprintf("%s/private/%s", userID, hashName)
host.Path = path.Join(host.Path, userID, "private", hashName)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", token)
Expand Down Expand Up @@ -107,7 +107,7 @@ func (client *seagullClient) GetCollection(userID, collectionName, token string,
if host == nil {
return nil
}
host.Path += fmt.Sprintf("%s/%s", userID, collectionName)
host.Path = path.Join(host.Path, userID, collectionName)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", token)
Expand Down
21 changes: 11 additions & 10 deletions clients/shoreline/shoreline.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This is a client module to support server-side use of the Tidepool
// Package shoreline is a client module to support server-side use of the Tidepool
// service called user-api.
package shoreline

Expand All @@ -10,6 +10,7 @@ import (
"log"
"net/http"
"net/url"
"path"
"sync"
"time"

Expand All @@ -19,7 +20,7 @@ import (
"github.com/tidepool-org/go-common/jepson"
)

//Generic client interface that we will implement and mock
// Client interface that we will implement and mock
type Client interface {
Start() error
Close()
Expand All @@ -31,7 +32,7 @@ type Client interface {
UpdateUser(userID string, userUpdate UserUpdate, token string) error
}

// UserApiClient manages the local data for a client. A client is intended to be shared among multiple
// ShorelineClient manages the local data for a client. A client is intended to be shared among multiple
// goroutines so it's OK to treat it as a singleton (and probably a good idea).
type ShorelineClient struct {
httpClient *http.Client // store a reference to the http client so we can reuse it
Expand Down Expand Up @@ -203,10 +204,10 @@ func (client *ShorelineClient) Close() {
func (client *ShorelineClient) serverLogin() error {
host := client.getHost()
if host == nil {
return errors.New("No known user-api hosts.")
return errors.New("No known user-api hosts")
}

host.Path += "/serverlogin"
host.Path = path.Join(host.Path, "serverlogin")

req, _ := http.NewRequest("POST", host.String(), nil)
req.Header.Add("x-tidepool-server-name", client.config.Name)
Expand Down Expand Up @@ -247,7 +248,7 @@ func (client *ShorelineClient) Signup(username, password, email string) (*UserDa
return nil, errors.New("No known user-api hosts.")
}

host.Path += "/user"
host.Path = path.Join(host.Path, "user")
data := []byte(fmt.Sprintf(`{"username": "%s", "password": "%s","emails":["%s"]}`, username, password, email))

req, _ := http.NewRequest("POST", host.String(), bytes.NewBuffer(data))
Expand Down Expand Up @@ -279,7 +280,7 @@ func (client *ShorelineClient) Login(username, password string) (*UserData, stri
return nil, "", errors.New("No known user-api hosts.")
}

host.Path += "/login"
host.Path = path.Join(host.Path, "login")

req, _ := http.NewRequest("POST", host.String(), nil)
req.SetBasicAuth(username, password)
Expand Down Expand Up @@ -314,7 +315,7 @@ func (client *ShorelineClient) CheckToken(token string) *TokenData {
return nil
}

host.Path += "/token/" + token
host.Path = path.Join(host.Path, "token", token)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", client.serverToken)
Expand Down Expand Up @@ -357,7 +358,7 @@ func (client *ShorelineClient) GetUser(userID, token string) (*UserData, error)
return nil, errors.New("No known user-api hosts.")
}

host.Path += fmt.Sprintf("user/%s", userID)
host.Path = path.Join(host.Path, "user", userID)

req, _ := http.NewRequest("GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", token)
Expand Down Expand Up @@ -396,7 +397,7 @@ func (client *ShorelineClient) UpdateUser(userID string, userUpdate UserUpdate,
Updates UserUpdate `json:"updates"`
}

host.Path += "/user/" + userID
host.Path = path.Join(host.Path, "user", userID)

if jsonUser, err := json.Marshal(updatesToApply{Updates: userUpdate}); err != nil {
return &status.StatusError{
Expand Down

0 comments on commit c7e3d51

Please sign in to comment.