Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(SPV-1478): migrate existing admin v2 endpoints to openapi3 #899

Merged
merged 20 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
700a9d3
feat(SPV-1478): migrate existing admin users endpoint to openapi3 imp…
pawellewandowski98 Feb 8, 2025
7b9f9bc
chore(SPV-1478): fix linter errors
pawellewandowski98 Feb 8, 2025
cab6e35
chore: require fields in models
pawellewandowski98 Feb 9, 2025
2730aec
chore: regenerate models
pawellewandowski98 Feb 9, 2025
e35b2ea
chore: update response description
pawellewandowski98 Feb 9, 2025
c62bc88
feat: change the way of creating servers
pawellewandowski98 Feb 10, 2025
95b327a
Merge branch 'main' into feat/SPV-1478-migrate-admin-endpoints
pawellewandowski98 Feb 10, 2025
6ff6a5a
chore: regenerate api
pawellewandowski98 Feb 10, 2025
a96c3e2
chore: update api structures
pawellewandowski98 Feb 11, 2025
71ed788
chore: regenerate api
pawellewandowski98 Feb 11, 2025
c0398d5
Merge remote-tracking branch 'origin/main' into feat/SPV-1478-migrate…
dorzepowski Feb 12, 2025
958fbc6
fix: fixes after merge of main
dorzepowski Feb 13, 2025
8ff6321
feat: use operation ids
dorzepowski Feb 13, 2025
3fdc27f
feat: shorten names of generated models
dorzepowski Feb 13, 2025
986df03
feat: use generated models in endpoint handlers
dorzepowski Feb 13, 2025
0230ff4
Merge remote-tracking branch 'origin/main' into feat/SPV-1478-migrate…
dorzepowski Feb 13, 2025
423dfcc
chore: fix linter
dorzepowski Feb 13, 2025
4ba8a7c
adjust error on admin create user
dorzepowski Feb 14, 2025
a4b742b
remove user endpoints tag from shared config
dorzepowski Feb 14, 2025
0b22239
Merge remote-tracking branch 'origin/main' into feat/SPV-1478-migrate…
dorzepowski Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions actions/v2/admin/register.go

This file was deleted.

19 changes: 19 additions & 0 deletions actions/v2/admin/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package admin

import (
"github.com/bitcoin-sv/spv-wallet/actions/v2/admin/users"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/rs/zerolog"
)

// APIAdmin represents server with API endpoints
type APIAdmin struct {
users.APIAdminUsers
}

// NewAPIAdmin creates a new APIAdmin
func NewAPIAdmin(spvWalletEngine engine.ClientInterface, logger *zerolog.Logger) *APIAdmin {
return &APIAdmin{
*users.NewAPIAdminUsers(spvWalletEngine, logger),
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
}
}
5 changes: 1 addition & 4 deletions actions/v2/admin/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"github.com/gin-gonic/gin"
)

// Server represents server with API endpoints
type Server struct{}

// GetApiV2AdminStatus return the status of the server only after admin authentication
func (s *Server) GetApiV2AdminStatus(c *gin.Context) {
func (s *APIAdmin) GetApiV2AdminStatus(c *gin.Context) {
c.Status(http.StatusOK)
}
16 changes: 7 additions & 9 deletions actions/v2/admin/users/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ import (
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/v2/users/usersmodels"
"github.com/bitcoin-sv/spv-wallet/models/request/adminrequest"
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
"github.com/gin-gonic/gin"
)

func create(c *gin.Context, _ *reqctx.AdminContext) {
logger := reqctx.Logger(c)

// PostApiV2AdminUsers creates a new user
func (s *APIAdminUsers) PostApiV2AdminUsers(c *gin.Context) {
var requestBody adminrequest.CreateUser
if err := c.Bind(&requestBody); err != nil {
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.Wrap(err), logger)
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.Wrap(err), s.logger)
return
}

if err := validatePubKey(requestBody.PublicKey); err != nil {
spverrors.ErrorResponse(c, err, logger)
spverrors.ErrorResponse(c, err, s.logger)
return
}

Expand All @@ -34,7 +32,7 @@ func create(c *gin.Context, _ *reqctx.AdminContext) {
if requestBody.PaymailDefined() {
alias, domain, err := parsePaymail(requestBody.Paymail)
if err != nil {
spverrors.ErrorResponse(c, err, logger)
spverrors.ErrorResponse(c, err, s.logger)
return
}

Expand All @@ -47,9 +45,9 @@ func create(c *gin.Context, _ *reqctx.AdminContext) {
}
}

createdUser, err := reqctx.Engine(c).UsersService().Create(c, newUser)
createdUser, err := s.engine.UsersService().Create(c, newUser)
if err != nil {
spverrors.MapResponse(c, err, logger).
spverrors.MapResponse(c, err, s.logger).
If(configerrors.ErrUnsupportedDomain).Then(adminerrors.ErrInvalidDomain).
Else(adminerrors.ErrCreatingUser)
return
Expand Down
10 changes: 4 additions & 6 deletions actions/v2/admin/users/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import (

"github.com/bitcoin-sv/spv-wallet/actions/v2/admin/internal/mapping"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
"github.com/gin-gonic/gin"
)

func get(c *gin.Context, _ *reqctx.AdminContext) {
userID := c.Param("id")

user, err := reqctx.Engine(c).UsersService().GetByID(c, userID)
// GetApiV2AdminUsersId returns a user by ID
func (s *APIAdminUsers) GetApiV2AdminUsersId(c *gin.Context, id string) {
user, err := s.engine.UsersService().GetByID(c, id)
if err != nil {
spverrors.ErrorResponse(c, err, reqctx.Logger(c))
spverrors.ErrorResponse(c, err, s.logger)
return
}

Expand Down
18 changes: 7 additions & 11 deletions actions/v2/admin/users/paymail_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ import (
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/engine/v2/paymails/paymailsmodels"
"github.com/bitcoin-sv/spv-wallet/models/request/adminrequest"
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
"github.com/gin-gonic/gin"
)

func addPaymail(c *gin.Context, _ *reqctx.AdminContext) {
logger := reqctx.Logger(c)

// PostApiV2AdminUsersIdPaymails add paymails to the user
func (s *APIAdminUsers) PostApiV2AdminUsersIdPaymails(c *gin.Context, id string) {
var requestBody adminrequest.AddPaymail
if err := c.Bind(&requestBody); err != nil {
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.Wrap(err), logger)
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.Wrap(err), s.logger)
return
}

userID := c.Param("id")

alias, domain, err := parsePaymail(&requestBody)
if err != nil {
spverrors.ErrorResponse(c, err, logger)
spverrors.ErrorResponse(c, err, s.logger)
return
}

Expand All @@ -36,11 +32,11 @@ func addPaymail(c *gin.Context, _ *reqctx.AdminContext) {
Domain: domain,
PublicName: requestBody.PublicName,
Avatar: requestBody.Avatar,
UserID: userID,
UserID: id,
}
createdPaymail, err := reqctx.Engine(c).PaymailsService().Create(c, newPaymail)
createdPaymail, err := s.engine.PaymailsService().Create(c, newPaymail)
if err != nil {
spverrors.MapResponse(c, err, logger).
spverrors.MapResponse(c, err, s.logger).
If(configerrors.ErrUnsupportedDomain).Then(adminerrors.ErrInvalidDomain).
Else(adminerrors.ErrAddingPaymail)
return
Expand Down
15 changes: 0 additions & 15 deletions actions/v2/admin/users/routes.go

This file was deleted.

20 changes: 20 additions & 0 deletions actions/v2/admin/users/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package users

import (
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/rs/zerolog"
)

// APIAdminUsers represents server with admin API endpoints
type APIAdminUsers struct {
engine engine.ClientInterface
logger *zerolog.Logger
}

// NewAPIAdminUsers creates a new APIAdminUsers
func NewAPIAdminUsers(engine engine.ClientInterface, logger *zerolog.Logger) *APIAdminUsers {
return &APIAdminUsers{
engine: engine,
logger: logger,
}
}
6 changes: 0 additions & 6 deletions actions/v2/register.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v2

import (
"github.com/bitcoin-sv/spv-wallet/actions/v2/admin"
"github.com/bitcoin-sv/spv-wallet/actions/v2/base"
"github.com/bitcoin-sv/spv-wallet/actions/v2/data"
"github.com/bitcoin-sv/spv-wallet/actions/v2/operations"
"github.com/bitcoin-sv/spv-wallet/actions/v2/transactions"
Expand All @@ -16,8 +14,4 @@ func Register(handlersManager *handlers.Manager) {
operations.RegisterRoutes(handlersManager)
transactions.RegisterRoutes(handlersManager)
data.RegisterRoutes(handlersManager)

admin.Register(handlersManager)

base.RegisterRoutes(handlersManager)
}
11 changes: 8 additions & 3 deletions actions/v2/server.go
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package v2
import (
"github.com/bitcoin-sv/spv-wallet/actions/v2/admin"
"github.com/bitcoin-sv/spv-wallet/api"
"github.com/bitcoin-sv/spv-wallet/config"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/rs/zerolog"
)

// Server is the implementation of the server oapi-codegen's interface
type Server struct {
admin.Server
admin.APIAdmin
}

// check if the Server implements the interface api.ServerInterface
var _ api.ServerInterface = &Server{}

// NewServer creates a new server
func NewServer() *Server {
return &Server{}
func NewServer(config *config.AppConfig, engine engine.ClientInterface, logger *zerolog.Logger) *Server {
return &Server{
APIAdmin: *admin.NewAPIAdmin(engine, logger),
}
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
}
78 changes: 70 additions & 8 deletions api/components/errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ info:
version: ignored

components:
responses:
NotAuthorized:
description: Security requirements failed
content:
application/json:
schema:
$ref: "#/components/schemas/ErrUnauthorized"

schemas:
ErrorSchema:
type: object
Expand Down Expand Up @@ -73,3 +65,73 @@ components:
example: "error-user-auth-on-non-user-endpoint"
message:
example: "cannot call non-user endpoints with user authorization"

ErrCannotBindRequest:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-bind-body-invalid"
message:
example: "cannot bind request body"

ErrInvalidPubKey:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-user-invalid-pubkey"
message:
example: "invalid public key"

ErrInvalidPaymail:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-user-invalid-paymail"
message:
example: "invalid paymail"

ErrPaymailInconsistent:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-user-inconsistent-paymail"
message:
example: "inconsistent paymail address and alias/domain"

ErrInvalidDomain:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-user-invalid-domain"
message:
example: "invalid domain"

ErrCreatingUser:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-user-creating"
message:
example: "error creating user"

ErrGettingUser:
allOf:
- $ref: '#/components/schemas/ErrorSchema'
- type: object
properties:
code:
example: "error-unknown"
message:
example: "Internal server error"
56 changes: 56 additions & 0 deletions api/components/models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,59 @@ info:

components:
schemas:
User:
type: object
properties:
id:
type: string
example: "1"
publicKey:
type: string
example: "76a914e069bd2e2fe3ea702c40d5e65b491b734c01686788ac"
paymails:
type: array
items:
$ref: "#/components/schemas/Paymail"
createdAt:
type: string
format: date-time
example: "2021-01-01T00:00:00Z"
updatedAt:
type: string
format: date-time
example: "2021-01-01T00:00:00Z"
required:
- id
- publicKey
- paymails
- createdAt
- updatedAt

Paymail:
type: object
properties:
id:
type: string
example: "1"
alias:
type: string
example: "test"
domain:
type: string
example: "spv-wallet.com"
paymail:
type: string
example: "test@spv-wallet.com"
publicName:
type: string
example: "Test"
avatar:
type: string
example: "https://spv-wallet.com/avatar.png"
required:
- id
- alias
- domain
- paymail
- publicName
- avatar
Loading
Loading