Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Access] Refactor REST Routes Package #6616

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ endif

.PHONY: generate-openapi
generate-openapi:
swagger-codegen generate -l go -i https://raw.githubusercontent.com/onflow/flow/master/openapi/access.yaml -D packageName=models,modelDocs=false,models -o engine/access/rest/models;
go fmt ./engine/access/rest/models
swagger-codegen generate -l go -i https://raw.githubusercontent.com/onflow/flow/master/openapi/access.yaml -D packageName=models,modelDocs=false,models -o engine/access/rest/http/models;
go fmt ./engine/access/rest/http/models

.PHONY: generate
generate: generate-proto generate-mocks generate-fvm-env-wrappers
Expand Down
4 changes: 2 additions & 2 deletions cmd/access/node_builder/access_node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
"github.com/onflow/flow-go/engine/access/ingestion/tx_error_messages"
pingeng "github.com/onflow/flow-go/engine/access/ping"
"github.com/onflow/flow-go/engine/access/rest"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/rest/router"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
rpcConnection "github.com/onflow/flow-go/engine/access/rpc/connection"
Expand Down Expand Up @@ -1715,7 +1715,7 @@ func (builder *FlowAccessNodeBuilder) Build() (cmd.Node, error) {
return nil
}).
Module("rest metrics", func(node *cmd.NodeConfig) error {
m, err := metrics.NewRestCollector(routes.URLToRoute, node.MetricsRegisterer)
m, err := metrics.NewRestCollector(router.URLToRoute, node.MetricsRegisterer)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/observer/node_builder/observer_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/onflow/flow-go/engine/access/index"
"github.com/onflow/flow-go/engine/access/rest"
restapiproxy "github.com/onflow/flow-go/engine/access/rest/apiproxy"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/rest/router"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
rpcConnection "github.com/onflow/flow-go/engine/access/rpc/connection"
Expand Down Expand Up @@ -1682,7 +1682,7 @@ func (builder *ObserverServiceBuilder) enqueueRPCServer() {
return nil
})
builder.Module("rest metrics", func(node *cmd.NodeConfig) error {
m, err := metrics.NewRestCollector(routes.URLToRoute, node.MetricsRegisterer)
m, err := metrics.NewRestCollector(router.URLToRoute, node.MetricsRegisterer)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions engine/access/handle_irrecoverable_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

accessmock "github.com/onflow/flow-go/engine/access/mock"
"github.com/onflow/flow-go/engine/access/rest"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
statestreambackend "github.com/onflow/flow-go/engine/access/state_stream/backend"
Expand Down Expand Up @@ -249,7 +248,7 @@ func (suite *IrrecoverableStateTestSuite) TestRestInconsistentNodeState() {
// optionsForBlocksIdGetOpts returns options for the BlocksApi.BlocksIdGet function.
func optionsForBlocksIdGetOpts() *restclient.BlocksApiBlocksIdGetOpts {
return &restclient.BlocksApiBlocksIdGetOpts{
Expand: optional.NewInterface([]string{routes.ExpandableFieldPayload}),
Expand: optional.NewInterface([]string{"payload"}),
UlyanaAndrukhiv marked this conversation as resolved.
Show resolved Hide resolved
Select_: optional.NewInterface([]string{"header.id"}),
}
}
4 changes: 3 additions & 1 deletion engine/access/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ the [Flow OpenAPI definition](https://github.com/onflow/flow/blob/master/openapi
available on our [docs site](https://docs.onflow.org/http-api/).

## Packages

<!---
TODO: update README.md after the reviews
-->
- `rest`: The HTTP handlers for the server generator and the select filter, implementation of handling local requests.
- `middleware`: The common [middlewares](https://github.com/gorilla/mux#middleware) that all request pass through.
- `models`: The generated models using openapi generators and implementation of model builders.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package common

import (
"encoding/json"
Expand All @@ -11,7 +11,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/common/models"
fvmErrors "github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
Expand Down Expand Up @@ -46,15 +46,15 @@ func (h *HttpHandler) VerifyRequest(w http.ResponseWriter, r *http.Request) erro
r.Body = http.MaxBytesReader(w, r.Body, MaxRequestSize)
err := r.ParseForm()
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return err
}
return nil
}

func (h *HttpHandler) errorHandler(w http.ResponseWriter, err error, errorLogger zerolog.Logger) {
func (h *HttpHandler) ErrorHandler(w http.ResponseWriter, err error, errorLogger zerolog.Logger) {
// rest status type error should be returned with status and user message provided
var statusErr models.StatusError
var statusErr StatusError
if errors.As(err, &statusErr) {
h.errorResponse(w, statusErr.Status(), statusErr.UserMessage(), errorLogger)
return
Expand Down Expand Up @@ -98,8 +98,8 @@ func (h *HttpHandler) errorHandler(w http.ResponseWriter, err error, errorLogger
h.errorResponse(w, http.StatusInternalServerError, msg, errorLogger)
}

// jsonResponse builds a JSON response and send it to the client
func (h *HttpHandler) jsonResponse(w http.ResponseWriter, code int, response interface{}, errLogger zerolog.Logger) {
// JsonResponse builds a JSON response and send it to the client
func (h *HttpHandler) JsonResponse(w http.ResponseWriter, code int, response interface{}, errLogger zerolog.Logger) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")

// serialize response to JSON and handler errors
Expand Down Expand Up @@ -131,5 +131,5 @@ func (h *HttpHandler) errorResponse(
Code: int32(returnCode),
Message: responseMessage,
}
h.jsonResponse(w, returnCode, modelError, logger)
h.JsonResponse(w, returnCode, modelError, logger)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package common

import "net/http"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package request
package common

import (
"net/http"
"strings"

"github.com/gorilla/mux"

"github.com/onflow/flow-go/engine/access/rest/middleware"
"github.com/onflow/flow-go/engine/access/rest/common/middleware"
"github.com/onflow/flow-go/model/flow"
)

Expand All @@ -18,102 +18,6 @@ type Request struct {
Chain flow.Chain
}

func (rd *Request) GetScriptRequest() (GetScript, error) {
var req GetScript
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockRequest() (GetBlock, error) {
var req GetBlock
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockByIDsRequest() (GetBlockByIDs, error) {
var req GetBlockByIDs
err := req.Build(rd)
return req, err
}

func (rd *Request) GetBlockPayloadRequest() (GetBlockPayload, error) {
var req GetBlockPayload
err := req.Build(rd)
return req, err
}

func (rd *Request) GetCollectionRequest() (GetCollection, error) {
var req GetCollection
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountRequest() (GetAccount, error) {
var req GetAccount
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountBalanceRequest() (GetAccountBalance, error) {
var req GetAccountBalance
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountKeysRequest() (GetAccountKeys, error) {
var req GetAccountKeys
err := req.Build(rd)
return req, err
}

func (rd *Request) GetAccountKeyRequest() (GetAccountKey, error) {
var req GetAccountKey
err := req.Build(rd)
return req, err
}

func (rd *Request) GetExecutionResultByBlockIDsRequest() (GetExecutionResultByBlockIDs, error) {
var req GetExecutionResultByBlockIDs
err := req.Build(rd)
return req, err
}

func (rd *Request) GetExecutionResultRequest() (GetExecutionResult, error) {
var req GetExecutionResult
err := req.Build(rd)
return req, err
}

func (rd *Request) GetTransactionRequest() (GetTransaction, error) {
var req GetTransaction
err := req.Build(rd)
return req, err
}

func (rd *Request) GetTransactionResultRequest() (GetTransactionResult, error) {
var req GetTransactionResult
err := req.Build(rd)
return req, err
}

func (rd *Request) GetEventsRequest() (GetEvents, error) {
var req GetEvents
err := req.Build(rd)
return req, err
}

func (rd *Request) CreateTransactionRequest() (CreateTransaction, error) {
var req CreateTransaction
err := req.Build(rd)
return req, err
}

func (rd *Request) SubscribeEventsRequest() (SubscribeEvents, error) {
var req SubscribeEvents
err := req.Build(rd)
return req, err
}

func (rd *Request) Expands(field string) bool {
return rd.ExpandFields[field]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package routes
package http

import (
"net/http"

"github.com/rs/zerolog"

"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/engine/access/rest/http/models"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)

// ApiHandlerFunc is a function that contains endpoint handling logic,
// it fetches necessary resources and returns an error or response model.
type ApiHandlerFunc func(
r *request.Request,
r *common.Request,
backend access.API,
generator models.LinkGenerator,
) (interface{}, error)
Expand All @@ -24,7 +24,7 @@ type ApiHandlerFunc func(
// Handler function allows easier handling of errors and responses as it
// wraps functionality for handling error and responses outside of endpoint handling.
type Handler struct {
*HttpHandler
*common.HttpHandler
backend access.API
linkGenerator models.LinkGenerator
apiHandlerFunc ApiHandlerFunc
Expand All @@ -41,7 +41,7 @@ func NewHandler(
backend: backend,
apiHandlerFunc: handlerFunc,
linkGenerator: generator,
HttpHandler: NewHttpHandler(logger, chain),
HttpHandler: common.NewHttpHandler(logger, chain),
}

return handler
Expand All @@ -57,22 +57,22 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
decoratedRequest := request.Decorate(r, h.Chain)
decoratedRequest := common.Decorate(r, h.Chain)

// execute handler function and check for error
response, err := h.apiHandlerFunc(decoratedRequest, h.backend, h.linkGenerator)
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return
}

// apply the select filter if any select fields have been specified
response, err = util.SelectFilter(response, decoratedRequest.Selects())
if err != nil {
h.errorHandler(w, err, errLog)
h.ErrorHandler(w, err, errLog)
return
}

// write response to response stream
h.jsonResponse(w, http.StatusOK, response, errLog)
h.JsonResponse(w, http.StatusOK, response, errLog)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package request
import (
"io"

"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/model/flow"
)

type CreateTransaction struct {
Transaction flow.TransactionBody
}

func (c *CreateTransaction) Build(r *Request) error {
func CreateTransactionRequest(r *common.Request) (CreateTransaction, error) {
var req CreateTransaction
err := req.Build(r)
return req, err
}

func (c *CreateTransaction) Build(r *common.Request) error {
return c.Parse(r.Body, r.Chain)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package request

import (
"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/model/flow"
)

Expand All @@ -12,7 +13,17 @@ type GetAccount struct {
Height uint64
}

func (g *GetAccount) Build(r *Request) error {
// GetAccountRequest extracts necessary variables and query parameters from the provided request,
// builds a GetAccount instance, and validates it.
//
// No errors are expected during normal operation.
func GetAccountRequest(r *common.Request) (GetAccount, error) {
var req GetAccount
err := req.Build(r)
return req, err
}

func (g *GetAccount) Build(r *common.Request) error {
return g.Parse(
r.GetVar(addressVar),
r.GetQueryParam(blockHeightQuery),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package request

import (
"github.com/onflow/flow-go/engine/access/rest/common"
"github.com/onflow/flow-go/model/flow"
)

Expand All @@ -9,7 +10,17 @@ type GetAccountBalance struct {
Height uint64
}

func (g *GetAccountBalance) Build(r *Request) error {
// GetAccountBalanceRequest extracts necessary variables and query parameters from the provided request,
// builds a GetAccountBalance instance, and validates it.
//
// No errors are expected during normal operation.
func GetAccountBalanceRequest(r *common.Request) (GetAccountBalance, error) {
var req GetAccountBalance
err := req.Build(r)
return req, err
}

func (g *GetAccountBalance) Build(r *common.Request) error {
return g.Parse(
r.GetVar(addressVar),
r.GetQueryParam(blockHeightQuery),
Expand Down
Loading
Loading