refactor: remove unnecessary getters

This commit is contained in:
James Houlahan
2020-04-08 13:15:42 +02:00
parent d787d8b223
commit bafd4e714e
4 changed files with 12 additions and 22 deletions

View File

@ -132,8 +132,8 @@ func writeMultipartReport(w *multipart.Writer, rep *ReportReq) error { // nolint
// Report sends request as json or multipart (if has attachment). // Report sends request as json or multipart (if has attachment).
func (c *client) Report(rep ReportReq) (err error) { func (c *client) Report(rep ReportReq) (err error) {
rep.Client = c.cm.GetConfig().ClientID rep.Client = c.cm.config.ClientID
rep.ClientVersion = c.cm.GetConfig().AppVersion rep.ClientVersion = c.cm.config.AppVersion
rep.ClientType = EmailClientType rep.ClientType = EmailClientType
var req *http.Request var req *http.Request
@ -196,8 +196,8 @@ func (c *client) ReportBugWithEmailClient(os, osVersion, title, description, use
// ReportCrash is old. Use sentry instead. // ReportCrash is old. Use sentry instead.
func (c *client) ReportCrash(stacktrace string) (err error) { func (c *client) ReportCrash(stacktrace string) (err error) {
crashReq := ReportReq{ crashReq := ReportReq{
Client: c.cm.GetConfig().ClientID, Client: c.cm.config.ClientID,
ClientVersion: c.cm.GetConfig().AppVersion, ClientVersion: c.cm.config.AppVersion,
ClientType: EmailClientType, ClientType: EmailClientType,
OS: runtime.GOOS, OS: runtime.GOOS,
Debug: stacktrace, Debug: stacktrace,

View File

@ -168,7 +168,7 @@ type client struct {
func newClient(cm *ClientManager, userID string) *client { func newClient(cm *ClientManager, userID string) *client {
return &client{ return &client{
cm: cm, cm: cm,
hc: getHTTPClient(cm.GetConfig(), cm.GetRoundTripper()), hc: getHTTPClient(cm.config, cm.roundTripper),
userID: userID, userID: userID,
requestLocker: &sync.Mutex{}, requestLocker: &sync.Mutex{},
keyLocker: &sync.Mutex{}, keyLocker: &sync.Mutex{},
@ -207,7 +207,7 @@ func (c *client) Do(req *http.Request, retryUnauthorized bool) (res *http.Respon
func (c *client) doBuffered(req *http.Request, bodyBuffer []byte, retryUnauthorized bool) (res *http.Response, err error) { // nolint[funlen] func (c *client) doBuffered(req *http.Request, bodyBuffer []byte, retryUnauthorized bool) (res *http.Response, err error) { // nolint[funlen]
isAuthReq := strings.Contains(req.URL.Path, "/auth") isAuthReq := strings.Contains(req.URL.Path, "/auth")
req.Header.Set("x-pm-appversion", c.cm.GetConfig().AppVersion) req.Header.Set("x-pm-appversion", c.cm.config.AppVersion)
req.Header.Set("x-pm-apiversion", strconv.Itoa(Version)) req.Header.Set("x-pm-apiversion", strconv.Itoa(Version))
if c.uid != "" { if c.uid != "" {
@ -310,7 +310,7 @@ func (c *client) doJSONBuffered(req *http.Request, reqBodyBuffer []byte, data in
req.Header.Set("Accept", "application/vnd.protonmail.v1+json") req.Header.Set("Accept", "application/vnd.protonmail.v1+json")
var cancelRequest context.CancelFunc var cancelRequest context.CancelFunc
if c.cm.GetConfig().MinSpeed > 0 { if c.cm.config.MinSpeed > 0 {
var ctx context.Context var ctx context.Context
ctx, cancelRequest = context.WithCancel(req.Context()) ctx, cancelRequest = context.WithCancel(req.Context())
defer func() { defer func() {
@ -326,7 +326,7 @@ func (c *client) doJSONBuffered(req *http.Request, reqBodyBuffer []byte, data in
defer res.Body.Close() //nolint[errcheck] defer res.Body.Close() //nolint[errcheck]
var resBody []byte var resBody []byte
if c.cm.GetConfig().MinSpeed == 0 { if c.cm.config.MinSpeed == 0 {
resBody, err = ioutil.ReadAll(res.Body) resBody, err = ioutil.ReadAll(res.Body)
} else { } else {
resBody, err = c.readAllMinSpeed(res.Body, cancelRequest) resBody, err = c.readAllMinSpeed(res.Body, cancelRequest)
@ -404,7 +404,7 @@ func (c *client) doJSONBuffered(req *http.Request, reqBodyBuffer []byte, data in
} }
func (c *client) readAllMinSpeed(data io.Reader, cancelRequest context.CancelFunc) ([]byte, error) { func (c *client) readAllMinSpeed(data io.Reader, cancelRequest context.CancelFunc) ([]byte, error) {
firstReadTimeout := c.cm.GetConfig().FirstReadTimeout firstReadTimeout := c.cm.config.FirstReadTimeout
if firstReadTimeout == 0 { if firstReadTimeout == 0 {
firstReadTimeout = 5 * time.Minute firstReadTimeout = 5 * time.Minute
} }
@ -413,7 +413,7 @@ func (c *client) readAllMinSpeed(data io.Reader, cancelRequest context.CancelFun
}) })
var buffer bytes.Buffer var buffer bytes.Buffer
for { for {
_, err := io.CopyN(&buffer, data, c.cm.GetConfig().MinSpeed) _, err := io.CopyN(&buffer, data, c.cm.config.MinSpeed)
timer.Stop() timer.Stop()
timer.Reset(1 * time.Second) timer.Reset(1 * time.Second)
if err == io.EOF { if err == io.EOF {

View File

@ -100,11 +100,6 @@ func (cm *ClientManager) SetRoundTripper(rt http.RoundTripper) {
cm.roundTripper = rt cm.roundTripper = rt
} }
// GetRoundTripper gets the roundtripper used by clients created by this client manager.
func (cm *ClientManager) GetRoundTripper() (rt http.RoundTripper) {
return cm.roundTripper
}
// GetClient returns a client for the given userID. // GetClient returns a client for the given userID.
// If the client does not exist already, it is created. // If the client does not exist already, it is created.
func (cm *ClientManager) GetClient(userID string) Client { func (cm *ClientManager) GetClient(userID string) Client {
@ -241,11 +236,6 @@ func (cm *ClientManager) switchToReachableServer() (proxy string, err error) {
return return
} }
// GetConfig returns the config used to configure clients.
func (cm *ClientManager) GetConfig() *ClientConfig {
return cm.config
}
// GetToken returns the token for the given userID. // GetToken returns the token for the given userID.
func (cm *ClientManager) GetToken(userID string) string { func (cm *ClientManager) GetToken(userID string) string {
cm.tokensLocker.Lock() cm.tokensLocker.Lock()

View File

@ -152,8 +152,8 @@ func (c *client) ReportSentryCrash(reportErr error) (err error) {
} }
tags := map[string]string{ tags := map[string]string{
"OS": runtime.GOOS, "OS": runtime.GOOS,
"Client": c.cm.GetConfig().ClientID, "Client": c.cm.config.ClientID,
"Version": c.cm.GetConfig().AppVersion, "Version": c.cm.config.AppVersion,
"UserAgent": CurrentUserAgent, "UserAgent": CurrentUserAgent,
"UserID": c.userID, "UserID": c.userID,
} }