-
Notifications
You must be signed in to change notification settings - Fork 244
/
api.go
48 lines (40 loc) · 1.22 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package peer
import (
"context"
"errors"
)
var (
// ErrInvalidTopic error returned when the requested topic is not valid.
ErrInvalidTopic = errors.New("topic not valid")
// ErrInvalidRange error returned when max-min range is not valid.
ErrInvalidRange = errors.New("invalid range, Min should be lower or equal to Max")
// ErrDiscovererNotProvided error when discoverer is not being provided.
ErrDiscovererNotProvided = errors.New("discoverer not provided")
)
// PublicAPI represents a set of APIs from the `web3.peer` namespace.
type PublicAPI struct {
s *Service
}
// NewAPI creates an instance of the peer API.
func NewAPI(s *Service) *PublicAPI {
return &PublicAPI{s: s}
}
// DiscoverRequest json request for peer_discover.
type DiscoverRequest struct {
Topic string `json:"topic"`
Max int `json:"max"`
Min int `json:"min"`
}
// Discover is an implementation of `peer_discover` or `web3.peer.discover` API.
func (api *PublicAPI) Discover(context context.Context, req DiscoverRequest) (err error) {
if api.s.d == nil {
return ErrDiscovererNotProvided
}
if len(req.Topic) == 0 {
return ErrInvalidTopic
}
if req.Max < req.Min {
return ErrInvalidRange
}
return api.s.d.Discover(req.Topic, req.Max, req.Min)
}