Skip to content

Commit bb15176

Browse files
committed
feat: adding apps_list and apps_terminate
1 parent 5163c8a commit bb15176

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

docs/jsonrpc_README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ This section documents the JSON-RPC methods registered by the server and shows e
144144
- `deviceId` (string)
145145
- `bundleId` (string) - required
146146

147+
- `apps_terminate`
148+
- Description: Terminate a running app on a device.
149+
- Params: object
150+
- `deviceId` (string)
151+
- `bundleId` (string) - required
152+
153+
- `apps_list`
154+
- Description: List installed apps on a device.
155+
- Params: object (optional)
156+
- `deviceId` (string)
157+
147158
Common notes:
148159
- For most methods `deviceId` is optional; when omitted the server auto-selects a single online device or returns an error when multiple devices are available.
149160
- Methods that interact with the UI/agent (`io_*`, `dump_ui`, `apps_launch`, `device_info`, etc.) call `StartAgent` which may start/forward WDA for iOS devices. If WDA is unresponsive the server will attempt to relaunch it.
@@ -245,3 +256,19 @@ curl -s -X POST http://localhost:12000/rpc \
245256
-H 'Content-Type: application/json' \
246257
-d '{"jsonrpc":"2.0","method":"apps_launch","params":{"deviceId":"<id>","bundleId":"com.example.app"},"id":20}'
247258
```
259+
260+
- Terminate app:
261+
262+
```bash
263+
curl -s -X POST http://localhost:12000/rpc \
264+
-H 'Content-Type: application/json' \
265+
-d '{"jsonrpc":"2.0","method":"apps_terminate","params":{"deviceId":"<id>","bundleId":"com.example.app"},"id":21}'
266+
```
267+
268+
- List apps:
269+
270+
```bash
271+
curl -s -X POST http://localhost:12000/rpc \
272+
-H 'Content-Type: application/json' \
273+
-d '{"jsonrpc":"2.0","method":"apps_list","params":{"deviceId":"<id>"},"id":22}'
274+
```

server/server.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ func handleJSONRPC(w http.ResponseWriter, r *http.Request) {
188188
result, err = handleDumpUI(req.Params)
189189
case "apps_launch":
190190
result, err = handleAppsLaunch(req.Params)
191+
case "apps_terminate":
192+
result, err = handleAppsTerminate(req.Params)
193+
case "apps_list":
194+
result, err = handleAppsList(req.Params)
191195
case "":
192196
err = fmt.Errorf("'method' is required")
193197

@@ -469,6 +473,15 @@ type AppsLaunchParams struct {
469473
BundleID string `json:"bundleId"`
470474
}
471475

476+
type AppsTerminateParams struct {
477+
DeviceID string `json:"deviceId"`
478+
BundleID string `json:"bundleId"`
479+
}
480+
481+
type AppsListParams struct {
482+
DeviceID string `json:"deviceId"`
483+
}
484+
472485
func handleIoButton(params json.RawMessage) (interface{}, error) {
473486
if len(params) == 0 {
474487
return nil, fmt.Errorf("'params' is required with fields: deviceId, button")
@@ -726,6 +739,49 @@ func handleAppsLaunch(params json.RawMessage) (interface{}, error) {
726739
return response.Data, nil
727740
}
728741

742+
func handleAppsTerminate(params json.RawMessage) (interface{}, error) {
743+
if len(params) == 0 {
744+
return nil, fmt.Errorf("'params' is required with fields: deviceId, bundleId")
745+
}
746+
747+
var appsTerminateParams AppsTerminateParams
748+
if err := json.Unmarshal(params, &appsTerminateParams); err != nil {
749+
return nil, fmt.Errorf("invalid parameters: %v. Expected fields: deviceId, bundleId", err)
750+
}
751+
752+
req := commands.AppRequest{
753+
DeviceID: appsTerminateParams.DeviceID,
754+
BundleID: appsTerminateParams.BundleID,
755+
}
756+
757+
response := commands.TerminateAppCommand(req)
758+
if response.Status == "error" {
759+
return nil, fmt.Errorf("%s", response.Error)
760+
}
761+
762+
return response.Data, nil
763+
}
764+
765+
func handleAppsList(params json.RawMessage) (interface{}, error) {
766+
var appsListParams AppsListParams
767+
if len(params) > 0 {
768+
if err := json.Unmarshal(params, &appsListParams); err != nil {
769+
return nil, fmt.Errorf("invalid parameters: %v. Expected fields: deviceId (optional)", err)
770+
}
771+
}
772+
773+
req := commands.ListAppsRequest{
774+
DeviceID: appsListParams.DeviceID,
775+
}
776+
777+
response := commands.ListAppsCommand(req)
778+
if response.Status == "error" {
779+
return nil, fmt.Errorf("%s", response.Error)
780+
}
781+
782+
return response.Data, nil
783+
}
784+
729785
func sendJSONRPCError(w http.ResponseWriter, id interface{}, code int, message string, data interface{}) {
730786
response := JSONRPCResponse{
731787
JSONRPC: "2.0",

0 commit comments

Comments
 (0)