Skip to content

Commit c985206

Browse files
authored
Add endpoints for changing device location (danielpaulus#188)
Added GenericResponse struct to have a single interface that can be used to return both errors and meaningful messages where applicable, instead of generating JSON on each spot with gin.H or whatever. If you are okay with this I can later update the existing functions Added endpoint to change the current device location by latitude and longtitude query params Added endpoint to reset the device location to the actual one Bubbled up an unhandled error in my own code for device location simulation
1 parent 797bebf commit c985206

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

ios/simlocation/simlocation.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ func SetLocationGPX(device ios.DeviceEntry, filePath string) error {
162162
pointLat := point.PointLatitude
163163

164164
// Set the current point location by its latitude and longitude
165-
SetLocation(device, pointLat, pointLon)
165+
err = SetLocation(device, pointLat, pointLon)
166+
if err != nil {
167+
return err
168+
}
166169
}
167170
}
168171
}

restapi/api/device_endpoints.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package api
22

33
import (
4+
"net/http"
5+
46
"github.com/danielpaulus/go-ios/ios"
57
"github.com/danielpaulus/go-ios/ios/instruments"
68
"github.com/danielpaulus/go-ios/ios/screenshotr"
9+
"github.com/danielpaulus/go-ios/ios/simlocation"
710
"github.com/gin-gonic/gin"
811
log "github.com/sirupsen/logrus"
9-
"net/http"
1012
)
1113

1214
// Info gets device info
@@ -64,3 +66,54 @@ func Screenshot(c *gin.Context) {
6466
c.Header("Content-Type", "image/png")
6567
c.Data(http.StatusOK, "application/octet-stream", b)
6668
}
69+
70+
// Change the current device location
71+
// @Summary Change the current device location
72+
// @Description Change the current device location to provided latitude and longtitude
73+
// @Tags general_device_specific
74+
// @Produce json
75+
// @Param latitude query string true "Location latitude"
76+
// @Param longtitude query string true "Location longtitude"
77+
// @Success 200 {object} GenericResponse
78+
// @Failure 422 {object} GenericResponse
79+
// @Failure 500 {object} GenericResponse
80+
// @Router /device/{udid}/setlocation [post]
81+
func SetLocation(c *gin.Context) {
82+
device := c.MustGet(IOS_KEY).(ios.DeviceEntry)
83+
latitude := c.Query("latitude")
84+
if latitude == "" {
85+
c.JSON(http.StatusUnprocessableEntity, GenericResponse{Error: "latitude query param is missing"})
86+
return
87+
}
88+
89+
longtitude := c.Query("longtitude")
90+
if longtitude == "" {
91+
c.JSON(http.StatusUnprocessableEntity, GenericResponse{Error: "longtitude query param is missing"})
92+
return
93+
}
94+
95+
err := simlocation.SetLocation(device, latitude, longtitude)
96+
if err != nil {
97+
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()})
98+
return
99+
}
100+
c.JSON(http.StatusOK, GenericResponse{Message: "Device location set to latitude=" + latitude + ", longtitude=" + longtitude})
101+
}
102+
103+
// Reset to the actual device location
104+
// @Summary Reset the changed device location
105+
// @Description Reset the changed device location to the actual one
106+
// @Tags general_device_specific
107+
// @Produce json
108+
// @Success 200
109+
// @Failure 500 {object} GenericResponse
110+
// @Router /device/{udid}/resetlocation [post]
111+
func ResetLocation(c *gin.Context) {
112+
device := c.MustGet(IOS_KEY).(ios.DeviceEntry)
113+
err := simlocation.ResetLocation(device)
114+
if err != nil {
115+
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()})
116+
return
117+
}
118+
c.JSON(http.StatusOK, GenericResponse{Message: "Device location reset"})
119+
}

restapi/api/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ func registerRoutes(router *gin.RouterGroup) {
88
device.Use(DeviceMiddleware())
99
device.GET("/info", Info)
1010
device.GET("/screenshot", Screenshot)
11+
device.PUT("/setlocation", SetLocation)
12+
device.POST("/resetlocation", ResetLocation)
1113

1214
initAppRoutes(device)
1315
initStreamingResponseRoutes(device, router)

restapi/api/util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ package api
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/gin-gonic/gin"
7-
"github.com/sirupsen/logrus"
86
"io/ioutil"
97
"math"
108
"net/http"
119
"os"
1210
"time"
11+
12+
"github.com/gin-gonic/gin"
13+
"github.com/sirupsen/logrus"
1314
)
1415

16+
type GenericResponse struct {
17+
Message string `json:"message,omitempty"`
18+
Error string `json:"error,omitempty"`
19+
}
20+
1521
//GetVersion reads the contents of the file version.txt and returns it.
1622
//If the file cannot be read, it returns "could not read version"
1723
func GetVersion() string {

0 commit comments

Comments
 (0)