func (self WritableCatalogAPI) Update(w http.ResponseWriter, req *http.Request) { params := mux.Vars(req) id := fmt.Sprintf("%v/%v", params["hostid"], params["regid"]) body, err := ioutil.ReadAll(req.Body) req.Body.Close() var s Service err = json.Unmarshal(body, &s) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "Error processing the request:: %s\n", err.Error()) return } err = self.catalogStorage.update(id, s) if err == ErrorNotFound { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Service not found\n") return } else if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error requesting the service: %s\n", err.Error()) return } w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion) w.WriteHeader(http.StatusOK) return }
func (self ReadableCatalogAPI) GetResource(w http.ResponseWriter, req *http.Request) { params := mux.Vars(req) devid := fmt.Sprintf("%v/%v", params["dgwid"], params["regid"]) resid := fmt.Sprintf("%v/%v", devid, params["resname"]) // check if device devid exists _, err := self.catalogStorage.get(devid) if err == ErrorNotFound { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Registration not found\n") return } else if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error requesting the device: %s\n", err.Error()) return } // check if it has a resource resid res, err := self.catalogStorage.getResourceById(resid) if err == ErrorNotFound { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Registration not found\n") return } else if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error requesting the resource: %s\n", err.Error()) return } b, _ := json.Marshal(res.ldify(self.apiLocation)) w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion) w.Write(b) return }
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 }
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) }
func (self WritableCatalogAPI) Delete(w http.ResponseWriter, req *http.Request) { params := mux.Vars(req) id := fmt.Sprintf("%v/%v", params["hostid"], params["regid"]) err := self.catalogStorage.delete(id) if err == ErrorNotFound { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Not found\n") return } else if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error deleting the device: %s\n", err.Error()) return } w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion) w.WriteHeader(http.StatusOK) return }
func (self ReadableCatalogAPI) Get(w http.ResponseWriter, req *http.Request) { params := mux.Vars(req) id := fmt.Sprintf("%v/%v", params["hostid"], params["regid"]) r, err := self.catalogStorage.get(id) if err == ErrorNotFound { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Service not found\n") return } else if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error requesting the service: %s\n", err.Error()) return } b, _ := json.Marshal(r.ldify(self.apiLocation)) w.Header().Set("Content-Type", "application/ld+json;version="+ApiVersion) w.Write(b) return }