def getPostWithTimeout(self, seconds=10, body=None): if self._cancel: return self.logRequest(body, seconds, False) try: if self.method == 'PUT': res = self.session.put(self.url, timeout=seconds, stream=True) elif self.method == 'DELETE': res = self.session.delete(self.url, timeout=seconds, stream=True) elif self.method == 'HEAD': res = self.session.head(self.url, timeout=seconds, stream=True) elif self.method == 'POST' or body is not None: res = self.session.post(self.url, data=body, timeout=seconds, stream=True) else: res = self.session.get(self.url, timeout=seconds, stream=True) self.currentResponse = res if self._cancel: return None util.LOG("Got a {0} from {1}".format(res.status_code, util.cleanToken(self.url))) # self.event = msg return res except Exception, e: util.WARN_LOG( "Request to {0} errored out after {1} ms: {2}".format( self.url, seconds, e.message))
def connect(self): util.LOG('Connecting: {0}'.format(util.cleanToken(self.URL))) try: self.data = self.query('/') self.reachable = True return True except Exception as err: util.ERROR(util.cleanToken(self.URL), err) util.LOG('Connecting: Secure failed, trying insecure...') self.secure = False try: self.data = self.query('/') self.reachable = True return True except Exception as err: util.ERROR(util.cleanToken(self.URL), err) return False
def onRequestTimeout(self, context): requestID = context.request.getIdentity() if requestID not in self.pendingRequests: return context.request.cancel() util.WARN_LOG("Request to {0} timed out after {1} sec".format(util.cleanToken(context.request.url), context.timeout)) if context.callback: context.callback(None, context)
def getPostWithTimeout(self, seconds=DEFAULT_TIMEOUT, body=None): if self._cancel: return self.logRequest(body, seconds, False) try: if self.method == 'PUT': res = self.session.put(self.url, timeout=seconds, stream=True) elif self.method == 'DELETE': res = self.session.delete(self.url, timeout=seconds, stream=True) elif self.method == 'HEAD': res = self.session.head(self.url, timeout=seconds, stream=True) elif self.method == 'POST' or body is not None: res = self.session.post(self.url, data=body, timeout=seconds, stream=True) else: res = self.session.get(self.url, timeout=seconds, stream=True) self.currentResponse = res if self._cancel: return None util.LOG("Got a {0} from {1}".format(res.status_code, util.cleanToken(self.url))) # self.event = msg return res except Exception, e: info = traceback.extract_tb(sys.exc_info()[2])[-1] util.WARN_LOG( "Request errored out - URL: {0} File: {1} Line: {2} Msg: {3}". format(util.cleanToken(self.url), os.path.basename(info[0]), info[1], e.message))
def _startAsync(self, body=None, contentType=None, context=None): timeout = context and context.timeout or DEFAULT_TIMEOUT self.logRequest(body, timeout) if self._cancel: return try: if self.method == 'PUT': res = self.session.put(self.url, timeout=timeout, stream=True) elif self.method == 'DELETE': res = self.session.delete(self.url, timeout=timeout, stream=True) elif self.method == 'HEAD': res = self.session.head(self.url, timeout=timeout, stream=True) elif self.method == 'POST' or body is not None: if not contentType: self.session.headers[ "Content-Type"] = "application/x-www-form-urlencoded" else: self.session.headers[ "Content-Type"] = mimetypes.guess_type(contentType) res = self.session.post(self.url, data=body or None, timeout=timeout, stream=True) else: res = self.session.get(self.url, timeout=timeout, stream=True) util.TEST(res) self.currentResponse = res if self._cancel: return except asyncadapter.TimeoutException: import plexapp plexapp.APP.onRequestTimeout(context) self.removeAsPending() return except Exception, e: util.ERROR('Request failed {0}'.format(util.cleanToken(self.url)), e) if not hasattr(e, 'response'): return res = e.response
context.callbackCtx = callback_.context return context def onResponse(self, event, context): if context.completionCallback: response = HttpResponse(event) context.completionCallback(self, response, context) def logRequest(self, body, timeout=None, async=True): # Log the real request method method = self.method if not method: method = body is not None and "POST" or "GET" util.LOG("Starting request: {0} {1} (async={2} timeout={3})".format( method, util.cleanToken(self.url), async, timeout)) class HttpResponse(object): def __init__(self, event): self.event = event if not self.event is None: self.event.content # force data to be read self.event.close() def isSuccess(self): if not self.event: return False return self.event.status_code >= 200 and self.event.status_code < 300 def isError(self):