Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Use new ParseRequestByHeader and SendResponseByHeader funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Oct 21, 2023
1 parent 8d12493 commit d1c6f86
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 64 deletions.
36 changes: 6 additions & 30 deletions components/restapi/core/blocks.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package core

import (
"io"

"github.com/labstack/echo/v4"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/inx-app/pkg/httpserver"
"github.com/iotaledger/iota-core/pkg/blockhandler"
"github.com/iotaledger/iota-core/pkg/model"
Expand Down Expand Up @@ -78,37 +75,16 @@ func blockIssuanceBySlot(slotIndex iotago.SlotIndex) (*apimodels.IssuanceBlockHe
}

func sendBlock(c echo.Context) (*apimodels.BlockCreatedResponse, error) {
mimeType, err := httpserver.GetRequestContentType(c, httpserver.MIMEApplicationVendorIOTASerializerV2, echo.MIMEApplicationJSON)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

var iotaBlock *iotago.ProtocolBlock

if c.Request().Body == nil {
// bad request
return nil, ierrors.Wrap(httpserver.ErrInvalidParameter, "invalid block, error: request body missing")
}

bytes, err := io.ReadAll(c.Request().Body)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

switch mimeType {
case echo.MIMEApplicationJSON:
if err := deps.Protocol.CommittedAPI().JSONDecode(bytes, iotaBlock, serix.WithValidation()); err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

case httpserver.MIMEApplicationVendorIOTASerializerV2:
iotaBlock, _, err = iotago.ProtocolBlockFromBytes(deps.Protocol)(bytes)
iotaBlock, err := httpserver.ParseRequestByHeader(c, deps.Protocol, func(apiProvider iotago.APIProvider, bytes []byte) (*iotago.ProtocolBlock, error) {
iotaBlock, _, err := iotago.ProtocolBlockFromBytes(deps.Protocol)(bytes)
if err != nil {
return nil, ierrors.Wrapf(httpserver.ErrInvalidParameter, "invalid block, error: %w", err)
}

default:
return nil, echo.ErrUnsupportedMediaType
return iotaBlock, nil
})
if err != nil {
return nil, err
}

blockID, err := deps.BlockHandler.AttachBlock(c.Request().Context(), iotaBlock)
Expand Down
27 changes: 2 additions & 25 deletions components/restapi/core/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,29 +383,6 @@ func checkNodeSynced() echo.MiddlewareFunc {
}

func responseByHeader(c echo.Context, obj any) error {
mimeType, err := httpserver.GetAcceptHeaderContentType(c, httpserver.MIMEApplicationVendorIOTASerializerV2, echo.MIMEApplicationJSON)
if err != nil && err != httpserver.ErrNotAcceptable {
return err
}

switch mimeType {
case httpserver.MIMEApplicationVendorIOTASerializerV2:
// TODO: that should take the API that belongs to the object
b, err := deps.Protocol.CommittedAPI().Encode(obj)
if err != nil {
return err
}

return c.Blob(http.StatusOK, httpserver.MIMEApplicationVendorIOTASerializerV2, b)

// default to echo.MIMEApplicationJSON
default:
// TODO: that should take the API that belongs to the object
j, err := deps.Protocol.CommittedAPI().JSONEncode(obj)
if err != nil {
return err
}

return c.Blob(http.StatusOK, echo.MIMEApplicationJSON, j)
}
// TODO: that should take the API that belongs to the object
return httpserver.SendResponseByHeader(c, deps.Protocol.CommittedAPI(), obj)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ require (
github.com/iotaledger/hive.go/runtime v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b
github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021193746-dfd0ae303cd3
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021180345-9a57a3eeac3b
github.com/iotaledger/iota.go/v4 v4.0.0-20231020175721-e9e5ab76e32d
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021205206-ae075218c732
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2
github.com/iotaledger/iota.go/v4 v4.0.0-20231021203915-ad9b32d411e8
github.com/labstack/echo/v4 v4.11.2
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.31.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,12 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231020115340-13da292c580b/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g=
github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b h1:MDZhTZTVDiydXcW5j4TA7HixVCyAdToIMPhHfJee7cE=
github.com/iotaledger/hive.go/stringify v0.0.0-20231020115340-13da292c580b/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021193746-dfd0ae303cd3 h1:h6UPW2mAS9ezyBNiTxplG/9Ks5B+1j3o+ruE6r4Gcpk=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021193746-dfd0ae303cd3/go.mod h1:BV9ZwStmolK+65z0chdTEuQos0ih+d/roagO5umDIl8=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021180345-9a57a3eeac3b h1:pa3Ht3ZOyeRLhBMxjTjUsF1KW4aoPXJ+nJi5Tz8s1BY=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021180345-9a57a3eeac3b/go.mod h1:yXIAs6BGYIljDvx8o6OSuyM03lQ3+HJ7sL4sG3RYSDs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231020175721-e9e5ab76e32d h1:PAAXoVUIsoQGJy2NTsbwxKSsnBan7PZx+BMkCZPmzIs=
github.com/iotaledger/iota.go/v4 v4.0.0-20231020175721-e9e5ab76e32d/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021205206-ae075218c732 h1:VlrpB5wftbkUn7tbGagweUZNE1S0mh29Za8cGCbo9T0=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231021205206-ae075218c732/go.mod h1:PA5WAQu9uONyd5H4tBRJvpsMjRaDUCQ/y5cLnBuWFTM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2 h1:xC5hFWN5wSGvEFjPWUWdmlj6UuYJOo72T0r0AfbzRUs=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231021205014-392b75609cc2/go.mod h1:8CiEU9nM9q37wtYBnElactUR6RiZ2IGXX7RRtswRuVI=
github.com/iotaledger/iota.go/v4 v4.0.0-20231021203915-ad9b32d411e8 h1:tHLUwh8nFZk5VsBH/rNr804q0ReTFS0WhfKy/1nAYlw=
github.com/iotaledger/iota.go/v4 v4.0.0-20231021203915-ad9b32d411e8/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down

0 comments on commit d1c6f86

Please sign in to comment.