refactor: rename doGet to HTTPRequest

This commit is contained in:
Jan De Dobbeleer 2022-01-07 19:41:58 +01:00 committed by Jan De Dobbeleer
parent 00a09ca894
commit 88206ff9c9
14 changed files with 22 additions and 22 deletions

View file

@ -113,7 +113,7 @@ type Environment interface {
getShellName() string getShellName() string
getWindowTitle(imageName, windowTitleRegex string) (string, error) getWindowTitle(imageName, windowTitleRegex string) (string, error)
getWindowsRegistryKeyValue(path string) (*windowsRegistryValue, error) getWindowsRegistryKeyValue(path string) (*windowsRegistryValue, error)
doGet(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) HTTPRequest(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error)
hasParentFilePath(path string) (fileInfo *fileInfo, err error) hasParentFilePath(path string) (fileInfo *fileInfo, err error)
isWsl() bool isWsl() bool
isWsl2() bool isWsl2() bool
@ -469,8 +469,8 @@ func (env *environment) getShellName() string {
return *env.args.Shell return *env.args.Shell
} }
func (env *environment) doGet(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) { func (env *environment) HTTPRequest(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) {
defer env.trace(time.Now(), "doGet", url) defer env.trace(time.Now(), "HTTPRequest", url)
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout)) ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
defer cncl() defer cncl()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
@ -482,13 +482,13 @@ func (env *environment) doGet(url string, timeout int, requestModifiers ...HTTPR
} }
response, err := client.Do(request) response, err := client.Do(request)
if err != nil { if err != nil {
env.log(Error, "doGet", err.Error()) env.log(Error, "HTTPRequest", err.Error())
return nil, err return nil, err
} }
defer response.Body.Close() defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) body, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
env.log(Error, "doGet", err.Error()) env.log(Error, "HTTPRequest", err.Error())
return nil, err return nil, err
} }
return body, nil return body, nil

View file

@ -271,7 +271,7 @@ func (bf *brewfather) getResult() (*Batch, error) {
addAuthHeader := func(request *http.Request) { addAuthHeader := func(request *http.Request) {
request.Header.Add("authorization", authHeader) request.Header.Add("authorization", authHeader)
} }
body, err := bf.env.doGet(batchURL, httpTimeout, addAuthHeader) body, err := bf.env.HTTPRequest(batchURL, httpTimeout, addAuthHeader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -283,7 +283,7 @@ func (bf *brewfather) getResult() (*Batch, error) {
} }
// readings // readings
body, err = bf.env.doGet(batchReadingsURL, httpTimeout, addAuthHeader) body, err = bf.env.HTTPRequest(batchReadingsURL, httpTimeout, addAuthHeader)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -148,8 +148,8 @@ func TestBrewfatherSegment(t *testing.T) {
cache.On("get", BFCacheKey).Return(nil, false) // cache testing later because cache is a little more complicated than just the single response. cache.On("get", BFCacheKey).Return(nil, false) // cache testing later because cache is a little more complicated than just the single response.
// cache.On("set", BFCacheKey, tc.JSONResponse, tc.CacheTimeout).Return() // cache.On("set", BFCacheKey, tc.JSONResponse, tc.CacheTimeout).Return()
env.On("doGet", BFBatchURL).Return([]byte(tc.BatchJSONResponse), tc.Error) env.On("HTTPRequest", BFBatchURL).Return([]byte(tc.BatchJSONResponse), tc.Error)
env.On("doGet", BFBatchReadingsURL).Return([]byte(tc.BatchReadingsJSONResponse), tc.Error) env.On("HTTPRequest", BFBatchReadingsURL).Return([]byte(tc.BatchReadingsJSONResponse), tc.Error)
env.On("cache", nil).Return(cache) env.On("cache", nil).Return(cache)
if tc.Template != "" { if tc.Template != "" {

View file

@ -51,7 +51,7 @@ func (i *ipify) getResult() (string, error) {
httpTimeout := i.props.getInt(HTTPTimeout, DefaultHTTPTimeout) httpTimeout := i.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
body, err := i.env.doGet(url, httpTimeout) body, err := i.env.HTTPRequest(url, httpTimeout)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -47,7 +47,7 @@ func TestIpifySegment(t *testing.T) {
CacheTimeout: 0, CacheTimeout: 0,
} }
env.On("doGet", IPIFYAPIURL).Return([]byte(tc.Response), tc.Error) env.On("HTTPRequest", IPIFYAPIURL).Return([]byte(tc.Response), tc.Error)
if tc.Template != "" { if tc.Template != "" {
props[SegmentTemplate] = tc.Template props[SegmentTemplate] = tc.Template

View file

@ -126,7 +126,7 @@ func (ns *nightscout) getResult() (*NightscoutData, error) {
} }
} }
body, err := ns.env.doGet(url, httpTimeout) body, err := ns.env.HTTPRequest(url, httpTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -139,7 +139,7 @@ func TestNSSegment(t *testing.T) {
cache.On("get", FAKEAPIURL).Return(tc.JSONResponse, !tc.CacheFoundFail) cache.On("get", FAKEAPIURL).Return(tc.JSONResponse, !tc.CacheFoundFail)
cache.On("set", FAKEAPIURL, tc.JSONResponse, tc.CacheTimeout).Return() cache.On("set", FAKEAPIURL, tc.JSONResponse, tc.CacheTimeout).Return()
env.On("doGet", FAKEAPIURL).Return([]byte(tc.JSONResponse), tc.Error) env.On("HTTPRequest", FAKEAPIURL).Return([]byte(tc.JSONResponse), tc.Error)
env.On("cache", nil).Return(cache) env.On("cache", nil).Return(cache)
if tc.Template != "" { if tc.Template != "" {

View file

@ -99,7 +99,7 @@ func (d *owm) getResult() (*owmDataResponse, error) {
httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout) httpTimeout := d.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
d.URL = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey) d.URL = fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s", location, units, apikey)
body, err := d.env.doGet(d.URL, httpTimeout) body, err := d.env.HTTPRequest(d.URL, httpTimeout)
if err != nil { if err != nil {
return new(owmDataResponse), err return new(owmDataResponse), err
} }

View file

@ -58,7 +58,7 @@ func TestOWMSegmentSingle(t *testing.T) {
CacheTimeout: 0, CacheTimeout: 0,
} }
env.On("doGet", OWMAPIURL).Return([]byte(tc.JSONResponse), tc.Error) env.On("HTTPRequest", OWMAPIURL).Return([]byte(tc.JSONResponse), tc.Error)
if tc.Template != "" { if tc.Template != "" {
props[SegmentTemplate] = tc.Template props[SegmentTemplate] = tc.Template
@ -184,7 +184,7 @@ func TestOWMSegmentIcons(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID) response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString) expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString)
env.On("doGet", OWMAPIURL).Return([]byte(response), nil) env.On("HTTPRequest", OWMAPIURL).Return([]byte(response), nil)
o := &owm{ o := &owm{
props: properties{ props: properties{
@ -207,7 +207,7 @@ func TestOWMSegmentIcons(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID) response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", tc.ExpectedIconString) expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", tc.ExpectedIconString)
env.On("doGet", OWMAPIURL).Return([]byte(response), nil) env.On("HTTPRequest", OWMAPIURL).Return([]byte(response), nil)
o := &owm{ o := &owm{
props: properties{ props: properties{

View file

@ -134,7 +134,7 @@ func (env *MockedEnvironment) getWindowsRegistryKeyValue(path string) (*windowsR
return args.Get(0).(*windowsRegistryValue), args.Error(1) return args.Get(0).(*windowsRegistryValue), args.Error(1)
} }
func (env *MockedEnvironment) doGet(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) { func (env *MockedEnvironment) HTTPRequest(url string, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) {
args := env.Called(url) args := env.Called(url)
return args.Get(0).([]byte), args.Error(1) return args.Get(0).([]byte), args.Error(1)
} }

View file

@ -58,7 +58,7 @@ func (w *wakatime) setAPIData() error {
httpTimeout := w.props.getInt(HTTPTimeout, DefaultHTTPTimeout) httpTimeout := w.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
body, err := w.env.doGet(url, httpTimeout) body, err := w.env.HTTPRequest(url, httpTimeout)
if err != nil { if err != nil {
return err return err
} }

View file

@ -71,7 +71,7 @@ func TestWTTrackedTime(t *testing.T) {
response := fmt.Sprintf(`{"cummulative_total": {"seconds": %.2f, "text": "x"}}`, float64(tc.Seconds)) response := fmt.Sprintf(`{"cummulative_total": {"seconds": %.2f, "text": "x"}}`, float64(tc.Seconds))
env.On("doGet", FAKEAPIURL).Return([]byte(response), tc.Error) env.On("HTTPRequest", FAKEAPIURL).Return([]byte(response), tc.Error)
cache := &MockedCache{} cache := &MockedCache{}
cache.On("get", FAKEAPIURL).Return(response, !tc.CacheFoundFail) cache.On("get", FAKEAPIURL).Return(response, !tc.CacheFoundFail)

View file

@ -86,7 +86,7 @@ func (y *ytm) setStatus() error {
// https://github.com/ytmdesktop/ytmdesktop/wiki/Remote-Control-API // https://github.com/ytmdesktop/ytmdesktop/wiki/Remote-Control-API
url := y.props.getString(APIURL, "http://127.0.0.1:9863") url := y.props.getString(APIURL, "http://127.0.0.1:9863")
httpTimeout := y.props.getInt(APIURL, DefaultHTTPTimeout) httpTimeout := y.props.getInt(APIURL, DefaultHTTPTimeout)
body, err := y.env.doGet(url+"/query", httpTimeout) body, err := y.env.HTTPRequest(url+"/query", httpTimeout)
if err != nil { if err != nil {
return err return err
} }

View file

@ -43,7 +43,7 @@ func TestYTMStringStoppedSong(t *testing.T) {
func bootstrapYTMDATest(json string, err error) *ytm { func bootstrapYTMDATest(json string, err error) *ytm {
url := "http://127.0.0.1:9863" url := "http://127.0.0.1:9863"
env := new(MockedEnvironment) env := new(MockedEnvironment)
env.On("doGet", url+"/query").Return([]byte(json), err) env.On("HTTPRequest", url+"/query").Return([]byte(json), err)
ytm := &ytm{ ytm := &ytm{
env: env, env: env,
props: properties{ props: properties{