Skip to content

Commit

Permalink
Add Schema field to the new HTTP delegated routing translator
Browse files Browse the repository at this point in the history
Add `Schema` field to identify the internal HTTP schema used by the new
delegated routing.

See:
- ipfs/specs#337 (comment)
  • Loading branch information
masih committed Dec 7, 2022
1 parent e446cf1 commit ec5ab27
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion delegated_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/filecoin-shipyard/indexstar/httpserver"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multicodec"
)

const (
unknownProtocol = "unknown"
unknownSchema = unknownProtocol
)

func NewDelegatedTranslator(backend findFunc) (http.Handler, error) {
Expand Down Expand Up @@ -87,7 +93,8 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) {
err := md.UnmarshalBinary(p.Metadata)
if err != nil {
out.Providers = append(out.Providers, drProvider{
Protocol: "unknown",
Protocol: unknownProtocol,
Schema: unknownSchema,
ID: p.Provider.ID,
Addrs: p.Provider.Addrs,
})
Expand All @@ -97,6 +104,7 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) {
plb, _ := pl.MarshalBinary()
out.Providers = append(out.Providers, drProvider{
Protocol: proto.String(),
Schema: schemaByProtocolID(proto),
ID: p.Provider.ID,
Addrs: p.Provider.Addrs,
Metadata: plb,
Expand All @@ -114,12 +122,24 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request) {
httpserver.WriteJsonResponse(w, http.StatusOK, outBytes)
}

func schemaByProtocolID(p multicodec.Code) string {
switch p {
case multicodec.TransportBitswap:
return "bitswap"
case multicodec.TransportGraphsyncFilecoinv1:
return "graphsync-filecoinv1"
default:
return unknownProtocol
}
}

type drResp struct {
Providers []drProvider
}

type drProvider struct {
Protocol string
Schema string
ID peer.ID
Addrs []multiaddr.Multiaddr
Metadata []byte `json:",omitempty"`
Expand Down

0 comments on commit ec5ab27

Please sign in to comment.