Skip to content

Commit 010be0b

Browse files
refactor circuit breaker & add it for method PUT (#265)
1 parent 78de641 commit 010be0b

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

pkg/gofr/service/circuit_breaker.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,23 @@ func (cb *CircuitBreaker) doRequest(ctx context.Context, method, path string, qu
181181
var err error
182182

183183
switch method {
184-
case "GET":
184+
case http.MethodGet:
185185
result, err = cb.executeWithCircuitBreaker(ctx, func(ctx context.Context) (*http.Response, error) {
186186
return cb.HTTP.GetWithHeaders(ctx, path, queryParams, headers)
187187
})
188-
case "POST":
188+
case http.MethodPost:
189189
result, err = cb.executeWithCircuitBreaker(ctx, func(ctx context.Context) (*http.Response, error) {
190190
return cb.HTTP.PostWithHeaders(ctx, path, queryParams, body, headers)
191191
})
192-
case "PATCH":
192+
case http.MethodPatch:
193193
result, err = cb.executeWithCircuitBreaker(ctx, func(ctx context.Context) (*http.Response, error) {
194194
return cb.HTTP.PatchWithHeaders(ctx, path, queryParams, body, headers)
195195
})
196-
case "PUT":
196+
case http.MethodPut:
197197
result, err = cb.executeWithCircuitBreaker(ctx, func(ctx context.Context) (*http.Response, error) {
198198
return cb.HTTP.PutWithHeaders(ctx, path, queryParams, body, headers)
199199
})
200-
case "DELETE":
200+
case http.MethodDelete:
201201
result, err = cb.executeWithCircuitBreaker(ctx, func(ctx context.Context) (*http.Response, error) {
202202
return cb.HTTP.DeleteWithHeaders(ctx, path, body, headers)
203203
})
@@ -211,50 +211,31 @@ func (cb *CircuitBreaker) doRequest(ctx context.Context, method, path string, qu
211211
return resp, err
212212
}
213213

214-
func (cb *CircuitBreaker) Get(ctx context.Context, api string, queryParams map[string]interface{}) (*http.Response, error) {
215-
return cb.doRequest(ctx, "GET", api, queryParams, nil, nil)
216-
}
217214
func (cb *CircuitBreaker) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
218215
headers map[string]string) (*http.Response, error) {
219-
return cb.doRequest(ctx, "GET", path, queryParams, nil, headers)
220-
}
221-
222-
// Post is a wrapper for doRequest with the POST method.
223-
func (cb *CircuitBreaker) Post(ctx context.Context, path string, queryParams map[string]interface{}, body []byte) (*http.Response, error) {
224-
return cb.doRequest(ctx, "POST", path, queryParams, body, nil)
216+
return cb.doRequest(ctx, http.MethodGet, path, queryParams, nil, headers)
225217
}
226218

227219
// PostWithHeaders is a wrapper for doRequest with the POST method and headers.
228220
func (cb *CircuitBreaker) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
229221
body []byte, headers map[string]string) (*http.Response, error) {
230-
return cb.doRequest(ctx, "POST", path, queryParams, body, headers)
231-
}
232-
233-
// Patch is a wrapper for doRequest with the PATCH method.
234-
func (cb *CircuitBreaker) Patch(ctx context.Context, path string, queryParams map[string]interface{},
235-
body []byte) (*http.Response, error) {
236-
return cb.doRequest(ctx, "PATCH", path, queryParams, body, nil)
222+
return cb.doRequest(ctx, http.MethodPost, path, queryParams, body, headers)
237223
}
238224

239225
// PatchWithHeaders is a wrapper for doRequest with the PATCH method and headers.
240226
func (cb *CircuitBreaker) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
241227
body []byte, headers map[string]string) (*http.Response, error) {
242-
return cb.doRequest(ctx, "PATCH", path, queryParams, body, headers)
228+
return cb.doRequest(ctx, http.MethodPatch, path, queryParams, body, headers)
243229
}
244230

245231
// PutWithHeaders is a wrapper for doRequest with the PUT method and headers.
246232
func (cb *CircuitBreaker) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
247233
body []byte, headers map[string]string) (*http.Response, error) {
248-
return cb.doRequest(ctx, "PUT", path, queryParams, body, headers)
249-
}
250-
251-
// Delete is a wrapper for doRequest with the DELETE method.
252-
func (cb *CircuitBreaker) Delete(ctx context.Context, path string, body []byte) (*http.Response, error) {
253-
return cb.doRequest(ctx, "DELETE", path, nil, body, nil)
234+
return cb.doRequest(ctx, http.MethodPut, path, queryParams, body, headers)
254235
}
255236

256237
// DeleteWithHeaders is a wrapper for doRequest with the DELETE method and headers.
257238
func (cb *CircuitBreaker) DeleteWithHeaders(ctx context.Context, path string, body []byte, headers map[string]string) (
258239
*http.Response, error) {
259-
return cb.doRequest(ctx, "DELETE", path, nil, body, headers)
240+
return cb.doRequest(ctx, http.MethodDelete, path, nil, body, headers)
260241
}

pkg/gofr/service/new.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func NewHTTPService(serviceAddress string, logger Logger, options ...Options) HT
8585
}
8686

8787
func (h *httpService) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error) {
88-
return h.createAndSendRequest(ctx, http.MethodGet, path, queryParams, nil, nil)
88+
return h.GetWithHeaders(ctx, path, queryParams, nil)
8989
}
9090

9191
func (h *httpService) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
@@ -95,7 +95,7 @@ func (h *httpService) GetWithHeaders(ctx context.Context, path string, queryPara
9595

9696
func (h *httpService) Post(ctx context.Context, path string, queryParams map[string]interface{},
9797
body []byte) (*http.Response, error) {
98-
return h.createAndSendRequest(ctx, http.MethodPost, path, queryParams, body, nil)
98+
return h.PostWithHeaders(ctx, path, queryParams, body, nil)
9999
}
100100

101101
func (h *httpService) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},

0 commit comments

Comments
 (0)