def _processPOSTRequest(self, content): try: o = self._getJSONContent(content) except Exception as ex: self._logRefused('BAD REQUEST, %s' % ex) self._sendHTTPResponse(400, '400 : Bad Request (%s)' % ex) return try: uid = IoTSocketStruct.UIDToBin128(o['UID']) timeout = int(o.get('Timeout', self._maxSecWaitResponse)) fmt, data = IoTSocketStruct.EncodeJSONPayload( o['Payload'], o['Format']) if uid and timeout > 0 and fmt is not None and data is not None: if timeout > self._maxSecWaitResponse: timeout = self._maxSecWaitResponse exp = time() + timeout self._trackingNbr = self._router.AddCentralHTTPRequest( self, exp) strUID = ('{%s}' % IoTSocketStruct.UIDFromBin128(uid)) self._log('REQUEST TO %s RECEIVED (#%s)' % (strUID, self._trackingNbr)) if not self._router.RouteRequest( fromUID=None, toUID=uid, trackingNbr=self._trackingNbr, dataFormat=fmt, formatOpt=IoTSocketStruct.PLDATA_FMT_OPT_NONE, data=data): self._router.RemoveCentralHTTPRequest(self) self.SendResponseErrNoDest() return except: pass self._logRefused('BAD REQUEST, INCORRECT JSON DATA') self._sendHTTPResponse(400, '400 : Bad Request (incorrect json data)')
def _onWebHookResponseOk(self, centralHTTPWebHook, o): if o: uid, trackingNbr = centralHTTPWebHook.ObjRef try: code = int(o['Code']) fmt, data = IoTSocketStruct.EncodeJSONPayload( o['Payload'], o['Format']) if fmt is not None and data is not None: centralHTTPWebHook.ObjRef = (None, None) session = self._objectsSessions.get(uid, None) if session: session.EndTrackingRequest(trackingNbr) data = IoTSocketStruct.MakeResponseTRHdr( None, trackingNbr, code, fmt, IoTSocketStruct.PLDATA_FMT_OPT_NONE, len(data) ) \ + data session.Send(data) except: pass