Exemple #1
0
    def _build_response(self, payload, format="json"):
        """
        build a simple response
        """
        response = ""
        callback = getattr(self.request, "clientCallback", None)

        formater = JsonFormater()

        if isinstance(payload, failure.Failure):
            print("Failure: error message:", payload.getErrorMessage(),
                  "printTraceback:")
            payload.printTraceback()
            payload = self._handle_errors(payload)
            self.request.setResponseCode(payload.responseCode)
            self.request.setHeader("Content-Type",
                                   "application/pollapli.error+json")
            payload = payload._toDict() or ''
            payload = json.dumps(payload)
        else:
            self.request.setResponseCode(self.status)
            self.request.setHeader("Content-Type", self.contentType)
            try:
                payload = formater.format(
                    payload, self.resource
                )  #,self.resource,self.rootUri or self.request.path)
            except Exception as inst:
                print("error in reponse gen", str(inst))
                traceback.print_exc(file=sys.stdout)
                payload = ""

        if callback is not None and payload is not None:
            payload = callback + "(" + payload + ")"
        response = payload or ""

        response = response.encode("utf-8")
        self.request.setHeader('Content-Encoding', 'charset=utf-8')
        log.msg("building response using payload:",
                payload,
                logLevel=logging.DEBUG)

        requestCall = getattr(self.request, "_call", None)
        self.request.notifyFinish().addErrback(self._responseFailed,
                                               requestCall)
        self._delayedRender(str(response))
 def _build_response(self,payload,format="json"):
     """
     build a simple response
     """    
     response=""
     callback=getattr(self.request,"clientCallback",None)
     
     formater=JsonFormater()
     
     if isinstance(payload, failure.Failure):
         print("Failure: error message:",payload.getErrorMessage(),"printTraceback:")
         payload.printTraceback()
         payload=self._handle_errors(payload)
         self.request.setResponseCode(payload.responseCode)
         self.request.setHeader("Content-Type", "application/pollapli.error+json")
         payload=payload._toDict() or ''
         payload=json.dumps(payload)
     else:
         self.request.setResponseCode(self.status)
         self.request.setHeader("Content-Type", self.contentType)       
         try:
             payload=formater.format(payload,self.resource)#,self.resource,self.rootUri or self.request.path)
         except Exception as inst:
             print("error in reponse gen",str(inst))
             traceback.print_exc(file=sys.stdout)
             payload=""  
         
     if callback is not None and payload is not None:
         payload= callback+"("+payload+")" 
     response=payload or ""
     
     response=response.encode("utf-8")  
     self.request.setHeader('Content-Encoding', 'charset=utf-8')
     log.msg("building response using payload:",payload,logLevel=logging.DEBUG)
     
     requestCall=getattr(self.request,"_call",None)
     self.request.notifyFinish().addErrback(self._responseFailed, requestCall)
     self._delayedRender(str(response))
Exemple #3
0
 def formatter_tests(self):
     formater=JsonFormater(resource="Tutu",rootUri="http://localhost",ignoredAttrs=["blah","blob"],addedAttrs={"gateaux":75},list=False)
     class subThing(object):
        EXPOSE=["strengh","width","height"]
        def __init__(self,strengh="Max",width=10,height=7):
            self.strengh=strengh
            self.width=width
            self.height=height
     class Thing(object):
         #EXPOSE=["subThings"]
         def __init__(self):
             self.subThings=[]
             self.subThings.append(subThing("truc")) 
             self.subThings.append(subThing("muche")) 
     class OtherThing(object): 
         EXPOSE=["var","subThing1","subThing2"]
         def __init__(self,var="somevalue"):
             self.var=var    
             self.subThing1=subThing()
             self.subThing2=subThing("min")
             
     class Tutu(object):
        id=0
        EXPOSE=["id","thing.subThings"]
        def __init__(self,blob="blob",blib=42,blah="nii"):
            self.blob=blob
            self.blib=blib
            self.blah=blah
            self.id=Tutu.id
            self.name="truc"
            self.thing=Thing()
            Tutu.id+=1
            
     class ThingWithId(object):
         id=0
         EXPOSE=["id","thingy"]
         def __init__(self,thingy="thing"):
             self.id=ThingWithId.id
             ThingWithId.id+=1
             self.thingy=thingy
     print("single item: ",formater.format(OtherThing(),"Otherthing","http://localhost/otherthing"))
     print("single item: ",formater.format(Tutu(),"tutu","http://localhost/tutu"))
     print("multiple items: ",formater.format([subThing("min",0.5,3),subThing()],"kpouer","http://localhost/kpouer"))
     print("multiple items: ",formater.format([ThingWithId(),ThingWithId()],"wobbly","http://localhost/wobbly"))