mergedapi.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "github.com/ethereum/go-ethereum/logger"
  19. "github.com/ethereum/go-ethereum/logger/glog"
  20. "github.com/ethereum/go-ethereum/rpc/shared"
  21. )
  22. const (
  23. MergedApiVersion = "1.0"
  24. )
  25. // combines multiple API's
  26. type MergedApi struct {
  27. apis map[string]string
  28. methods map[string]shared.EthereumApi
  29. }
  30. // create new merged api instance
  31. func newMergedApi(apis ...shared.EthereumApi) *MergedApi {
  32. mergedApi := new(MergedApi)
  33. mergedApi.apis = make(map[string]string, len(apis))
  34. mergedApi.methods = make(map[string]shared.EthereumApi)
  35. for _, api := range apis {
  36. mergedApi.apis[api.Name()] = api.ApiVersion()
  37. for _, method := range api.Methods() {
  38. mergedApi.methods[method] = api
  39. }
  40. }
  41. return mergedApi
  42. }
  43. // Supported RPC methods
  44. func (self *MergedApi) Methods() []string {
  45. all := make([]string, len(self.methods))
  46. for method, _ := range self.methods {
  47. all = append(all, method)
  48. }
  49. return all
  50. }
  51. // Call the correct API's Execute method for the given request
  52. func (self *MergedApi) Execute(req *shared.Request) (interface{}, error) {
  53. glog.V(logger.Detail).Infof("%s %s", req.Method, req.Params)
  54. if res, _ := self.handle(req); res != nil {
  55. return res, nil
  56. }
  57. if api, found := self.methods[req.Method]; found {
  58. return api.Execute(req)
  59. }
  60. return nil, shared.NewNotImplementedError(req.Method)
  61. }
  62. func (self *MergedApi) Name() string {
  63. return shared.MergedApiName
  64. }
  65. func (self *MergedApi) ApiVersion() string {
  66. return MergedApiVersion
  67. }
  68. func (self *MergedApi) handle(req *shared.Request) (interface{}, error) {
  69. if req.Method == "modules" { // provided API's
  70. return self.apis, nil
  71. }
  72. return nil, nil
  73. }