def GET(self, id=None, *args, **kwargs): if len(args) > 1: #test = self.doQuery(self.id) #return test raise cherrypy.HTTPError(405, "Not Supported! %d " % len(args)) conceptid = unicode(str(id), "utf-8") self.id = conceptid log("============= id => %s" % self.id) if self.id == 'None': self.id = '' if self.ipDeny(): raise cherrypy.HTTPError(403, "Not Supported!") else: if len(kwargs) >= 1: for key in kwargs: log("params (%s, %s) " % (key,kwargs[key])) raise cherrypy.HTTPError(405, "Not Supported - attributes! -> %s" % self.id) else: best = accept(['application/rdf+xml', 'text/json', 'text/x-json', 'text/html', 'application/rdf', 'application/xml']) log(str(best)) if best in ['text/json', 'text/x-json']: self.format = 'json' elif best in ['text/html']: self.format = 'html' elif best in ['application/rdf', 'application/rdf+xml']: self.format = 'rdf' concept = Concept(id=self.id, format=self.format) self.getWADLDescription() return concept.result(self.descList, self.wadlDesc)
def find_handler(self, path_info): app = cherrypy.serving.request.app parentcls = super(ContentTypeDispatcher, self) try: bmimes = list(app.root.__branches__) if bmimes: if "text/html" in bmimes: # set text html at top. bmimes.insert(0, bmimes.pop(bmimes.index("text/html"))) branch_name = cptools.accept(media=bmimes) with self._branch(app.root, branch_name): return parentcls.find_handler(path_info) except AttributeError as err: cherrypy.log.error(str(err)) return parentcls.find_handler(path_info)
def _formatResponse(self, data, expires, format=None): """ data format can be anything API provides, but it will make sense to have either dict format or list of dict format. """ acchdr = request.headers.elements('Accept') if format: datatype = format elif len(acchdr) == 1 and '*/*' == str(acchdr[0]): datatype = '*/*' else: datatype = accept(self.supporttypes) return self.formatter.format(data, datatype, expires)
def format(self, data): """ Returns a string represent 'data' in one of the supported types. If the client has expressed preference, this should be honored if possible. If not possible (or there was no preference) a default type will be used. """ datatype = accept(self.supportedTypes) response.headers['Content-Type'] = datatype if datatype in ('text/json', 'text/x-json', 'application/json'): # Serialise to json return self.tojson(data) # Default... return in json anyway return self.tojson(data)
def index(self, entidad="nalada"): methods = ("OPTIONS", "GET", "POST", "PUT", "DELETE") method = cherrypy.request.method if method not in methods: raise cherrypy.HTTPError(400, "Bad Request") best = accept(["text/html", "application/json", "text/json"]) respuesta = "" if best == "text/html": respuesta = self.html(entidad) else: cherrypy.response.headers["Content-Type"] = "application/json;charset=utf-8" respuesta = self.json(entidad) print entidad return respuesta
def response(self,iData,method): # inspect Accept header for matching type dataType = accept(self.supportTypes) # get data-formatter instance form = DataCache(iData) # look-up data in appropriate format for our object data = None rType= None if dataType in ['application/xml', 'application/atom+xml']: data = form.to_xml() rType= self.xmlReturnType elif dataType in ['text/json', 'text/x-json', 'application/json']: data = form.to_json() rType= dataType elif dataType in ['text/plain']: data = form.to_txt() rType= dataType elif dataType in ['text/html']: data = form.to_html(self._host,self._url,self._mUrl,self._fUrl,self._dbs) rType= dataType else: data = iData rType= self.defaultType if method=="head": self.setHeader(rType,len(data)) return else: self.setHeader(rType) if self._verbose: self.printResponse() print "return data='%s'"%data if data: return data raise cherrypy.HTTPError(400, 'Bad Request')
def __getitem__(self, item): try: return cptools.accept(item, debug=True) except HTTPError: return False