mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 22:44:05 +08:00
refactor: move shoots related server to ext
This commit is contained in:
@@ -13,7 +13,7 @@ var serverCmd = &cobra.Command{
|
|||||||
Long: `start hrp server, call httprunner by HTTP`,
|
Long: `start hrp server, call httprunner by HTTP`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return server.NewServer(port)
|
return server.NewRouter().Run(port)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v5.0.0+2502101335
|
v5.0.0+2502101959
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func foregroundAppHandler(c *gin.Context) {
|
func foregroundAppHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
appInfo, err := dExt.Driver.GetForegroundApp()
|
appInfo, err := dExt.GetDriver().GetForegroundApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -31,7 +31,7 @@ func foregroundAppHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func clearAppHandler(c *gin.Context) {
|
func clearAppHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ func clearAppHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.Clear(appClearReq.PackageName)
|
err = dExt.GetDriver().Clear(appClearReq.PackageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -58,7 +58,7 @@ func clearAppHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func launchAppHandler(c *gin.Context) {
|
func launchAppHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ func launchAppHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.AppLaunch(appLaunchReq.PackageName)
|
err = dExt.GetDriver().AppLaunch(appLaunchReq.PackageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appLaunchReq.PackageName))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appLaunchReq.PackageName))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -85,7 +85,7 @@ func launchAppHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func terminalAppHandler(c *gin.Context) {
|
func terminalAppHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -96,7 +96,7 @@ func terminalAppHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
success, err := dExt.Driver.AppTerminate(appTerminalReq.PackageName)
|
success, err := dExt.GetDriver().AppTerminate(appTerminalReq.PackageName)
|
||||||
if !success {
|
if !success {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appTerminalReq.PackageName))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appTerminalReq.PackageName))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
var uiClients = make(map[string]uixt.IDriverExt) // UI automation clients for iOS and Android, key is udid/serial
|
var uiClients = make(map[string]uixt.IDriverExt) // UI automation clients for iOS and Android, key is udid/serial
|
||||||
|
|
||||||
func handleDeviceContext() gin.HandlerFunc {
|
func (r *Router) HandleDeviceContext() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
platform := c.Param("platform")
|
platform := c.Param("platform")
|
||||||
serial := c.Param("serial")
|
serial := c.Param("serial")
|
||||||
@@ -81,7 +81,7 @@ func handleDeviceContext() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getContextDriver(c *gin.Context) (*uixt.DriverExt, error) {
|
func GetContextDriver(c *gin.Context) (uixt.IDriverExt, error) {
|
||||||
driverObj, exists := c.Get("driver")
|
driverObj, exists := c.Get("driver")
|
||||||
if !exists {
|
if !exists {
|
||||||
handlerInitDeviceDriverFailedContext(c)
|
handlerInitDeviceDriverFailedContext(c)
|
||||||
|
|||||||
@@ -6,10 +6,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v5/code"
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
"github.com/httprunner/httprunner/v5/pkg/gadb"
|
"github.com/httprunner/httprunner/v5/pkg/gadb"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func listDeviceHandler(c *gin.Context) {
|
func listDeviceHandler(c *gin.Context) {
|
||||||
@@ -92,57 +91,3 @@ func listDeviceHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginHandler(c *gin.Context) {
|
|
||||||
dExt, err := getContextDriver(c)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var loginReq LoginRequest
|
|
||||||
if err := c.ShouldBindJSON(&loginReq); err != nil {
|
|
||||||
handlerValidateRequestFailedContext(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
info, err := dExt.Driver.LoginNoneUI(loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", 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", Result: info})
|
|
||||||
}
|
|
||||||
|
|
||||||
func logoutHandler(c *gin.Context) {
|
|
||||||
dExt, err := getContextDriver(c)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var logoutReq LogoutRequest
|
|
||||||
if err := c.ShouldBindJSON(&logoutReq); err != nil {
|
|
||||||
handlerValidateRequestFailedContext(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = dExt.Driver.LogoutNoneUI(logoutReq.PackageName)
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", 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"})
|
|
||||||
}
|
|
||||||
242
server/ext/shoots.go
Normal file
242
server/ext/shoots.go
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
package server_ext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
|
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||||
|
"github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext"
|
||||||
|
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||||
|
"github.com/httprunner/httprunner/v5/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewExtServer(port int) error {
|
||||||
|
router := server.NewRouter()
|
||||||
|
apiV1PlatformSerial := router.Group("/api/v1").Group("/:platform").Group("/:serial")
|
||||||
|
|
||||||
|
// shoots operations
|
||||||
|
apiV1PlatformSerial.GET("/shoots/source", handleDeviceContext(), sourceHandler)
|
||||||
|
apiV1PlatformSerial.POST("/shoots/login", handleDeviceContext(), loginHandler)
|
||||||
|
apiV1PlatformSerial.POST("/shoots/logout", handleDeviceContext(), logoutHandler)
|
||||||
|
|
||||||
|
err := router.Engine.Run(fmt.Sprintf("127.0.0.1:%d", port))
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("failed to start http server")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sourceHandler(c *gin.Context) {
|
||||||
|
dExt, err := getContextDriver(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
app, err := dExt.GetForegroundApp()
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get foreground app", c.HandlerName()))
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
source, err := dExt.Source(option.NewSourceOption().WithProcessName(app.PackageName))
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get source %s", c.HandlerName(), app.PackageName))
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, server.HttpResponse{Result: source})
|
||||||
|
}
|
||||||
|
|
||||||
|
func loginHandler(c *gin.Context) {
|
||||||
|
dExt, err := getContextDriver(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var loginReq LoginRequest
|
||||||
|
if err := c.ShouldBindJSON(&loginReq); err != nil {
|
||||||
|
log.Error().Err(err).Msg("validate request failed")
|
||||||
|
c.JSON(http.StatusBadRequest, server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(code.InvalidParamError),
|
||||||
|
Message: fmt.Sprintf("validate request param failed: %s", err.Error()),
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var info driver_ext.AppLoginInfo
|
||||||
|
platform := c.Param("platform")
|
||||||
|
if platform == "android" {
|
||||||
|
info, err = dExt.(*driver_ext.ShootsAndroidDriver).LoginNoneUI(
|
||||||
|
loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
|
||||||
|
} else {
|
||||||
|
// ios
|
||||||
|
info, err = dExt.(*driver_ext.ShootsIOSDriver).LoginNoneUI(
|
||||||
|
loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, server.HttpResponse{Code: 0, Message: "success", Result: info})
|
||||||
|
}
|
||||||
|
|
||||||
|
func logoutHandler(c *gin.Context) {
|
||||||
|
dExt, err := getContextDriver(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var logoutReq LogoutRequest
|
||||||
|
if err := c.ShouldBindJSON(&logoutReq); err != nil {
|
||||||
|
log.Error().Err(err).Msg("validate request failed")
|
||||||
|
c.JSON(http.StatusBadRequest, server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(code.InvalidParamError),
|
||||||
|
Message: fmt.Sprintf("validate request param failed: %s", err.Error()),
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
platform := c.Param("platform")
|
||||||
|
if platform == "android" {
|
||||||
|
err = dExt.(*driver_ext.ShootsAndroidDriver).LogoutNoneUI(logoutReq.PackageName)
|
||||||
|
} else {
|
||||||
|
// ios
|
||||||
|
err = dExt.(*driver_ext.ShootsIOSDriver).LogoutNoneUI(logoutReq.PackageName)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, server.HttpResponse{Code: 0, Message: "success"})
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginRequest struct {
|
||||||
|
PackageName string `json:"packageName"`
|
||||||
|
PhoneNumber string `json:"phoneNumber"`
|
||||||
|
Captcha string `json:"captcha"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogoutRequest struct {
|
||||||
|
PackageName string `json:"packageName"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var uiClients = make(map[string]uixt.IDriver) // UI automation clients for iOS and Android, key is udid/serial
|
||||||
|
|
||||||
|
func handleDeviceContext() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
platform := c.Param("platform")
|
||||||
|
serial := c.Param("serial")
|
||||||
|
if serial == "" {
|
||||||
|
log.Error().Str("platform", platform).Msg(fmt.Sprintf("[%s]: serial is empty", c.HandlerName()))
|
||||||
|
c.JSON(http.StatusBadRequest, server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(code.InvalidParamError),
|
||||||
|
Message: "serial is empty",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// get cached driver
|
||||||
|
if driver, ok := uiClients[serial]; ok {
|
||||||
|
c.Set("driver", driver)
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch strings.ToLower(platform) {
|
||||||
|
case "android":
|
||||||
|
device, err := uixt.NewAndroidDevice(
|
||||||
|
option.WithSerialNumber(serial))
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("platform", platform).Str("serial", serial).
|
||||||
|
Msg("android device not found")
|
||||||
|
c.JSON(http.StatusBadRequest,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
device.Setup()
|
||||||
|
|
||||||
|
driver, err := driver_ext.NewShootsAndroidDriver(device)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("platform", platform).Str("serial", serial).
|
||||||
|
Msg("failed to init shoots android driver")
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(err),
|
||||||
|
Message: err.Error(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Set("driver", driver)
|
||||||
|
// cache driver
|
||||||
|
uiClients[serial] = driver
|
||||||
|
default:
|
||||||
|
c.JSON(http.StatusBadRequest, server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(code.InvalidParamError),
|
||||||
|
Message: fmt.Sprintf("unsupported platform %s", platform),
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getContextDriver(c *gin.Context) (uixt.IDriver, error) {
|
||||||
|
driverObj, exists := c.Get("driver")
|
||||||
|
if !exists {
|
||||||
|
log.Error().Msg("init device driver failed")
|
||||||
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
server.HttpResponse{
|
||||||
|
Code: code.GetErrorCode(code.MobileUIDriverError),
|
||||||
|
Message: "init driver failed",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
c.Abort()
|
||||||
|
return nil, fmt.Errorf("driver not found")
|
||||||
|
}
|
||||||
|
dExt := driverObj.(uixt.IDriver)
|
||||||
|
return dExt, nil
|
||||||
|
}
|
||||||
@@ -11,12 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func unlockHandler(c *gin.Context) {
|
func unlockHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.Unlock()
|
err = dExt.GetDriver().Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -32,12 +32,12 @@ func unlockHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func homeHandler(c *gin.Context) {
|
func homeHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.Homescreen()
|
err = dExt.GetDriver().Homescreen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to enter homescreen", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to enter homescreen", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -53,7 +53,7 @@ func homeHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func keycodeHandler(c *gin.Context) {
|
func keycodeHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ func keycodeHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.PressKeyCode(uixt.KeyCode(keycodeReq.Keycode))
|
err = dExt.GetDriver().PressKeyCode(uixt.KeyCode(keycodeReq.Keycode))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input keycode %d", c.HandlerName(), keycodeReq.Keycode))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input keycode %d", c.HandlerName(), keycodeReq.Keycode))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
|||||||
@@ -8,41 +8,48 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewServer(port int) error {
|
func NewRouter() *Router {
|
||||||
router := gin.Default()
|
router := &Router{}
|
||||||
router.GET("/ping", pingHandler)
|
router.Init()
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
|
||||||
apiV1Platform := router.Group("/api/v1").Group("/:platform")
|
type Router struct {
|
||||||
|
*gin.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) Init() {
|
||||||
|
r.Engine = gin.Default()
|
||||||
|
r.Engine.GET("/ping", pingHandler)
|
||||||
|
|
||||||
|
apiV1Platform := r.Engine.Group("/api/v1").Group("/:platform")
|
||||||
apiV1Platform.GET("/devices", listDeviceHandler)
|
apiV1Platform.GET("/devices", listDeviceHandler)
|
||||||
|
|
||||||
apiV1PlatformSerial := apiV1Platform.Group("/:serial")
|
apiV1PlatformSerial := apiV1Platform.Group("/:serial")
|
||||||
// UI operations
|
// UI operations
|
||||||
apiV1PlatformSerial.POST("/ui/tap", handleDeviceContext(), tapHandler)
|
apiV1PlatformSerial.POST("/ui/tap", r.HandleDeviceContext(), tapHandler)
|
||||||
apiV1PlatformSerial.POST("/ui/drag", handleDeviceContext(), dragHandler)
|
apiV1PlatformSerial.POST("/ui/drag", r.HandleDeviceContext(), dragHandler)
|
||||||
apiV1PlatformSerial.POST("/ui/input", handleDeviceContext(), inputHandler)
|
apiV1PlatformSerial.POST("/ui/input", r.HandleDeviceContext(), inputHandler)
|
||||||
// Key operations
|
// Key operations
|
||||||
apiV1PlatformSerial.POST("/key/unlock", handleDeviceContext(), unlockHandler)
|
apiV1PlatformSerial.POST("/key/unlock", r.HandleDeviceContext(), unlockHandler)
|
||||||
apiV1PlatformSerial.POST("/key/home", handleDeviceContext(), homeHandler)
|
apiV1PlatformSerial.POST("/key/home", r.HandleDeviceContext(), homeHandler)
|
||||||
apiV1PlatformSerial.POST("/key", handleDeviceContext(), keycodeHandler)
|
apiV1PlatformSerial.POST("/key", r.HandleDeviceContext(), keycodeHandler)
|
||||||
// App operations
|
// App operations
|
||||||
apiV1PlatformSerial.GET("/app/foreground", handleDeviceContext(), foregroundAppHandler)
|
apiV1PlatformSerial.GET("/app/foreground", r.HandleDeviceContext(), foregroundAppHandler)
|
||||||
apiV1PlatformSerial.POST("/app/clear", handleDeviceContext(), clearAppHandler)
|
apiV1PlatformSerial.POST("/app/clear", r.HandleDeviceContext(), clearAppHandler)
|
||||||
apiV1PlatformSerial.POST("/app/launch", handleDeviceContext(), launchAppHandler)
|
apiV1PlatformSerial.POST("/app/launch", r.HandleDeviceContext(), launchAppHandler)
|
||||||
apiV1PlatformSerial.POST("/app/terminal", handleDeviceContext(), terminalAppHandler)
|
apiV1PlatformSerial.POST("/app/terminal", r.HandleDeviceContext(), terminalAppHandler)
|
||||||
// get screen info
|
// get screen info
|
||||||
apiV1PlatformSerial.GET("/screenshot", handleDeviceContext(), screenshotHandler)
|
apiV1PlatformSerial.GET("/screenshot", r.HandleDeviceContext(), screenshotHandler)
|
||||||
apiV1PlatformSerial.POST("/screenresult", handleDeviceContext(), screenResultHandler)
|
apiV1PlatformSerial.POST("/screenresult", r.HandleDeviceContext(), screenResultHandler)
|
||||||
apiV1PlatformSerial.GET("/shoots/source", handleDeviceContext(), sourceHandler)
|
apiV1PlatformSerial.GET("/adb/source", r.HandleDeviceContext(), adbSourceHandler)
|
||||||
apiV1PlatformSerial.GET("/adb/source", handleDeviceContext(), adbSourceHandler)
|
|
||||||
// shoots operations
|
|
||||||
apiV1PlatformSerial.POST("/shoots/login", handleDeviceContext(), loginHandler)
|
|
||||||
apiV1PlatformSerial.POST("/shoots/logout", handleDeviceContext(), logoutHandler)
|
|
||||||
|
|
||||||
// run uixt actions
|
// run uixt actions
|
||||||
apiV1PlatformSerial.POST("/uixt/action", handleDeviceContext(), uixtActionHandler)
|
apiV1PlatformSerial.POST("/uixt/action", r.HandleDeviceContext(), uixtActionHandler)
|
||||||
apiV1PlatformSerial.POST("/uixt/actions", handleDeviceContext(), uixtActionsHandler)
|
apiV1PlatformSerial.POST("/uixt/actions", r.HandleDeviceContext(), uixtActionsHandler)
|
||||||
|
}
|
||||||
|
|
||||||
err := router.Run(fmt.Sprintf("127.0.0.1:%d", port))
|
func (r *Router) Run(port int) error {
|
||||||
|
err := r.Engine.Run(fmt.Sprintf("127.0.0.1:%d", port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to start http server")
|
log.Err(err).Msg("failed to start http server")
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -51,14 +51,3 @@ type AppLaunchRequest struct {
|
|||||||
type AppTerminalRequest struct {
|
type AppTerminalRequest struct {
|
||||||
PackageName string `json:"packageName"`
|
PackageName string `json:"packageName"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginRequest struct {
|
|
||||||
PackageName string `json:"packageName"`
|
|
||||||
PhoneNumber string `json:"phoneNumber"`
|
|
||||||
Captcha string `json:"captcha"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogoutRequest struct {
|
|
||||||
PackageName string `json:"packageName"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func screenshotHandler(c *gin.Context) {
|
func screenshotHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
raw, err := dExt.Driver.Screenshot()
|
raw, err := dExt.GetDriver().Screenshot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get screenshot", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get screenshot", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
@@ -41,7 +41,7 @@ func screenshotHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func screenResultHandler(c *gin.Context) {
|
func screenResultHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -78,47 +78,13 @@ func screenResultHandler(c *gin.Context) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sourceHandler(c *gin.Context) {
|
|
||||||
dExt, err := getContextDriver(c)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
app, err := dExt.Driver.GetForegroundApp()
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get foreground app", c.HandlerName()))
|
|
||||||
c.JSON(http.StatusInternalServerError,
|
|
||||||
HttpResponse{
|
|
||||||
Code: code.GetErrorCode(err),
|
|
||||||
Message: err.Error(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
c.Abort()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
source, err := dExt.Driver.Source(option.NewSourceOption().WithProcessName(app.PackageName))
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get source %s", c.HandlerName(), app.PackageName))
|
|
||||||
c.JSON(http.StatusInternalServerError,
|
|
||||||
HttpResponse{
|
|
||||||
Code: code.GetErrorCode(err),
|
|
||||||
Message: err.Error(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
c.Abort()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, HttpResponse{Result: source})
|
|
||||||
}
|
|
||||||
|
|
||||||
func adbSourceHandler(c *gin.Context) {
|
func adbSourceHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
source, err := dExt.Driver.Source()
|
source, err := dExt.GetDriver().Source()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName()))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName()))
|
||||||
c.JSON(http.StatusInternalServerError,
|
c.JSON(http.StatusInternalServerError,
|
||||||
|
|||||||
10
server/ui.go
10
server/ui.go
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func tapHandler(c *gin.Context) {
|
func tapHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ func tapHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dragHandler(c *gin.Context) {
|
func dragHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ func dragHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := dExt.Driver.Swipe(
|
err := dExt.GetDriver().Swipe(
|
||||||
dragReq.FromX, dragReq.FromY, dragReq.ToX, dragReq.ToY,
|
dragReq.FromX, dragReq.FromY, dragReq.ToX, dragReq.ToY,
|
||||||
actionOptions...)
|
actionOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -129,7 +129,7 @@ func dragHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func inputHandler(c *gin.Context) {
|
func inputHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ func inputHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dExt.Driver.SendKeys(inputReq.Text,
|
err = dExt.GetDriver().SendKeys(inputReq.Text,
|
||||||
option.WithFrequency(inputReq.Frequency))
|
option.WithFrequency(inputReq.Frequency))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input text %s", c.HandlerName(), inputReq.Text))
|
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input text %s", c.HandlerName(), inputReq.Text))
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
// exec a single uixt action
|
// exec a single uixt action
|
||||||
func uixtActionHandler(c *gin.Context) {
|
func uixtActionHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ func uixtActionHandler(c *gin.Context) {
|
|||||||
|
|
||||||
// exec multiple uixt actions
|
// exec multiple uixt actions
|
||||||
func uixtActionsHandler(c *gin.Context) {
|
func uixtActionsHandler(c *gin.Context) {
|
||||||
dExt, err := getContextDriver(c)
|
dExt, err := GetContextDriver(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user