Exemplo n.º 1
0
//Parse Get Request to extract input params.
func parseGetReq(r *http.Request) (*models.Device, error) {

	d := models.NewDevice()
	if r.Method != "GET" {
		return nil, fmt.Errorf("Only Get Request is configured right now.")
	}

	u, err := url.Parse(r.RequestURI)
	if err != nil {
		return nil, fmt.Errorf("Failed to parse url: %s", r.RequestURI)
	}
	//parse query
	q, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		return nil, fmt.Errorf("Failed to parse query in url: %s", r.RequestURI)
	}
	d.Name = q.Get("name")
	d.Desc = q.Get("desc")
	d.IpAddress = q.Get("ipaddress")
	//Device Id
	if g, err := util.Guid(); err != nil {
		return nil, err
	} else {
		d.Id = g
	}
	return d, nil
}
Exemplo n.º 2
0
//Get Devices
func (this *DeviceHandller) GetDevices(w http.ResponseWriter, r *http.Request) {
	log.Info("Getting Devices")

	d := models.NewDevice()
	d, e := parseGetReq(r)
	if e != nil {
		ProcessError(w, r, e)
		return
	}
	if len(d.IpAddress) > 0 {
		if device, err := d.GetDevicesByIp(d.IpAddress); err != nil {
			ProcessError(w, r, err)
		} else {
			ProcessResponse(w, r, device)
		}
	} else {
		if devices, err := d.GetDevices(); err != nil {
			ProcessError(w, r, err)
		} else {
			ProcessResponse(w, r, devices)
		}
	}

}