-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New webhook testing route (#40)
- Loading branch information
Antoine Gelloz
authored
Dec 1, 2022
1 parent
f9e64ea
commit 4b5dfe2
Showing
11 changed files
with
165 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/formancehq/go-libs/sharedapi" | ||
"github.com/formancehq/go-libs/sharedlogging" | ||
webhooks "github.com/formancehq/webhooks/pkg" | ||
"github.com/formancehq/webhooks/pkg/storage" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func (h *serverHandler) testOneConfigHandle(w http.ResponseWriter, r *http.Request) { | ||
id := chi.URLParam(r, PathParamId) | ||
cfgs, err := h.store.FindManyConfigs(r.Context(), map[string]any{webhooks.KeyID: id}) | ||
if err == nil { | ||
if len(cfgs) == 0 { | ||
sharedlogging.GetLogger(r.Context()).Errorf("GET %s/%s%s: %s", PathConfigs, id, PathTest, storage.ErrConfigNotFound) | ||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | ||
return | ||
} | ||
sharedlogging.GetLogger(r.Context()).Infof("GET %s/%s%s", PathConfigs, id, PathTest) | ||
attempt, err := webhooks.MakeAttempt(r.Context(), h.httpClient, nil, | ||
uuid.NewString(), 0, cfgs[0], []byte(`{"data":"test"}`), true) | ||
if err != nil { | ||
sharedlogging.GetLogger(r.Context()).Errorf("GET %s/%s%s: %s", PathConfigs, id, PathTest, err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} else { | ||
sharedlogging.GetLogger(r.Context()).Infof("GET %s/%s%s", PathConfigs, id, PathTest) | ||
resp := sharedapi.BaseResponse[webhooks.Attempt]{ | ||
Data: &attempt, | ||
} | ||
if err := json.NewEncoder(w).Encode(resp); err != nil { | ||
sharedlogging.GetLogger(r.Context()).Errorf("json.Encoder.Encode: %s", err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
} else { | ||
sharedlogging.GetLogger(r.Context()).Errorf("GET %s/%s%s: %s", PathConfigs, id, PathTest, err) | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters