Skip to content

Commit 09aa342

Browse files
committed
Refactor Client to consolidate URL parsing
1 parent ebe2d85 commit 09aa342

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

sumologic/sumologic_client.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ var endpoints = map[string]string{
4646

4747
var rateLimiter = time.NewTicker(time.Minute / 240)
4848

49+
func (s *Client) createSumoRequest(method, relativeURL string, body io.Reader) (*http.Request, error) {
50+
parsedRelativeURL, err := url.Parse(relativeURL)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
fullURL := s.BaseURL.ResolveReference(parsedRelativeURL).String()
56+
return createNewRequest(method, fullURL, body, s.AccessID, s.AccessKey, s.AuthJwt)
57+
}
58+
4959
func createNewRequest(method, url string, body io.Reader, accessID string, accessKey string, authJwt string) (*http.Request, error) {
5060
req, err := http.NewRequest(method, url, body)
5161
if err != nil {
@@ -68,15 +78,12 @@ func logRequestAndResponse(req *http.Request, resp *http.Response) {
6878
}
6979

7080
func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
71-
relativeURL, _ := url.Parse(urlPath)
72-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
73-
7481
body, err := json.Marshal(payload)
7582
if err != nil {
7683
return nil, err
7784
}
7885

79-
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
86+
req, err := s.createSumoRequest(http.MethodPost, urlPath, bytes.NewBuffer(body))
8087
if err != nil {
8188
return nil, err
8289
}
@@ -107,9 +114,7 @@ func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
107114
}
108115

109116
func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error) {
110-
relativeURL, _ := url.Parse(urlPath)
111-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
112-
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer([]byte(payload)), s.AccessID, s.AccessKey, s.AuthJwt)
117+
req, err := s.createSumoRequest(http.MethodPost, urlPath, bytes.NewBuffer([]byte(payload)))
113118
if err != nil {
114119
return nil, err
115120
}
@@ -139,17 +144,14 @@ func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error)
139144
}
140145

141146
func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
142-
relativeURL, _ := url.Parse(urlPath)
143-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
144-
145-
_, etag, _ := s.Get(sumoURL.String())
147+
_, etag, _ := s.Get(urlPath)
146148

147149
body, err := json.Marshal(payload)
148150
if err != nil {
149151
return nil, err
150152
}
151153

152-
req, err := createNewRequest(http.MethodPut, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
154+
req, err := s.createSumoRequest(http.MethodPut, urlPath, bytes.NewBuffer(body))
153155
if err != nil {
154156
return nil, err
155157
}
@@ -185,10 +187,7 @@ func (s *Client) Get(urlPath string) ([]byte, string, error) {
185187
}
186188

187189
func (s *Client) GetWithErrOpt(urlPath string, return404Err bool) ([]byte, string, error) {
188-
relativeURL, _ := url.Parse(urlPath)
189-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
190-
191-
req, err := createNewRequest(http.MethodGet, sumoURL.String(), nil, s.AccessID, s.AccessKey, s.AuthJwt)
190+
req, err := s.createSumoRequest(http.MethodGet, urlPath, nil)
192191
if err != nil {
193192
return nil, "", err
194193
}
@@ -225,10 +224,7 @@ func (s *Client) GetWithErrOpt(urlPath string, return404Err bool) ([]byte, strin
225224
}
226225

227226
func (s *Client) Delete(urlPath string) ([]byte, error) {
228-
relativeURL, _ := url.Parse(urlPath)
229-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
230-
231-
req, err := createNewRequest(http.MethodDelete, sumoURL.String(), nil, s.AccessID, s.AccessKey, s.AuthJwt)
227+
req, err := s.createSumoRequest(http.MethodDelete, urlPath, nil)
232228
if err != nil {
233229
return nil, err
234230
}

0 commit comments

Comments
 (0)