Skip to content

Commit

Permalink
fix: Rework API responses (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz committed Dec 1, 2022
1 parent 1dc229a commit f9e64ea
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 349 deletions.
24 changes: 18 additions & 6 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import (
"net/url"
"strings"
"time"
)

type ConfigUser struct {
Endpoint string `json:"endpoint" bson:"endpoint"`
Secret string `json:"secret" bson:"secret"`
EventTypes []string `json:"eventTypes" bson:"eventTypes"`
}
"github.com/google/uuid"
)

type Config struct {
ConfigUser `bson:"inline"`
Expand All @@ -23,6 +19,22 @@ type Config struct {
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}

type ConfigUser struct {
Endpoint string `json:"endpoint" bson:"endpoint"`
Secret string `json:"secret" bson:"secret"`
EventTypes []string `json:"eventTypes" bson:"eventTypes"`
}

func NewConfig(cfgUser ConfigUser) Config {
return Config{
ConfigUser: cfgUser,
ID: uuid.NewString(),
Active: true,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
}
}

const (
KeySecret = "secret"
KeyEventTypes = "eventTypes"
Expand Down
37 changes: 0 additions & 37 deletions pkg/server/activate_one.go

This file was deleted.

63 changes: 63 additions & 0 deletions pkg/server/activation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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/pkg/errors"
)

func (h *serverHandler) activateOneConfigHandle(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, PathParamId)
c, err := h.store.UpdateOneConfigActivation(r.Context(), id, true)
if err == nil {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s", PathConfigs, id, PathActivate)
resp := sharedapi.BaseResponse[webhooks.Config]{
Data: &c,
}
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 if errors.Is(err, storage.ErrConfigNotFound) {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s: %s", PathConfigs, id, PathActivate, storage.ErrConfigNotFound)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else if errors.Is(err, storage.ErrConfigNotModified) {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s: %s", PathConfigs, id, PathActivate, storage.ErrConfigNotModified)
w.WriteHeader(http.StatusNotModified)
} else {
sharedlogging.GetLogger(r.Context()).Errorf("PUT %s/%s%s: %s", PathConfigs, id, PathActivate, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}

func (h *serverHandler) deactivateOneConfigHandle(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, PathParamId)
c, err := h.store.UpdateOneConfigActivation(r.Context(), id, false)
if err == nil {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s", PathConfigs, id, PathDeactivate)
resp := sharedapi.BaseResponse[webhooks.Config]{
Data: &c,
}
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 if errors.Is(err, storage.ErrConfigNotFound) {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s: %s", PathConfigs, id, PathDeactivate, storage.ErrConfigNotFound)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else if errors.Is(err, storage.ErrConfigNotModified) {
sharedlogging.GetLogger(r.Context()).Infof("PUT %s/%s%s: %s", PathConfigs, id, PathDeactivate, storage.ErrConfigNotModified)
w.WriteHeader(http.StatusNotModified)
} else {
sharedlogging.GetLogger(r.Context()).Errorf("PUT %s/%s%s: %s", PathConfigs, id, PathDeactivate, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
37 changes: 0 additions & 37 deletions pkg/server/deactivate_one.go

This file was deleted.

9 changes: 5 additions & 4 deletions pkg/server/delete_one.go → pkg/server/delete.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package server

import (
"errors"
"net/http"

"github.com/formancehq/go-libs/sharedlogging"
"github.com/formancehq/webhooks/pkg/storage"
"github.com/go-chi/chi/v5"
"github.com/pkg/errors"
)

func (h *serverHandler) deleteOneConfigHandle(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, PathParamId)
err := deleteOneConfig(r.Context(), id, h.store)
err := h.store.DeleteOneConfig(r.Context(), id)
if err == nil {
sharedlogging.GetLogger(r.Context()).Infof("DELETE %s/%s", PathConfigs, id)
} else if errors.Is(err, ErrConfigNotFound) {
sharedlogging.GetLogger(r.Context()).Infof("DELETE %s/%s: %s", PathConfigs, id, ErrConfigNotFound)
} else if errors.Is(err, storage.ErrConfigNotFound) {
sharedlogging.GetLogger(r.Context()).Infof("DELETE %s/%s: %s", PathConfigs, id, storage.ErrConfigNotFound)
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
sharedlogging.GetLogger(r.Context()).Errorf("DELETE %s/%s: %s", PathConfigs, id, err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/server/get_many.go → pkg/server/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ func (h *serverHandler) getManyConfigsHandle(w http.ResponseWriter, r *http.Requ
return
}

cursor, err := h.store.FindManyConfigs(r.Context(), filter)
cfgs, err := h.store.FindManyConfigs(r.Context(), filter)
if err != nil {
sharedlogging.GetLogger(r.Context()).Errorf("storage.store.FindManyConfigs: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

resp := sharedapi.BaseResponse[webhooks.Config]{
Cursor: &cursor,
Cursor: &sharedapi.Cursor[webhooks.Config]{
Data: cfgs,
},
}

if err := json.NewEncoder(w).Encode(resp); err != nil {
Expand All @@ -35,7 +37,7 @@ func (h *serverHandler) getManyConfigsHandle(w http.ResponseWriter, r *http.Requ
return
}

sharedlogging.GetLogger(r.Context()).Infof("GET /configs: %d results", len(cursor.Data))
sharedlogging.GetLogger(r.Context()).Infof("GET /configs: %d results", len(resp.Cursor.Data))
}

var ErrInvalidParams = errors.New("invalid params: only 'id' and 'endpoint' with a valid URL are accepted")
Expand Down
File renamed without changes.
26 changes: 15 additions & 11 deletions pkg/server/insert_one.go → pkg/server/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package server

import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/formancehq/go-libs/sharedapi"
"github.com/formancehq/go-libs/sharedlogging"
webhooks "github.com/formancehq/webhooks/pkg"
"github.com/pkg/errors"
)

func (h *serverHandler) insertOneConfigHandle(w http.ResponseWriter, r *http.Request) {
Expand All @@ -24,21 +24,25 @@ func (h *serverHandler) insertOneConfigHandle(w http.ResponseWriter, r *http.Req
}

if err := cfg.Validate(); err != nil {
err := fmt.Errorf("invalid config: %w", err)
err := errors.Wrap(err, "invalid config")
sharedlogging.GetLogger(r.Context()).Errorf(err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if id, err := insertOneConfig(r.Context(), cfg, h.store); err != nil {
c, err := h.store.InsertOneConfig(r.Context(), cfg)
if err == nil {
sharedlogging.GetLogger(r.Context()).Infof("POST %s: inserted id %s", PathConfigs, c.ID)
resp := sharedapi.BaseResponse[webhooks.Config]{
Data: &c,
}
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("POST %s: %s", PathConfigs, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else if err := json.NewEncoder(w).Encode(id); 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()).Infof("POST %s: inserted id %s", PathConfigs, id)
}
}
Loading

0 comments on commit f9e64ea

Please sign in to comment.