mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-10 17:43:00 +08:00
113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/httprunner/httprunner/v5/code"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func foregroundAppHandler(c *gin.Context) {
|
|
dExt, err := getContextDriver(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
appInfo, err := dExt.Driver.GetForegroundApp()
|
|
if err != nil {
|
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
|
c.JSON(http.StatusInternalServerError,
|
|
HttpResponse{
|
|
Code: code.GetErrorCode(err),
|
|
Message: err.Error(),
|
|
},
|
|
)
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, HttpResponse{Result: appInfo})
|
|
}
|
|
|
|
func clearAppHandler(c *gin.Context) {
|
|
dExt, err := getContextDriver(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var appClearReq AppClearRequest
|
|
if err := c.ShouldBindJSON(&appClearReq); err != nil {
|
|
handlerValidateRequestFailedContext(c, err)
|
|
return
|
|
}
|
|
|
|
err = dExt.Driver.Clear(appClearReq.PackageName)
|
|
if err != nil {
|
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
|
c.JSON(http.StatusInternalServerError,
|
|
HttpResponse{
|
|
Code: code.GetErrorCode(err),
|
|
Message: err.Error(),
|
|
},
|
|
)
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success"})
|
|
}
|
|
|
|
func launchAppHandler(c *gin.Context) {
|
|
dExt, err := getContextDriver(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var appLaunchReq AppLaunchRequest
|
|
if err := c.ShouldBindJSON(&appLaunchReq); err != nil {
|
|
handlerValidateRequestFailedContext(c, err)
|
|
return
|
|
}
|
|
|
|
err = dExt.Driver.AppLaunch(appLaunchReq.PackageName)
|
|
if err != nil {
|
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appLaunchReq.PackageName))
|
|
c.JSON(http.StatusInternalServerError,
|
|
HttpResponse{
|
|
Code: code.GetErrorCode(err),
|
|
Message: err.Error(),
|
|
},
|
|
)
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success"})
|
|
}
|
|
|
|
func terminalAppHandler(c *gin.Context) {
|
|
dExt, err := getContextDriver(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var appTerminalReq AppTerminalRequest
|
|
if err := c.ShouldBindJSON(&appTerminalReq); err != nil {
|
|
handlerValidateRequestFailedContext(c, err)
|
|
return
|
|
}
|
|
|
|
success, err := dExt.Driver.AppTerminate(appTerminalReq.PackageName)
|
|
if !success {
|
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appTerminalReq.PackageName))
|
|
c.JSON(http.StatusInternalServerError,
|
|
HttpResponse{
|
|
Code: code.GetErrorCode(err),
|
|
Message: err.Error(),
|
|
},
|
|
)
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success"})
|
|
}
|