github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/request/subscribe_events.go (about) 1 package request 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/onflow/flow-go/model/flow" 8 ) 9 10 const startBlockIdQuery = "start_block_id" 11 const eventTypesQuery = "event_types" 12 const addressesQuery = "addresses" 13 const contractsQuery = "contracts" 14 const heartbeatIntervalQuery = "heartbeat_interval" 15 16 type SubscribeEvents struct { 17 StartBlockID flow.Identifier 18 StartHeight uint64 19 20 EventTypes []string 21 Addresses []string 22 Contracts []string 23 24 HeartbeatInterval uint64 25 } 26 27 func (g *SubscribeEvents) Build(r *Request) error { 28 return g.Parse( 29 r.GetQueryParam(startBlockIdQuery), 30 r.GetQueryParam(startHeightQuery), 31 r.GetQueryParams(eventTypesQuery), 32 r.GetQueryParams(addressesQuery), 33 r.GetQueryParams(contractsQuery), 34 r.GetQueryParam(heartbeatIntervalQuery), 35 ) 36 } 37 38 func (g *SubscribeEvents) Parse( 39 rawStartBlockID string, 40 rawStartHeight string, 41 rawTypes []string, 42 rawAddresses []string, 43 rawContracts []string, 44 rawHeartbeatInterval string, 45 ) error { 46 var startBlockID ID 47 err := startBlockID.Parse(rawStartBlockID) 48 if err != nil { 49 return err 50 } 51 g.StartBlockID = startBlockID.Flow() 52 53 var height Height 54 err = height.Parse(rawStartHeight) 55 if err != nil { 56 return fmt.Errorf("invalid start height: %w", err) 57 } 58 g.StartHeight = height.Flow() 59 60 // if both start_block_id and start_height are provided 61 if g.StartBlockID != flow.ZeroID && g.StartHeight != EmptyHeight { 62 return fmt.Errorf("can only provide either block ID or start height") 63 } 64 65 // default to root block 66 if g.StartHeight == EmptyHeight { 67 g.StartHeight = 0 68 } 69 70 var eventTypes EventTypes 71 err = eventTypes.Parse(rawTypes) 72 if err != nil { 73 return err 74 } 75 76 g.EventTypes = eventTypes.Flow() 77 g.Addresses = rawAddresses 78 g.Contracts = rawContracts 79 80 // parse heartbeat interval 81 if rawHeartbeatInterval == "" { 82 // set zero if the interval wasn't passed in request, so we can check it later and apply any default value if needed 83 g.HeartbeatInterval = 0 84 return nil 85 } 86 87 g.HeartbeatInterval, err = strconv.ParseUint(rawHeartbeatInterval, 10, 64) 88 if err != nil { 89 return fmt.Errorf("invalid heartbeat interval format") 90 } 91 92 return nil 93 }