def notify_single_user(self, message: typing.Text, user: User): assert user.slack_team_id is not None params = self.get_params_for_workspace(user.slack_team_id) params['text'] = message.encode('utf-8') params['channel'] = user.slack_user_id resp = requests.get(API_URL, params=params) content = json.loads(resp.content.decode('utf-8')) logger.info('Posted to user %s: response:%s, content:%s', user.id, resp.status_code, content)
def notify_channel(self, message: typing.Text, team_id: typing.Text): params = self.get_params_for_workspace(team_id) params['text'] = message.encode('utf-8') params['channel'] = '#coffee' resp = requests.get(API_URL, params=params) content = json.loads(resp.content.decode('utf-8')) logger.info('Posted to channel: response:%s, content:%s', resp.status_code, content)
async def fetch( self, query: typing.Text ) -> typing.Text: """ Fetch data by query. :param query: Query. :type query: str :return: Query result. :rtype: JsonType """ async with self.semaphore: self.logger.debug("Query: %s", query) data = query.encode(self.encoding) async with aiohttp.ClientSession( conn_timeout=60, read_timeout=None ) as session: async with session.post( self.query_url, data=data, timeout=None ) as response: self.logger.debug("Response status: %d", response.status) # This could easily be implemented with stream.readline(), however, # that method raises an exception if a line is longer than 2**16 # bytes. stream = response.content all_bytes = bytes() while not stream.at_eof(): next_bytes = await stream.read(self.buffer_size) all_bytes += next_bytes text = all_bytes.decode(self.encoding) if (response.status // 100) == 2: # Success HTTP response return text else: raise RuntimeError( "HTTP Response Status: {}\n{}".format( response.status, text ) )
def synchronous_fetch(self, query: typing.Text) -> typing.Text: """ Synchronously fetch data by query. :param query: Query. :type query: str :return: Query result. :rtype: JsonType """ data = query.encode(self.encoding) response = urllib.request.urlopen(self.query_url, data) str_response = response.read().decode(self.encoding) return str_response
async def post_data( self, data: typing.Text ) -> None: """ Post new data. :param data: data. :type data: str """ async with self.semaphore: async with aiohttp.ClientSession() as session: async with session.post( self.post_url, data=data.encode(self.encoding) ) as response: self.logger.debug("Response status: %d", response.status) if response.status != 200: raise RuntimeError("Can't post data")