Skip to content

Commit

Permalink
SQS-412 | Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
deividaspetraitis committed Sep 24, 2024
1 parent cc3d892 commit 2674eee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
15 changes: 15 additions & 0 deletions delivery/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"github.com/labstack/echo/v4"
"github.com/osmosis-labs/sqs/validator"

Check failure on line 5 in delivery/http/http.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/osmosis-labs/sqs/validator; to add it:
)

// RequestUnmarshaler is any type capable to unmarshal data from HTTP request to itself.
Expand All @@ -13,3 +14,17 @@ type RequestUnmarshaler interface {
func UnmarshalRequest(c echo.Context, m RequestUnmarshaler) error {
return m.UnmarshalHTTPRequest(c)
}

// ParseRequest encapsulates the request unmarshalling and validation logic.
// It unmarshals the request and validates it if the request implements the Validator interface.
func ParseRequest(c echo.Context, req RequestUnmarshaler) error {
if err := UnmarshalRequest(c, req); err != nil {
return err
}

v, ok := req.(validator.Validator)
if !ok {
return nil
}
return validator.Validate(v)
}
23 changes: 23 additions & 0 deletions delivery/http/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package http

import (
"context"

"github.com/labstack/echo/v4"
"go.opentelemetry.io/otel/trace"
)

// Span returns the current span from the context.
func Span(c echo.Context) (context.Context, trace.Span) {
ctx := c.Request().Context()
span := trace.SpanFromContext(ctx)
return ctx, span
}

// RecordSpanError records an error ( if any ) for the span.
func RecordSpanError(ctx context.Context, span trace.Span, err error) {
if err != nil {
span.RecordError(err)
}
// Note: we do not end the span here as it is ended in the middleware.
}
57 changes: 16 additions & 41 deletions passthrough/delivery/http/passthrough_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/osmosis-labs/sqs/orderbook/types"

"github.com/labstack/echo/v4"
"go.opentelemetry.io/otel/trace"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -72,38 +71,18 @@ func (a *PassthroughHandler) GetPortfolioAssetsByAddress(c echo.Context) error {
return c.JSON(http.StatusOK, portfolioAssetsResult)
}

// parseAndValidateGetActiveOrdersRequest encapsulates the request unmarshalling and validation logic
// for the GetActiveOrders request.
func (a *PassthroughHandler) parseAndValidateGetActiveOrdersRequest(c echo.Context) (types.GetActiveOrdersRequest, error) {
var req types.GetActiveOrdersRequest
if err := deliveryhttp.UnmarshalRequest(c, &req); err != nil {
return req, err
}

// Validate the request
if err := req.Validate(); err != nil {
return req, err
}

return req, nil
}

func (a *PassthroughHandler) GetActiveOrdersStream(c echo.Context) (err error) {
ctx := c.Request().Context()
func (a *PassthroughHandler) GetActiveOrdersStream(c echo.Context) error {
var (
req types.GetActiveOrdersRequest
err error
)

span := trace.SpanFromContext(ctx)
ctx, span := deliveryhttp.Span(c)
defer func() {
if err != nil {
span.RecordError(err)
// nolint:errcheck // ignore error
c.JSON(domain.GetStatusCode(err), domain.ResponseError{Message: err.Error()})
}

// Note: we do not end the span here as it is ended in the middleware.
deliveryhttp.RecordSpanError(ctx, span, err)
}()

req, err := a.parseAndValidateGetActiveOrdersRequest(c)
if err != nil {
if err := deliveryhttp.ParseRequest(c, &req); err != nil {
return c.JSON(http.StatusBadRequest, domain.ResponseError{Message: err.Error()})
}

Expand Down Expand Up @@ -150,22 +129,18 @@ func (a *PassthroughHandler) GetActiveOrdersStream(c echo.Context) (err error) {
// @Failure 500 {object} domain.ResponseError "Response error"
// @Param userOsmoAddress query string true "Osmo wallet address"
// @Router /passthrough/active-orders [get]
func (a *PassthroughHandler) GetActiveOrders(c echo.Context) (err error) {
ctx := c.Request().Context()
func (a *PassthroughHandler) GetActiveOrders(c echo.Context) error {
var (
req types.GetActiveOrdersRequest
err error
)

span := trace.SpanFromContext(ctx)
ctx, span := deliveryhttp.Span(c)
defer func() {
if err != nil {
span.RecordError(err)
// nolint:errcheck // ignore error
c.JSON(domain.GetStatusCode(err), domain.ResponseError{Message: err.Error()})
}

// Note: we do not end the span here as it is ended in the middleware.
deliveryhttp.RecordSpanError(ctx, span, err)
}()

req, err := a.parseAndValidateGetActiveOrdersRequest(c)
if err != nil {
if err = deliveryhttp.ParseRequest(c, &req); err != nil {
return c.JSON(http.StatusBadRequest, domain.ResponseError{Message: err.Error()})
}

Expand Down

0 comments on commit 2674eee

Please sign in to comment.