Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Dec 29, 2022
1 parent 42f8537 commit 6c392b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
12 changes: 7 additions & 5 deletions internal/service/handlers/get_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ import (
func GetHandler(w http.ResponseWriter, r *http.Request) {
nodes, err := helpers.Noder(r).Select()
if err != nil {
helpers.Log(r).Error("failed to get node list")
helpers.Log(r).WithError(err).Error("failed to get node list")
ape.RenderErr(w, problems.InternalError())
return
}

var res *http.Response
successful := false
for i := 0; i < len(nodes); i++ {
endpoint := fmt.Sprintf("%v%v", nodes[i], r.RequestURI)
res, err = http.Get(endpoint)
if res != nil && res.StatusCode >= 200 && res.StatusCode < 300 {
if err == nil && res != nil && res.StatusCode >= 200 && res.StatusCode < 300 {
successful = true
break
}
}

if err != nil {
if !successful {
helpers.Log(r).Error("failed to send request to all nodes")
ape.RenderErr(w, problems.InternalError())
return
}

body, err := io.ReadAll(res.Body)
if err != nil {
helpers.Log(r).Error("failed to read response body")
helpers.Log(r).WithError(err).Error("failed to read response body")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -45,7 +47,7 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {

_, err = w.Write(body)
if err != nil {
helpers.Log(r).Error("failed to write to response")
helpers.Log(r).WithError(err).Error("failed to write to response")
ape.RenderErr(w, problems.InternalError())
return
}
Expand Down
15 changes: 8 additions & 7 deletions internal/service/handlers/post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func PostHandler(w http.ResponseWriter, r *http.Request) {
nodes, err := helpers.Noder(r).Select()
if err != nil {
helpers.Log(r).Error("failed to get node list")
helpers.Log(r).WithError(err).Error("failed to get node list")
ape.RenderErr(w, problems.InternalError())
return
}
Expand All @@ -26,22 +26,23 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
for i := 0; i < len(nodes); i++ {
body, err := helpers.AppendTxToBody(r, tx)
if err != nil {
helpers.Log(r).Error("failed to append tx to body")
helpers.Log(r).WithError(err).Error("failed to append tx to body")
ape.RenderErr(w, problems.InternalError())
return
}

tx, status, err = helpers.SendRequest(bytes.NewBuffer(body), fmt.Sprintf("%v%v", nodes[i], r.RequestURI))
if err != nil {
helpers.Log(r).WithError(err).Error("failed to send request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}
if status == 400 {
helpers.Log(r).Error("validation failed")
ape.RenderErr(w, problems.BadRequest(errors.New("validation failed"))...)
return
}
if err != nil {
helpers.Log(r).Error("failed to send request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

if tx == nil {
helpers.Log(r).Error("failed to build transaction")
ape.RenderErr(w, problems.InternalError())
Expand Down
2 changes: 1 addition & 1 deletion internal/service/helpers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func AppendTxToBody(r *http.Request, tx *resources.EvmTransaction) ([]byte, erro
func SendRequest(body io.Reader, endpoint string) (*resources.EvmTransaction, int, error) {
res, err := http.Post(endpoint, "application/json", body)
if err != nil {
return nil, res.StatusCode, errors.Wrap(err, "failed to send request, endpoint: "+endpoint)
return nil, 0, errors.Wrap(err, "failed to send request, endpoint: "+endpoint)
}
if res.StatusCode == 400 {
return nil, res.StatusCode, errors.New("invalid parameters")
Expand Down

0 comments on commit 6c392b5

Please sign in to comment.