mergedapi.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 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. if api != nil {
  37. mergedApi.apis[api.Name()] = api.ApiVersion()
  38. for _, method := range api.Methods() {
  39. mergedApi.methods[method] = api
  40. }
  41. }
  42. }
  43. return mergedApi
  44. }
  45. // Supported RPC methods
  46. func (self *MergedApi) Methods() []string {
  47. all := make([]string, len(self.methods))
  48. for method, _ := range self.methods {
  49. all = append(all, method)
  50. }
  51. return all
  52. }
  53. // Call the correct API's Execute method for the given request
  54. func (self *MergedApi) Execute(req *shared.Request) (interface{}, error) {
  55. glog.V(logger.Detail).Infof("%s %s", req.Method, req.Params)
  56. if res, _ := self.handle(req); res != nil {
  57. return res, nil
  58. }
  59. if api, found := self.methods[req.Method]; found {
  60. return api.Execute(req)
  61. }
  62. return nil, shared.NewNotImplementedError(req.Method)
  63. }
  64. func (self *MergedApi) Name() string {
  65. return shared.MergedApiName
  66. }
  67. func (self *MergedApi) ApiVersion() string {
  68. return MergedApiVersion
  69. }
  70. func (self *MergedApi) handle(req *shared.Request) (interface{}, error) {
  71. if req.Method == "modules" { // provided API's
  72. return self.apis, nil
  73. }
  74. return nil, nil
  75. }