Skip to content

Commit

Permalink
Openapp refactor (#483)
Browse files Browse the repository at this point in the history
* Remove function connection to `OpenAppSettings` for functions not using settings. 
* Separate session object creation etc into a `DoPostOpenAppRequests` function
  • Loading branch information
DnlLrssn authored May 30, 2024
1 parent 6d156af commit 2bc578e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 91 deletions.
2 changes: 1 addition & 1 deletion scenario/connfunchandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func defaultGuidWsConnectTest(connectionSettings *connection.ConnectionSettings,
return errors.Wrapf(err, "failed to get connect function")
}

connectWs := OpenAppSettings{}.GetConnectWsAction("", connectFunc)
connectWs := GetConnectWsAction("", connectFunc)
if err := connectWs.Execute(sessionState, connectionSettings); err != nil {
return errors.Wrap(err, "failed to connect to engine over web socket")
}
Expand Down
185 changes: 95 additions & 90 deletions scenario/openapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (openApp OpenAppSettings) Execute(sessionState *session.State, actionState
wsLabel = fmt.Sprintf("%s - WS", label)
}

connectWs := openApp.GetConnectWsAction(wsLabel, connectFunc)
connectWs := GetConnectWsAction(wsLabel, connectFunc)

//Connect websocket and logs as separate action
actionState.NoResults = true // temporary set to not report while doing sub action.
Expand All @@ -101,97 +101,12 @@ func (openApp OpenAppSettings) Execute(sessionState *session.State, actionState

uplink := sessionState.Connection.Sense()

openApp.doOpen(sessionState, actionState, uplink, appEntry.ID)
DoOpenApp(sessionState, actionState, uplink, appEntry.ID)
if actionState.Failed {
return
}

if uplink.CurrentApp == nil {
actionState.AddErrors(errors.New("No current app"))
return
}
if uplink.CurrentApp.Doc == nil {
actionState.AddErrors(errors.New("No current enigma doc"))
return
}

doc := uplink.CurrentApp.Doc

var authUser string
// Ask for user synchronously to make sure it's on all subsequent log entries
getAuthUser := func(ctx context.Context) error {
var err error
authUser, err = uplink.Global.GetAuthenticatedUser(ctx)
return err
}
if err := sessionState.SendRequest(actionState, getAuthUser); err != nil {
actionState.AddErrors(err)
return
}
sessionState.LogEntry.LogInfo("AuthenticatedUser", authUser)

// send another AuthenticatedUser for api compliance
sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.GetAuthenticatedUser(ctx)
return err
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
layout, applyOutErr := doc.GetAppLayout(ctx)
if applyOutErr != nil {
return applyOutErr
}
uplink.CurrentApp.Layout = layout
return nil
}, actionState, true, fmt.Sprintf("Failed getting app layout for app GUID<%s>", appEntry.ID))

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetVariableList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetStoryList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetAppsPropsList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.AllowCreateApp(ctx)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.Doc.GetScriptEx(ctx) // ignore err, as when not ownning app an Access denied will be returned.
if err != nil {
sessionState.LogEntry.LogDebugf("GetScriptEx request returned error: %v", err)
}
return nil
}, actionState, true, "")

for i := 0; i < 2; i++ {
sessionState.QueueRequestRaw(uplink.CurrentApp.Doc.GetAppPropertiesRaw, actionState, true, "failed to get AppProperties")
}
sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.GetBaseBNFHash(ctx, "S")
return err
}, actionState, true, "")

// Send GetConfiguration request 5 times
for i := 0; i < 5; i++ {
sessionState.QueueRequest(func(ctx context.Context) error {
return errors.WithStack(uplink.Global.RPC(ctx, "GetConfiguration", nil))
}, actionState, false, "GetConfiguration request failed")
}

sessionState.GetSheetList(actionState, uplink)
if actionState.Failed {
return
}
DoPostOpenAppRequests(sessionState, actionState, uplink, appEntry.ID)

// setup re-connect function
sessionState.SetReconnectFunc(connectFunc)
Expand All @@ -216,7 +131,7 @@ func openDoc(ctx context.Context, uplink *enigmahandlers.SenseUplink, appGUID st
return uplink.SetCurrentApp(appGUID, doc)
}

func (openApp OpenAppSettings) GetConnectWsAction(wsLabel string, connectFunc func(bool) (string, error)) Action {
func GetConnectWsAction(wsLabel string, connectFunc func(bool) (string, error)) Action {
connectWs := Action{
ActionCore{
Type: ActionConnectWs,
Expand Down Expand Up @@ -274,7 +189,8 @@ func (openApp OpenAppSettings) AffectsAppObjectsAction(structure appstructure.Ap
return []*appstructure.AppStructurePopulatedObjects{&newObjs}, nil, true
}

func (openApp OpenAppSettings) doOpen(sessionState *session.State, actionState *action.State, uplink *enigmahandlers.SenseUplink, appGUID string) {
// DoOpenApp is intended to be used from inside a open app action after websocket is connected
func DoOpenApp(sessionState *session.State, actionState *action.State, uplink *enigmahandlers.SenseUplink, appGUID string) {
if err := sessionState.SendRequest(actionState, func(ctx context.Context) error {
return openDoc(ctx, uplink, appGUID)
}); err != nil {
Expand All @@ -291,3 +207,92 @@ func (openApp OpenAppSettings) doOpen(sessionState *session.State, actionState *
return
}
}

func DoPostOpenAppRequests(sessionState *session.State, actionState *action.State, uplink *enigmahandlers.SenseUplink, appID string) {
if uplink.CurrentApp == nil {
actionState.AddErrors(errors.New("No current app"))
return
}
if uplink.CurrentApp.Doc == nil {
actionState.AddErrors(errors.New("No current enigma doc"))
return
}

doc := uplink.CurrentApp.Doc

var authUser string
// Ask for user synchronously to make sure it's on all subsequent log entries
getAuthUser := func(ctx context.Context) error {
var err error
authUser, err = uplink.Global.GetAuthenticatedUser(ctx)
return err
}
if err := sessionState.SendRequest(actionState, getAuthUser); err != nil {
actionState.AddErrors(err)
return
}
sessionState.LogEntry.LogInfo("AuthenticatedUser", authUser)

// send another AuthenticatedUser for api compliance
sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.GetAuthenticatedUser(ctx)
return err
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
layout, applyOutErr := doc.GetAppLayout(ctx)
if applyOutErr != nil {
return applyOutErr
}
uplink.CurrentApp.Layout = layout
return nil
}, actionState, true, fmt.Sprintf("Failed getting app layout for app GUID<%s>", appID))

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetVariableList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetStoryList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.GetAppsPropsList(sessionState, actionState)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.AllowCreateApp(ctx)
return errors.WithStack(err)
}, actionState, true, "")

sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.CurrentApp.Doc.GetScriptEx(ctx) // ignore err, as when not ownning app an Access denied will be returned.
if err != nil {
sessionState.LogEntry.LogDebugf("GetScriptEx request returned error: %v", err)
}
return nil
}, actionState, true, "")

for i := 0; i < 2; i++ {
sessionState.QueueRequestRaw(uplink.CurrentApp.Doc.GetAppPropertiesRaw, actionState, true, "failed to get AppProperties")
}
sessionState.QueueRequest(func(ctx context.Context) error {
_, err := uplink.Global.GetBaseBNFHash(ctx, "S")
return err
}, actionState, true, "")

// Send GetConfiguration request 5 times
for i := 0; i < 5; i++ {
sessionState.QueueRequest(func(ctx context.Context) error {
return errors.WithStack(uplink.Global.RPC(ctx, "GetConfiguration", nil))
}, actionState, false, "GetConfiguration request failed")
}

sessionState.GetSheetList(actionState, uplink)
if actionState.Failed {
return
}
}

0 comments on commit 2bc578e

Please sign in to comment.