GetDeviceByID handles /devices/{deviceID}
( req *http.Request, userAPI api.ClientUserAPI, device *api.Device, deviceID string, )
| 50 | |
| 51 | // GetDeviceByID handles /devices/{deviceID} |
| 52 | func GetDeviceByID( |
| 53 | req *http.Request, userAPI api.ClientUserAPI, device *api.Device, |
| 54 | deviceID string, |
| 55 | ) util.JSONResponse { |
| 56 | var queryRes api.QueryDevicesResponse |
| 57 | err := userAPI.QueryDevices(req.Context(), &api.QueryDevicesRequest{ |
| 58 | UserID: device.UserID, |
| 59 | }, &queryRes) |
| 60 | if err != nil { |
| 61 | util.GetLogger(req.Context()).WithError(err).Error("QueryDevices failed") |
| 62 | return jsonerror.InternalServerError() |
| 63 | } |
| 64 | var targetDevice *api.Device |
| 65 | for _, device := range queryRes.Devices { |
| 66 | if device.ID == deviceID { |
| 67 | targetDevice = &device |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | if targetDevice == nil { |
| 72 | return util.JSONResponse{ |
| 73 | Code: http.StatusNotFound, |
| 74 | JSON: jsonerror.NotFound("Unknown device"), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return util.JSONResponse{ |
| 79 | Code: http.StatusOK, |
| 80 | JSON: deviceJSON{ |
| 81 | DeviceID: targetDevice.ID, |
| 82 | DisplayName: targetDevice.DisplayName, |
| 83 | LastSeenIP: stripIPPort(targetDevice.LastSeenIP), |
| 84 | LastSeenTS: targetDevice.LastSeenTS, |
| 85 | }, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // GetDevicesByLocalpart handles /devices |
| 90 | func GetDevicesByLocalpart( |
no test coverage detected