Esempio n. 1
0
    def _url_query(self, api_call, retries=0):
        try:
            if self.debug:
                print("Query url: " + self.url + api_call)
            response = urllib.request.urlopen(self.url + api_call)
            if response.code == 200:
                return True
            return False  # pragma: no cover

        except urllib.error.HTTPError as e:
            if e.code == 409:
                raise exceptions.AddressAlreadyRegistered(
                    self.address, self.url)
            elif e.code == 404:
                raise exceptions.FarmerNotFound(self.url)
            elif e.code == 400:
                raise exceptions.InvalidAddress(self.address)
            elif e.code == 500:  # pragma: no cover
                raise exceptions.FarmerError(self.url)  # pragma: no cover
            else:
                raise e  # pragma: no cover
        except HTTPException:
            self._handle_connection_error(api_call, retries)
        except urllib.error.URLError:
            self._handle_connection_error(api_call, retries)
        except socket.error:
            self._handle_connection_error(api_call, retries)
Esempio n. 2
0
    def _url_query(self, api_call, retries=0):
        try:
            if self.debug:
                print("Query url: " + self.url + api_call)
            response = urllib.request.urlopen(self.url + api_call)
            if response.code == 200:
                return True
            return False  # pragma: no cover

        except urllib.error.HTTPError as e:
            if e.code == 409:
                raise exceptions.AddressAlreadyRegistered(self.address,
                                                          self.url)
            elif e.code == 404:
                raise exceptions.FarmerNotFound(self.url)
            elif e.code == 400:
                raise exceptions.InvalidAddress(self.address)
            elif e.code == 500:  # pragma: no cover
                raise exceptions.FarmerError(self.url)  # pragma: no cover
            else:
                raise e  # pragma: no cover
        except urllib.error.URLError:
            if retries >= self.connection_retry_limit:
                raise exceptions.ConnectionError(self.url)
            time.sleep(self.connection_retry_delay)
            return self._url_query(api_call, retries + 1)
Esempio n. 3
0
 def _url_query(self, api_path, retries=0, authenticate=True):
     try:
         req = urllib.request.Request(self._server_url + api_path)
         if self._get_wif() and authenticate:
             headers = self._create_authentication_headers()
             req.add_header("Date", headers["Date"])
             req.add_header("Authorization", headers["Authorization"])
         response = urllib.request.urlopen(req)
         if response.code == 200:
             return response.read()
     except urllib.error.HTTPError as e:
         if e.code == 409:
             raise exceptions.AddressAlreadyRegistered(
                 self.auth_address(), self._server_url)
         elif e.code == 404:
             raise exceptions.FarmerNotFound(self._server_url)
         elif e.code == 400:
             raise exceptions.InvalidAddress(self.auth_address())
         elif e.code == 500:  # pragma: no cover
             raise exceptions.FarmerError(self._server_url)
         else:
             raise e  # pragma: no cover
     except http.client.HTTPException:
         self._handle_connection_error(api_path, retries, authenticate)
     except urllib.error.URLError:
         self._handle_connection_error(api_path, retries, authenticate)
     except socket.error:
         self._handle_connection_error(api_path, retries, authenticate)
Esempio n. 4
0
    def _url_query(self, api_path, authenticate=True):  # NOQA
        i = 0
        while i <= self.retry_limit:
            i += 1
            try:
                query_url = self._server_url + api_path
                req = Request(query_url)
                starttime = datetime.utcnow()
                if self.wif and authenticate:
                    headers = storjcore.auth.create_headers(
                        self.btctxstore, self._get_server_address(), self.wif)
                    req.add_header("Date", headers["Date"])
                    req.add_header("Authorization", headers["Authorization"])
                logger.info("Query: {0} generated in {1}".format(
                    query_url,
                    datetime.utcnow() - starttime))
                response = urlopen(req, timeout=30)
                if 200 <= response.code <= 299:
                    return response.read()
            except HTTPError as e:
                if e.code == 409:
                    raise exceptions.AddressAlreadyRegistered(
                        self.get_nodeid(), self._server_url)
                elif e.code == 404:
                    raise exceptions.ServerNotFound(self._server_url +
                                                    api_path)
                elif e.code == 400:
                    raise exceptions.InvalidAddress(self.get_nodeid())
                elif e.code == 401:  # auth error (likely clock off)
                    # log "HTTP Error 401: UNAUTHORIZED"
                    logger.warning(logmessages.InvalidAuthenticationHeaders())
                elif e.code == 500:  # pragma: no cover
                    raise exceptions.ServerError(self._server_url)
                else:
                    raise e  # pragma: no cover
            except http.client.HTTPException as e:
                logger.warning(repr(e))
            except URLError as e:
                logger.warning(repr(e))
            except socket.error as e:
                logger.warning(repr(e))

            # retry
            delay = self.retry_delay
            logger.info("Query retry in {0} seconds.".format(delay))
            time.sleep(delay)

        # retry limit
        logger.error("Failed to connect to {0}".format(self._server_url))
        raise exceptions.ConnectionError(self._server_url)
Esempio n. 5
0
 def _url_query(self, api_path, retries=0, authenticate=True):
     try:
         query_url = self._server_url + api_path
         req = urllib.request.Request(query_url)
         if self.wif and authenticate:
             headers = storjcore.auth.create_headers(
                 self.btctxstore, self._get_server_address(), self.wif)
             req.add_header("Date", headers["Date"])
             req.add_header("Authorization", headers["Authorization"])
         logger.info("Query: {0}".format(query_url))
         response = urllib.request.urlopen(req)
         if response.code == 200:
             return response.read()
     except urllib.error.HTTPError as e:
         logger.warning(repr(e))
         if e.code == 409:
             raise exceptions.AddressAlreadyRegistered(
                 self.auth_address(), self._server_url)
         elif e.code == 404:
             raise exceptions.ServerNotFound(self._server_url)
         elif e.code == 400:
             raise exceptions.InvalidAddress(self.auth_address())
         elif e.code == 401:  # auth error (likely clock off)
             logger.warning(repr(e))
             self._handle_connection_error(api_path, retries, authenticate)
         elif e.code == 500:  # pragma: no cover
             raise exceptions.ServerError(self._server_url)
         else:
             raise e  # pragma: no cover
     except http.client.HTTPException as e:
         logger.warning(repr(e))
         self._handle_connection_error(api_path, retries, authenticate)
     except urllib.error.URLError as e:
         logger.warning(repr(e))
         self._handle_connection_error(api_path, retries, authenticate)
     except socket.error as e:
         logger.warning(repr(e))
         self._handle_connection_error(api_path, retries, authenticate)