Exemplo n.º 1
0
func (self ReadableCatalogAPI) Get(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	page, _ := strconv.Atoi(req.Form.Get(GetParamPage))
	perPage, _ := strconv.Atoi(req.Form.Get(GetParamPerPage))
	page, perPage = catalog.ValidatePagingParams(page, perPage, MaxPerPage)

	params := mux.Vars(req)
	id := fmt.Sprintf("%v/%v", params["dgwid"], params["regid"])

	d, err := self.catalogStorage.get(id)
	if err == ErrorNotFound {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, "Device not found\n")
		return
	} else if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, "Error requesting the device: %s\n", err.Error())
		return
	}

	pd := self.paginatedDeviceFromDevice(d, page, perPage)
	b, _ := json.Marshal(pd)

	w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion)
	w.Write(b)
	return
}
Exemplo n.º 2
0
func (self ReadableCatalogAPI) List(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	page, _ := strconv.Atoi(req.Form.Get(GetParamPage))
	perPage, _ := strconv.Atoi(req.Form.Get(GetParamPerPage))
	page, perPage = catalog.ValidatePagingParams(page, perPage, MaxPerPage)

	services, total, _ := self.catalogStorage.getMany(page, perPage)
	coll := self.collectionFromServices(services, page, perPage, total)

	b, _ := json.Marshal(coll)
	w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion)
	w.Write(b)
}
Exemplo n.º 3
0
func (self ReadableCatalogAPI) Filter(w http.ResponseWriter, req *http.Request) {
	params := mux.Vars(req)
	ftype := params["type"]
	fpath := params["path"]
	fop := params["op"]
	fvalue := params["value"]

	req.ParseForm()
	page, _ := strconv.Atoi(req.Form.Get(GetParamPage))
	perPage, _ := strconv.Atoi(req.Form.Get(GetParamPerPage))
	page, perPage = catalog.ValidatePagingParams(page, perPage, MaxPerPage)

	var data interface{}
	var err error

	switch ftype {
	case FTypeService:
		data, err = self.catalogStorage.pathFilterOne(fpath, fop, fvalue)
		if data.(Service).Id != "" {
			svc := data.(Service)
			data = svc.ldify(self.apiLocation)
		} else {
			data = nil
		}

	case FTypeServices:
		var total int
		data, total, err = self.catalogStorage.pathFilter(fpath, fop, fvalue, page, perPage)
		data = self.collectionFromServices(data.([]Service), page, perPage, total)
		if data.(*Collection).Total == 0 {
			data = nil
		}
	}

	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "Error processing the request: %s\n", err.Error())
		return
	}

	if data == nil {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, "No matched entries found\n")
		return
	}

	b, _ := json.Marshal(data)
	w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion)
	w.Write(b)
}