-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4547 from UlyanaAndrukhiv/UlyanaAndrukhiv/4379-re…
…st-event-streaming [Access] Enable Event streaming on REST API
- Loading branch information
Showing
36 changed files
with
1,561 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package request | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type EventType string | ||
|
||
var basicEventRe = regexp.MustCompile(`[A-Z]\.[a-f0-9]{16}\.[\w+]*\.[\w+]*`) | ||
var flowEventRe = regexp.MustCompile(`flow\.[\w]*`) | ||
|
||
func (e *EventType) Parse(raw string) error { | ||
if !basicEventRe.MatchString(raw) && !flowEventRe.MatchString(raw) { | ||
return fmt.Errorf("invalid event type format") | ||
} | ||
*e = EventType(raw) | ||
return nil | ||
} | ||
|
||
func (e EventType) Flow() string { | ||
return string(e) | ||
} | ||
|
||
type EventTypes []EventType | ||
|
||
func (e *EventTypes) Parse(raw []string) error { | ||
// make a map to have only unique values as keys | ||
eventTypes := make(EventTypes, 0) | ||
uniqueTypes := make(map[string]bool) | ||
for i, r := range raw { | ||
var eType EventType | ||
err := eType.Parse(r) | ||
if err != nil { | ||
return fmt.Errorf("error at index %d: %w", i, err) | ||
} | ||
|
||
if !uniqueTypes[eType.Flow()] { | ||
uniqueTypes[eType.Flow()] = true | ||
eventTypes = append(eventTypes, eType) | ||
} | ||
} | ||
|
||
*e = eventTypes | ||
return nil | ||
} | ||
|
||
func (e EventTypes) Flow() []string { | ||
eventTypes := make([]string, len(e)) | ||
for j, eType := range e { | ||
eventTypes[j] = eType.Flow() | ||
} | ||
return eventTypes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package request | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
const startBlockIdQuery = "start_block_id" | ||
const eventTypesQuery = "event_types" | ||
const addressesQuery = "addresses" | ||
const contractsQuery = "contracts" | ||
|
||
type SubscribeEvents struct { | ||
StartBlockID flow.Identifier | ||
StartHeight uint64 | ||
|
||
EventTypes []string | ||
Addresses []string | ||
Contracts []string | ||
} | ||
|
||
func (g *SubscribeEvents) Build(r *Request) error { | ||
return g.Parse( | ||
r.GetQueryParam(startBlockIdQuery), | ||
r.GetQueryParam(startHeightQuery), | ||
r.GetQueryParams(eventTypesQuery), | ||
r.GetQueryParams(addressesQuery), | ||
r.GetQueryParams(contractsQuery), | ||
) | ||
} | ||
|
||
func (g *SubscribeEvents) Parse(rawStartBlockID string, rawStartHeight string, rawTypes []string, rawAddresses []string, rawContracts []string) error { | ||
var startBlockID ID | ||
err := startBlockID.Parse(rawStartBlockID) | ||
if err != nil { | ||
return err | ||
} | ||
g.StartBlockID = startBlockID.Flow() | ||
|
||
var height Height | ||
err = height.Parse(rawStartHeight) | ||
if err != nil { | ||
return fmt.Errorf("invalid start height: %w", err) | ||
} | ||
g.StartHeight = height.Flow() | ||
|
||
// if both start_block_id and start_height are provided | ||
if g.StartBlockID != flow.ZeroID && g.StartHeight != EmptyHeight { | ||
return fmt.Errorf("can only provide either block ID or start height") | ||
} | ||
|
||
// default to root block | ||
if g.StartHeight == EmptyHeight { | ||
g.StartHeight = 0 | ||
} | ||
|
||
var eventTypes EventTypes | ||
err = eventTypes.Parse(rawTypes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
g.EventTypes = eventTypes.Flow() | ||
g.Addresses = rawAddresses | ||
g.Contracts = rawContracts | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.