From a51cc24345c26d17e3000f6c54201ea24e64496a Mon Sep 17 00:00:00 2001 From: Benjamin Bengfort Date: Sat, 5 Oct 2024 09:19:41 -0500 Subject: [PATCH] Refactor API Key Update (#226) --- pkg/web/apikeys.go | 41 +++++++++++++++++++ pkg/web/routes.go | 1 + .../partials/apikey/apikey_detail.html | 33 +++------------ .../partials/apikey/apikey_list.html | 22 +++++----- .../partials/apikey/apikey_preview.html | 36 ++++++++++++++++ 5 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 pkg/web/templates/partials/apikey/apikey_preview.html diff --git a/pkg/web/apikeys.go b/pkg/web/apikeys.go index 903ff676..3f2c6c65 100644 --- a/pkg/web/apikeys.go +++ b/pkg/web/apikeys.go @@ -165,6 +165,47 @@ func (s *Server) APIKeyDetail(c *gin.Context) { }) } +func (s *Server) UpdateAPIKeyPreview(c *gin.Context) { + var ( + err error + keyID ulid.ULID + apikey *models.APIKey + out *api.APIKey + ) + + // Parse the keyID from the URL + if keyID, err = ulid.Parse(c.Param("id")); err != nil { + c.JSON(http.StatusNotFound, api.Error("apikey not found")) + return + } + + // Fetch the model from the database + if apikey, err = s.store.RetrieveAPIKey(c.Request.Context(), keyID); err != nil { + if errors.Is(err, dberr.ErrNotFound) { + c.JSON(http.StatusNotFound, api.Error("apikey not found")) + return + } + + c.Error(err) + c.JSON(http.StatusInternalServerError, api.Error("unable to process apikey detail request")) + return + } + + if out, err = api.NewAPIKey(apikey); err != nil { + c.Error(err) + c.JSON(http.StatusInternalServerError, api.Error("unable to process apikey detail request")) + return + } + + // Content negotiation + c.Negotiate(http.StatusOK, gin.Negotiate{ + Offered: []string{binding.MIMEJSON, binding.MIMEHTML}, + Data: out, + HTMLName: "apikey_preview.html", + HTMLData: scene.New(c).WithAPIData(out), + }) +} + func (s *Server) UpdateAPIKey(c *gin.Context) { var ( err error diff --git a/pkg/web/routes.go b/pkg/web/routes.go index 7dd963c8..2b8e1274 100644 --- a/pkg/web/routes.go +++ b/pkg/web/routes.go @@ -211,6 +211,7 @@ func (s *Server) setupRoutes() (err error) { apikeys.GET("", authorize(permiss.APIKeysView), s.ListAPIKeys) apikeys.POST("", authorize(permiss.APIKeysManage), s.CreateAPIKey) apikeys.GET("/:id", authorize(permiss.APIKeysView), s.APIKeyDetail) + apikeys.GET("/:id/edit", authorize(permiss.APIKeysManage), s.UpdateAPIKeyPreview) apikeys.PUT("/:id", authorize(permiss.APIKeysManage), s.UpdateAPIKey) apikeys.DELETE("/:id", authorize(permiss.APIKeysRevoke), s.DeleteAPIKey) } diff --git a/pkg/web/templates/partials/apikey/apikey_detail.html b/pkg/web/templates/partials/apikey/apikey_detail.html index e2ca346c..0638d3a1 100644 --- a/pkg/web/templates/partials/apikey/apikey_detail.html +++ b/pkg/web/templates/partials/apikey/apikey_detail.html @@ -1,36 +1,15 @@ {{- with .APIKeyDetail -}} {{- end }} diff --git a/pkg/web/templates/partials/apikey/apikey_list.html b/pkg/web/templates/partials/apikey/apikey_list.html index 11eadfff..76aff76d 100644 --- a/pkg/web/templates/partials/apikey/apikey_list.html +++ b/pkg/web/templates/partials/apikey/apikey_list.html @@ -23,24 +23,24 @@ {{ end }}
- - +
+
+ +
+ + +
+
+ +
+
+ +{{- end }}