github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/api/txfeeds.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  
     9  	"github.com/bytom/bytom/blockchain/txfeed"
    10  	"github.com/bytom/bytom/errors"
    11  )
    12  
    13  // POST /create-txfeed
    14  func (a *API) createTxFeed(ctx context.Context, in struct {
    15  	Alias  string `json:"alias"`
    16  	Filter string `json:"filter"`
    17  }) Response {
    18  	if err := a.txFeedTracker.Create(ctx, in.Alias, in.Filter); err != nil {
    19  		log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Add TxFeed Failed")
    20  		return NewErrorResponse(err)
    21  	}
    22  	return NewSuccessResponse(nil)
    23  }
    24  
    25  // POST /get-transaction-feed
    26  func (a *API) getTxFeed(ctx context.Context, in struct {
    27  	Alias string `json:"alias,omitempty"`
    28  }) Response {
    29  	var tmpTxFeed interface{}
    30  	rawTxfeed, err := a.GetTxFeedByAlias(ctx, in.Alias)
    31  	if err != nil {
    32  		return NewErrorResponse(err)
    33  	}
    34  	err = json.Unmarshal(rawTxfeed, &tmpTxFeed)
    35  	if err != nil {
    36  		return NewErrorResponse(err)
    37  	}
    38  	data := map[string]interface{}{"txfeed": tmpTxFeed}
    39  	return NewSuccessResponse(data)
    40  }
    41  
    42  // POST /delete-transaction-feed
    43  func (a *API) deleteTxFeed(ctx context.Context, in struct {
    44  	Alias string `json:"alias,omitempty"`
    45  }) Response {
    46  	if err := a.txFeedTracker.Delete(ctx, in.Alias); err != nil {
    47  		return NewErrorResponse(err)
    48  	}
    49  	return NewSuccessResponse(nil)
    50  }
    51  
    52  // POST /update-transaction-feed
    53  func (a *API) updateTxFeed(ctx context.Context, in struct {
    54  	Alias  string `json:"alias"`
    55  	Filter string `json:"filter"`
    56  }) Response {
    57  	if err := a.txFeedTracker.Delete(ctx, in.Alias); err != nil {
    58  		return NewErrorResponse(err)
    59  	}
    60  	if err := a.txFeedTracker.Create(ctx, in.Alias, in.Filter); err != nil {
    61  		log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Update TxFeed Failed")
    62  		return NewErrorResponse(err)
    63  	}
    64  	return NewSuccessResponse(nil)
    65  }
    66  
    67  func (a *API) getTxFeeds() ([]txfeed.TxFeed, error) {
    68  	txFeed := txfeed.TxFeed{}
    69  	txFeeds := make([]txfeed.TxFeed, 0)
    70  
    71  	iter := a.txFeedTracker.DB.Iterator()
    72  	defer iter.Release()
    73  
    74  	for iter.Next() {
    75  		if err := json.Unmarshal(iter.Value(), &txFeed); err != nil {
    76  			return nil, err
    77  		}
    78  		txFeeds = append(txFeeds, txFeed)
    79  	}
    80  
    81  	return txFeeds, nil
    82  }
    83  
    84  // listTxFeeds is an http handler for listing txfeeds. It does not take a filter.
    85  // POST /list-transaction-feeds
    86  func (a *API) listTxFeeds(ctx context.Context) Response {
    87  	txFeeds, err := a.getTxFeeds()
    88  	if err != nil {
    89  		return NewErrorResponse(err)
    90  	}
    91  
    92  	return NewSuccessResponse(txFeeds)
    93  }
    94  
    95  func (a *API) GetTxFeedByAlias(ctx context.Context, filter string) ([]byte, error) {
    96  	jf, err := json.Marshal(filter)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	value := a.txFeedTracker.DB.Get(jf)
   102  	if value == nil {
   103  		return nil, errors.New("No transaction feed")
   104  	}
   105  
   106  	return value, nil
   107  }