github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/models/link.go (about)

     1  package models
     2  
     3  import (
     4  	"github.com/gorilla/mux"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  )
     8  
     9  func (l *Links) Build(link string, err error) error {
    10  	if err != nil {
    11  		return err
    12  	}
    13  
    14  	l.Self = link
    15  	return nil
    16  }
    17  
    18  // LinkGenerator generates the expandable value for the known endpoints
    19  // e.g. "/v1/blocks/c5e935bc75163db82e4a6cf9dc3b54656709d3e21c87385138300abd479c33b7"
    20  type LinkGenerator interface {
    21  	BlockLink(id flow.Identifier) (string, error)
    22  	TransactionLink(id flow.Identifier) (string, error)
    23  	TransactionResultLink(id flow.Identifier) (string, error)
    24  	PayloadLink(id flow.Identifier) (string, error)
    25  	ExecutionResultLink(id flow.Identifier) (string, error)
    26  	AccountLink(address string) (string, error)
    27  	CollectionLink(id flow.Identifier) (string, error)
    28  }
    29  
    30  type LinkFunc func(id flow.Identifier) (string, error)
    31  
    32  type LinkGeneratorImpl struct {
    33  	router *mux.Router
    34  }
    35  
    36  func NewLinkGeneratorImpl(router *mux.Router) *LinkGeneratorImpl {
    37  	return &LinkGeneratorImpl{
    38  		router: router,
    39  	}
    40  }
    41  
    42  func (generator *LinkGeneratorImpl) BlockLink(id flow.Identifier) (string, error) {
    43  	return generator.linkForID("getBlocksByIDs", id)
    44  }
    45  func (generator *LinkGeneratorImpl) PayloadLink(id flow.Identifier) (string, error) {
    46  	return generator.linkForID("getBlockPayloadByID", id)
    47  }
    48  func (generator *LinkGeneratorImpl) ExecutionResultLink(id flow.Identifier) (string, error) {
    49  	return generator.linkForID("getExecutionResultByID", id)
    50  }
    51  
    52  func (generator *LinkGeneratorImpl) TransactionLink(id flow.Identifier) (string, error) {
    53  	return generator.linkForID("getTransactionByID", id)
    54  }
    55  
    56  func (generator *LinkGeneratorImpl) TransactionResultLink(id flow.Identifier) (string, error) {
    57  	return generator.linkForID("getTransactionResultByID", id)
    58  }
    59  
    60  func (generator *LinkGeneratorImpl) CollectionLink(id flow.Identifier) (string, error) {
    61  	return generator.linkForID("getCollectionByID", id)
    62  }
    63  
    64  func (generator *LinkGeneratorImpl) AccountLink(address string) (string, error) {
    65  	return generator.link("getAccount", "address", address)
    66  }
    67  
    68  // SelfLink generates the _link key value pair for the response
    69  // e.g.
    70  // "_links": { "_self": "/v1/blocks/c5e935bc75163db82e4a6cf9dc3b54656709d3e21c87385138300abd479c33b7" sx}
    71  func SelfLink(id flow.Identifier, linkFun LinkFunc) (*Links, error) {
    72  	url, err := linkFun(id)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	var link Links
    77  	err = link.Build(url, nil)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return &link, nil
    82  }
    83  
    84  func (generator *LinkGeneratorImpl) linkForID(route string, id flow.Identifier) (string, error) {
    85  	return generator.link(route, "id", id.String())
    86  }
    87  
    88  func (generator *LinkGeneratorImpl) link(route string, key string, value string) (string, error) {
    89  	url, err := generator.router.Get(route).URLPath(key, value)
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  	return url.String(), nil
    94  }