github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/mergedapi.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/logger"
    21  	"github.com/ethereum/go-ethereum/logger/glog"
    22  	"github.com/ethereum/go-ethereum/rpc/shared"
    23  )
    24  
    25  const (
    26  	MergedApiVersion = "1.0"
    27  )
    28  
    29  // combines multiple API's
    30  type MergedApi struct {
    31  	apis    map[string]string
    32  	methods map[string]shared.EthereumApi
    33  }
    34  
    35  // create new merged api instance
    36  func newMergedApi(apis ...shared.EthereumApi) *MergedApi {
    37  	mergedApi := new(MergedApi)
    38  	mergedApi.apis = make(map[string]string, len(apis))
    39  	mergedApi.methods = make(map[string]shared.EthereumApi)
    40  
    41  	for _, api := range apis {
    42  		mergedApi.apis[api.Name()] = api.ApiVersion()
    43  		for _, method := range api.Methods() {
    44  			mergedApi.methods[method] = api
    45  		}
    46  	}
    47  	return mergedApi
    48  }
    49  
    50  // Supported RPC methods
    51  func (self *MergedApi) Methods() []string {
    52  	all := make([]string, len(self.methods))
    53  	for method, _ := range self.methods {
    54  		all = append(all, method)
    55  	}
    56  	return all
    57  }
    58  
    59  // Call the correct API's Execute method for the given request
    60  func (self *MergedApi) Execute(req *shared.Request) (interface{}, error) {
    61  	glog.V(logger.Detail).Infof("%s %s", req.Method, req.Params)
    62  
    63  	if res, _ := self.handle(req); res != nil {
    64  		return res, nil
    65  	}
    66  	if api, found := self.methods[req.Method]; found {
    67  		return api.Execute(req)
    68  	}
    69  	return nil, shared.NewNotImplementedError(req.Method)
    70  }
    71  
    72  func (self *MergedApi) Name() string {
    73  	return shared.MergedApiName
    74  }
    75  
    76  func (self *MergedApi) ApiVersion() string {
    77  	return MergedApiVersion
    78  }
    79  
    80  func (self *MergedApi) handle(req *shared.Request) (interface{}, error) {
    81  	if req.Method == "modules" { // provided API's
    82  		return self.apis, nil
    83  	}
    84  
    85  	return nil, nil
    86  }