def openRequestUrl(url, params, files, requestHeaders, requestTimeout): requestHeaders['Accept'] = 'text/xml' requestHeaders['Accept-encoding'] = 'gzip' try: if not (params.get() or files): requestHeaders['Content-Type'] = 'application/json' return requests.post(url, headers=requestHeaders, timeout=requestTimeout) if files: if 'Content-Type' in requestHeaders: del requestHeaders['Content-Type'] fields = {} fields["json"] = params.toJson() fields.update(_get_file_params(files)) encoder = MultipartEncoder(fields=fields) requestHeaders['Content-Type'] = encoder.content_type return requests.post(url, headers=requestHeaders, data=encoder, timeout=requestTimeout) else: requestHeaders['Content-Type'] = 'application/json' return requests.post(url, json=params.get(), headers=requestHeaders, timeout=requestTimeout) except Exception as e: raise KalturaClientException( e, KalturaClientException.ERROR_CONNECTION_FAILED)
def parsePostResult(self, postResult): self.log("result (xml): %s" % postResult) try: resultXml = etree.fromstring(postResult) except etree.ParseError as e: raise KalturaClientException( e, KalturaClientException.ERROR_INVALID_XML) resultNode = resultXml.find('result') if resultNode is None: raise KalturaClientException( 'Could not find result node in response xml', KalturaClientException.ERROR_RESULT_NOT_FOUND) execTime = resultXml.find('executionTime') if execTime is not None: self.executionTime = getXmlNodeFloat(execTime) self.throwExceptionIfError(resultNode) return resultNode
def doQueue(self): self.responseHeaders = None self.executionTime = None if len(self.callsQueue) == 0: self.multiRequestReturnType = None return None if self.config.format != KALTURA_SERVICE_FORMAT_XML: raise KalturaClientException( "unsupported format: %s" % (postResult), KalturaClientException.ERROR_FORMAT_NOT_SUPPORTED) startTime = time.time() # get request params (url, params, files) = self.getRequestParams() # reset state self.callsQueue = [] # issue the request postResult = self.doHttpRequest(url, params, files) endTime = time.time() self.log("execution time for [%s]: [%s]" % (url, endTime - startTime)) # print server debug info to log serverName = None serverSession = None for curHeader in self.responseHeaders: if curHeader.startswith('X-Me:'): serverName = curHeader.split(':', 1)[1].strip() elif curHeader.startswith('X-Kaltura-Session:'): serverSession = curHeader.split(':', 1)[1].strip() if serverName is not None or serverSession is not None: self.log("server: [%s], session [%s]" % (serverName, serverSession)) # parse the result resultNode = self.parsePostResult(postResult) return resultNode
def readHttpResponse(r): try: return r.content except Exception as e: raise KalturaClientException( e, KalturaClientException.ERROR_READ_FAILED)