def get(self, end_point, params, response): """ :param end_point: str :param params: dict :param responseType: APIResponse """ req = self._buildRequest(end_point, params) qlogger.debug("Making request: %s with params=%s", req.get_full_url(), params) try: """ :type request: urllib2.Request """ request = urllib2.urlopen(req) # handle obvious errors if request.getcode() != 200: qlogger.error("Got NOK response from API: %s", request.read) raise APIRequestError("request failed: %s", request.read) response.response = request return response except urllib2.URLError, ue: qlogger.error("Error during request to %s, [%s] %s", end_point, ue.errno, ue.reason) raise APIRequestError("Error during request to %s, [%s] %s" % (end_point, ue.errno, ue.reason))
def _handle_and_return_response(self): try: firstChunk = True with open(self.file_name, 'wb') as fp: while True: chunk = self.response.read( XMLFileBufferedResponse.READ_CHUNK_SIZE) if not chunk: break if firstChunk: firstChunk = False if chunk.startswith("<!--"): (discard, chunk) = chunk.split("\n", 1) #end if #ned if fp.write(chunk) #end while qlogger.debug("wrote xml data to file: %s", self.file_name) return True except IOError, e: qlogger.error("Unable to save data to file %s", self.file_name) qlogger.exception(e) raise APIResponseError("Unable to save data to file: %s: %s", self.file_name, e.message)
def _process_root_element(self, elem): qlogger.debug("processing id set") ids = IDSet() if elem.tag == "ID_SET": for id in list(elem): if id.tag == 'ID' or id.tag == 'ID_RANGE': ids.addString(id.text) else: #do something about this maybe? pass #end if #end for else: pass #end if self._handle_idset(ids)
def validate(self): """:type response: str """ response = self.get("/msp/about.php", {}, SimpleAPIResponse()) response_text = response.get_response() if response_text.count("WEB-VERSION") > 0: root = ET.fromstring(response_text) version_parts = root.find('WEB-VERSION').text.split('.') qlogger.debug(version_parts) if len(version_parts) > 2: self.qweb_version = float(version_parts[0] + '.' + version_parts[1]) qlogger.debug("Found QWEB_VERSION=%s", self.qweb_version) return True else: qlogger.debug(response_text) return False
def _handle_idset(self, idset): qlogger.debug("Got Host IDs: %s" % (idset.tostring()))
def preflight(self): if self._config.useProxy: proxy = urllib2.ProxyHandler({"https": self._config.proxyHost}) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) qlogger.debug("installed proxy handler")