def load_user_info(self): """If a new authorization is requested, check Graylog authentication on /roles using the given credentials. Forbidden is expected. This process allow to login using tokens. :return: result of GET /users/{username} on success else None """ if getattr(g, 'user', None): return g.user auth = request.authorization if not auth: g.user = None return g.user client = RESTClient( server=self.app.config['GRAYLOG_API_URL'], **auth ) try: client.do_request(method="GET", path="/roles") except Unauthorized: self.authenticate() except Forbidden as exc: match = re.search("User \[(.*?)\] not authorized", exc.message) if match: g.user = client.do_request( method="GET", path="/users/%s" % match.group(1) ) return g.user
def load_user_info(self): """If a new authorization is requested, check Graylog authentication on /roles using the given credentials. Forbidden is expected. This process allow to login using tokens. :return: result of GET /users/{username} on success else None """ if getattr(g, 'user', None): return g.user auth = request.authorization if not auth: g.user = None return g.user client = RESTClient(server=self.app.config['GRAYLOG_API_URL'], **auth) try: client.do_request(method="GET", path="/roles") except Unauthorized: self.authenticate() except Forbidden as exc: match = re.search("User \[(.*?)\] not authorized", exc.message) if match: g.user = client.do_request(method="GET", path="/users/%s" % match.group(1)) return g.user
class Producer(BaseProducer): """Mother class for producers""" def __init__(self, config): """ Create new Producer instance using provided configuration dict. :param dict config: configuration """ self.client = RESTClient(server=config["bootstrap_servers"], username=config["sasl_plain_username"], password=config["sasl_plain_password"], timeout=config.get("timeout", 10)) def bulk_send(self, topic, kmsgs): """ Send a batch of messages :param str topic: a kafka topic :param ksr.transport.Message kmsgs: Messages to serialize :return: Execution result :rtype: kser.result.Result """ try: self.client.do_request( method="POST", path="/topic/{}".format(topic), data=[dict(Value=k.MARSHMALLOW_SCHEMA.dump(k)) for k in kmsgs]) return Result(stdout="{} message(s) sent".format(len(kmsgs))) except Exception as exc: return Result.from_exception(exc) def _send(self, topic, kmsg, timeout=60): """ Send the message into the given topic :param str topic: a kafka topic :param ksr.transport.Message kmsg: Message to serialize :return: Execution result :rtype: kser.result.Result """ try: self.client.do_request(method="POST", params=dict(format="raw"), path="/topic/{}".format(topic), data=kmsg.MARSHMALLOW_SCHEMA.dump(kmsg), timeout=timeout) result = Result(uuid=kmsg.uuid, stdout="Message sent: {} ({})".format( kmsg.uuid, kmsg.entrypoint)) except Exception as exc: result = Result.from_exception(exc, kmsg.uuid) finally: # noinspection PyUnboundLocalVariable if result.retcode < 300: return self._onsuccess(kmsg=kmsg, result=result) else: return self._onerror(kmsg=kmsg, result=result)
def __init__(self, config): """ Create new Producer instance using provided configuration dict. :param dict config: configuration """ self.client = RESTClient(server=config["bootstrap_servers"], username=config["sasl_plain_username"], password=config["sasl_plain_password"], timeout=config.get("timeout", 10))
class Consumer(object): """Http consumer""" REGISTRY = Controller def __init__(self, config, topics): self.client = RESTClient(server=config["bootstrap_servers"], username=config["sasl_plain_username"], password=config["sasl_plain_password"], timeout=config.get("timeout", 10)) self.topics = topics def call_kafka(self, topic, batch_size=1): """Call Kafka""" logger.debug("{}.Checking {}".format(self.__class__.__name__, topic)) data = self.client.do_request(method="GET", params=dict(limit=batch_size), path="/topic/{}".format(topic)) for item in data: str_msg = item.get("Value", None) if str_msg: self.REGISTRY.run(str_msg) def run(self, loopinfo=None, batch_size=1): """ Run consumer """ logger.info("{}.Starting...".format(self.__class__.__name__)) if loopinfo: while True: for topic in self.topics: self.call_kafka(topic, batch_size) time.sleep(loopinfo.sleep) else: for topic in self.topics: self.call_kafka(topic, batch_size)
def from_remote(src, dst=None): """description of fromurl""" try: file_write(dst, RESTClient(server=src).do_request(method="GET", path="")) except Exception as exc: oncritical(exc)
def client(self): if self._client is None: self._client = RESTClient( server=self.app.config['GRAYLOG_API_URL'], username=self.app.config['GRAYLOG_API_USERNAME'], password=self.app.config['GRAYLOG_API_PASSWORD'], timeout=self.app.config.get("GRAYLOG_API_TIMEOUT", 10) ) return self._client
def tat_client(self): if self._tat_client is None: self._tat_client = RESTClient(server=self.configuration['url'], headers={ "Tat_username": self.configuration["username"], "Tat_password": self.configuration["password"], "Content-type": "application/json", }) return self._tat_client
def __init__(self, config, topics): self.client = RESTClient(server=config["bootstrap_servers"], username=config["sasl_plain_username"], password=config["sasl_plain_password"], timeout=config.get("timeout", 10)) self.topics = topics
def test_post_a(self): client = RESTClient(server="http://jsonplaceholder.typicode.com") with self.assertRaises(NotFound): client.do_request(method="GET", path="/posts/a")
def test_post_1(self): client = RESTClient(server="http://jsonplaceholder.typicode.com") data = client.do_request(method="GET", path="/posts/1") self.assertEqual(data['userId'], 1) self.assertEqual(data['id'], 1)