def _get(self, resource): try: return requests.get(self.address + resource).json() except requests.exceptions.ConnectionError as exc: raise exceptions.BlogInternetError(exc) from exc except Exception as exc: raise exceptions.BlogMethodError(exc) from exc
def _call(self, method): try: return self._iface.call(method) except OSError as exc: raise exceptions.BlogInternetError(exc) from exc except wordpress_xmlrpc.exceptions.InvalidCredentialsError as exc: raise exceptions.BlogAuthenticationError(exc) from exc except Exception as exc: raise exceptions.BlogMethodError(exc) from exc
def _login(self): config_dict = config.get_blog_config() address = (self.address or config_dict["url"]) + self.API # authentication errors are never raised from here; socket related errors # can be raised on connection troubles; ServerConnectionError can be # raised by wordpress_xmlrpc on xmlrpc client ProtocolError but actually, on invalid # XML-RPC protocol, the OSError is raised by xmlrpc instead of the above try: self._iface = wordpress_xmlrpc.Client(address, config_dict["user_name"], config_dict["password"]) except OSError as exc: raise exceptions.BlogInternetError(exc) from exc except Exception as exc: raise exceptions.BlogMethodError(exc) from exc
def _get(self, resource): try: with self._lock: if (time.time() - self._last_req_ts) < self._reqs_interval: # we should go to sleep for not too long because new # reqest can arrive at any time so maybe it can happen # that the timeout is just about to expire left_to_wait = self._reqs_interval - (time.time() - self._last_req_ts) time.sleep(left_to_wait) self._last_req_ts = time.time() ret = requests.get(self.address + resource).json() self._last_req_ts = time.time() return ret except requests.exceptions.ConnectionError as exc: raise exceptions.BlogInternetError(exc) from exc except socket.timeout: raise except Exception as exc: raise exceptions.BlogMethodError(exc) from exc
def _call(self, method): try: with self._lock: if (time.time() - self._last_req_ts) < self._reqs_interval: # we should go to sleep for not too long because new # request can arrive at any time so maybe it can happen # that the timeout is just about to expire left_to_wait = self._reqs_interval - (time.time() - self._last_req_ts) time.sleep(left_to_wait) self._last_req_ts = time.time() ret = self._iface.call(method) self._last_req_ts = time.time() return ret except OSError as exc: raise exceptions.BlogInternetError(exc) from exc except wordpress_xmlrpc.exceptions.InvalidCredentialsError as exc: raise exceptions.BlogAuthenticationError(exc) from exc except socket.timeout: raise except Exception as exc: raise exceptions.BlogMethodError(exc) from exc
def _login(self): address = (self.address or self.config_dict["address"]) + self.API # authentication errors are never raised from here; socket related errors # can be raised on connection troubles; ServerConnectionError can be # raised by wordpress_xmlrpc on xmlrpc client ProtocolError but actually, on invalid # XML-RPC protocol, the OSError is raised by xmlrpc instead of the above if not (internet_on()): raise exceptions.BlogInternetError() for key in ['user_name', 'address', 'password']: if not (self.config_dict[key]): raise exceptions.BlogEmptyConfError() try: self._iface = wordpress_xmlrpc.Client( address, self.config_dict["user_name"], self.config_dict["password"]) self._last_req_ts = time.time() except OSError as exc: raise exceptions.BlogConfigurationError(exc) from exc except Exception as exc: raise exceptions.BlogMethodError(exc) from exc