Python aiohttp.ClientSession() Examples
The following are 30
code examples of aiohttp.ClientSession().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp
, or try the search function
.
Example #1
Source File: httpUtil.py From hsds with Apache License 2.0 | 7 votes |
def get_http_client(app): """ get http client """ if "client" in app: return app["client"] # first time call, create client interface # use shared client so that all client requests # will share the same connection pool if "loop" not in app: raise KeyError("loop not initialized") loop = app["loop"] max_tcp_connections = int(config.get("max_tcp_connections")) log.info(f"Initiating TCPConnector with limit {max_tcp_connections} connections") client = ClientSession(loop=loop, connector=TCPConnector(limit_per_host=max_tcp_connections)) #create the app object app['client'] = client return client
Example #2
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_should_answer_with_low_score_without_provided_context(self): qna = QnAMaker(QnaApplicationTest.tests_endpoint) question: str = "where can I buy?" options = QnAMakerOptions(top=2, context=None) turn_context = QnaApplicationTest._get_context(question, TestAdapter()) response_json = QnaApplicationTest._get_json_for_file( "AnswerWithLowScoreProvidedWithoutContext.json" ) with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): results = await qna.get_answers(turn_context, options) self.assertEqual( 2, len(results), "Should have received more than one answers." ) self.assertEqual(True, results[0].score < 1, "Score should be low.")
Example #3
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_should_filter_low_score_variation(self): options = QnAMakerOptions(top=5) qna = QnAMaker(QnaApplicationTest.tests_endpoint, options) question: str = "Q11" context = QnaApplicationTest._get_context(question, TestAdapter()) response_json = QnaApplicationTest._get_json_for_file("TopNAnswer.json") with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): results = await qna.get_answers(context) self.assertEqual(4, len(results), "Should have received 4 answers.") filtered_results = qna.get_low_score_variation(results) self.assertEqual( 3, len(filtered_results), "Should have 3 filtered answers after low score variation.", )
Example #4
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_should_answer_with_high_score_provided_context(self): qna = QnAMaker(QnaApplicationTest.tests_endpoint) question: str = "where can I buy?" context = QnARequestContext( previous_qna_id=5, previous_user_query="how do I clean the stove?" ) options = QnAMakerOptions(top=2, qna_id=55, context=context) turn_context = QnaApplicationTest._get_context(question, TestAdapter()) response_json = QnaApplicationTest._get_json_for_file( "AnswerWithHighScoreProvidedContext.json" ) with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): results = await qna.get_answers(turn_context, options) self.assertEqual(1, len(results), "Should have received 1 answers.") self.assertEqual(1, results[0].score, "Score should be high.")
Example #5
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def _get_service_result( cls, utterance: str, response_file: str, bot_adapter: BotAdapter = TestAdapter(), options: QnAMakerOptions = None, ) -> [dict]: response_json = QnaApplicationTest._get_json_for_file(response_file) qna = QnAMaker(QnaApplicationTest.tests_endpoint) context = QnaApplicationTest._get_context(utterance, bot_adapter) with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): result = await qna.get_answers(context, options) return result
Example #6
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def _get_service_result_raw( cls, utterance: str, response_file: str, bot_adapter: BotAdapter = TestAdapter(), options: QnAMakerOptions = None, ) -> [dict]: response_json = QnaApplicationTest._get_json_for_file(response_file) qna = QnAMaker(QnaApplicationTest.tests_endpoint) context = QnaApplicationTest._get_context(utterance, bot_adapter) with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): result = await qna.get_answers_raw(context, options) return result
Example #7
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_returns_answer_with_timeout(self): question: str = "how do I clean the stove?" options = QnAMakerOptions(timeout=999999) qna = QnAMaker(QnaApplicationTest.tests_endpoint, options) context = QnaApplicationTest._get_context(question, TestAdapter()) response_json = QnaApplicationTest._get_json_for_file("ReturnsAnswer.json") with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): result = await qna.get_answers(context, options) self.assertIsNotNone(result) self.assertEqual( options.timeout, qna._generate_answer_helper.options.timeout )
Example #8
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_should_answer_with_prompts(self): options = QnAMakerOptions(top=2) qna = QnAMaker(QnaApplicationTest.tests_endpoint, options) question: str = "how do I clean the stove?" turn_context = QnaApplicationTest._get_context(question, TestAdapter()) response_json = QnaApplicationTest._get_json_for_file("AnswerWithPrompts.json") with patch( "aiohttp.ClientSession.post", return_value=aiounittest.futurized(response_json), ): results = await qna.get_answers(turn_context, options) self.assertEqual(1, len(results), "Should have received 1 answers.") self.assertEqual( 1, len(results[0].context.prompts), "Should have received 1 prompt." )
Example #9
Source File: request_manager.py From darksky with MIT License | 6 votes |
def make_request( self, url: str, session: ClientSession, **params ): assert isinstance(session, ClientSession) for key in list(params.keys()): if params[key] is None: del params[key] elif isinstance(params[key], list): params[key] = ",".join(params[key]) async with session.get( url, params=params, headers=self.headers ) as resp: response = await resp.json() if "error" in response: raise DarkSkyException(response["code"], response["error"]) response["timezone"] = params.get("timezone") or response["timezone"] return response
Example #10
Source File: api.py From darksky with MIT License | 6 votes |
def get_forecast( self, latitude: float, longitude: float, client_session: aiohttp.ClientSession, extend: bool = None, lang=languages.ENGLISH, values_units=units.AUTO, exclude: [weather] = None, timezone: str = None, ) -> Forecast: url = self.get_url(latitude, longitude) data = await self.request_manager.make_request( url=url, extend=weather.HOURLY if extend else None, lang=lang, units=values_units, exclude=exclude, timezone=timezone, session=client_session, ) return Forecast(**data)
Example #11
Source File: api.py From darksky with MIT License | 6 votes |
def get_time_machine_forecast( self, latitude: float, longitude: float, time: datetime, client_session: aiohttp.ClientSession, extend: bool = False, lang=languages.ENGLISH, values_units=units.AUTO, exclude: [weather] = None, timezone: str = None ) -> Forecast: url = self.get_url(latitude, longitude, int(time.timestamp())) data = await self.request_manager.make_request( url=url, extend=weather.HOURLY if extend else None, lang=lang, units=values_units, exclude=exclude, timezone=timezone, session=client_session, ) return Forecast(**data)
Example #12
Source File: test_forecast.py From darksky with MIT License | 6 votes |
def get_forecast_async(): async def get_async_data(): darksky = DarkSkyAsync("api_key") with aioresponses.aioresponses() as resp: resp.get(re.compile(".+"), status=200, payload=copy.deepcopy(DATA)) result = await darksky.get_forecast( DATA["latitude"], DATA["longitude"], client_session=aiohttp.ClientSession() ) return result loop = asyncio.get_event_loop() return loop.run_until_complete(get_async_data())
Example #13
Source File: aiohttp_.py From uplink with MIT License | 6 votes |
def create(cls, *args, **kwargs): """ Builds a client instance with :py:class:`aiohttp.ClientSession` arguments. Instead of directly initializing this class with a :py:class:`aiohttp.ClientSession`, use this method to have the client lazily construct a session when sending the first request. Hence, this method guarantees that the creation of the underlying session happens inside of a coroutine. Args: *args: positional arguments that :py:class:`aiohttp.ClientSession` takes. **kwargs: keyword arguments that :py:class:`aiohttp.ClientSession` takes. """ session_build_args = cls._create_session(*args, **kwargs) return AiohttpClient(session=session_build_args)
Example #14
Source File: client.py From clashroyale with MIT License | 6 votes |
def __init__(self, token, session=None, is_async=False, **options): self.token = token self.is_async = is_async self.error_debug = options.get('error_debug', False) self.timeout = options.get('timeout', 10) self.api = API(options.get('url', 'https://api.royaleapi.com')) self.session = session or (aiohttp.ClientSession() if is_async else requests.Session()) self.camel_case = options.get('camel_case', False) self.headers = { 'Authorization': 'Bearer {}'.format(token), 'User-Agent': 'python-clashroyale-client (fourjr/kyb3r) ' + options.get('user_agent', '') } self.cache_fp = options.get('cache_fp') self.using_cache = bool(self.cache_fp) self.cache_reset = options.get('cache_expires', 300) self.ratelimit = [10, 10, 0] if self.using_cache: table = options.get('table_name', 'cache') self.cache = SqliteDict(self.cache_fp, table)
Example #15
Source File: test_clients.py From uplink with MIT License | 6 votes |
def test_close_auto_created_session(self, mocker): # Setup import asyncio import gc import aiohttp mock_session = mocker.Mock(spec=aiohttp.ClientSession) session_cls_mock = mocker.patch("aiohttp.ClientSession") session_cls_mock.return_value = mock_session positionals = [1] keywords = {"keyword": 2} # Run: Create client client = aiohttp_.AiohttpClient.create(*positionals, **keywords) # Run: Get session loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.ensure_future(client.session())) # Verify: session created with args session_cls_mock.assert_called_with(*positionals, **keywords) del client gc.collect() session_cls_mock.return_value.close.assert_called_with()
Example #16
Source File: test_clients.py From uplink with MIT License | 6 votes |
def test_create(self, mocker): # Setup import asyncio session_cls_mock = mocker.patch("aiohttp.ClientSession") positionals = [1] keywords = {"keyword": 2} # Run: Create client client = aiohttp_.AiohttpClient.create(*positionals, **keywords) # Verify: session hasn't been created yet. assert not session_cls_mock.called # Run: Get session loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.ensure_future(client.session())) # Verify: session created with args session_cls_mock.assert_called_with(*positionals, **keywords)
Example #17
Source File: test_aio.py From python-consul2 with MIT License | 6 votes |
def test_http_session(self, loop, local_timeout_server, consul_port): async def test_session_close(): http_server = await local_timeout_server c = consul.aio.Consul(port=http_server.port, loop=loop) c.agent.services() c.http._session = aiohttp.ClientSession() assert not c.http._session.closed c.http.__del__() await c.http.close() assert c.http._session.closed http_server.server.stop() ... loop.run_until_complete(test_session_close())
Example #18
Source File: logs.py From maubot with GNU Affero General Public License v3.0 | 5 votes |
def view_logs(server: str, token: str) -> None: async with ClientSession() as session: async with session.ws_connect(f"{server}/_matrix/maubot/v1/logs") as ws: await ws.send_str(token) try: msg: WSMessage async for msg in ws: if msg.type == WSMsgType.TEXT: if not handle_msg(msg.json()): break elif msg.type == WSMsgType.ERROR: print(Fore.YELLOW + "Connection error: " + msg.data + Fore.RESET) elif msg.type == WSMsgType.CLOSE: print(Fore.YELLOW + "Server closed connection" + Fore.RESET) except asyncio.CancelledError: pass
Example #19
Source File: resource.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def get_watcher_status(request: web.Request, params: Any) -> web.Response: log.info('GET_WATCHER_STATUS ()') watcher_info = await get_watcher_info(request, params['agent_id']) connector = aiohttp.TCPConnector() async with aiohttp.ClientSession(connector=connector) as sess: with _timeout(5.0): headers = {'X-BackendAI-Watcher-Token': watcher_info['token']} async with sess.get(watcher_info['addr'], headers=headers) as resp: if resp.status == 200: data = await resp.json() return web.json_response(data, status=resp.status) else: data = await resp.text() return web.Response(text=data, status=resp.status)
Example #20
Source File: profile.py From sudoBot with GNU General Public License v3.0 | 5 votes |
def wallpaper(self, ctx, *, url=""): """Set a profile card background image. Can either be a link to an image or an attachment.""" try: background = ctx.message.attachments[0].url except: background = url if (background == ""): return await ctx.send('```Image or URL not found.```') user = ctx.message.author async with aiohttp.ClientSession() as session: async with session.get(background) as r: image = await r.content.read() with open('data/users/backgrounds/{0}.png'.format(user.id),'wb') as f: f.write(image) isImage = imghdr.what('data/users/backgrounds/{0}.png'.format(user.id)) if(isImage == 'png' or isImage == 'jpeg' or isImage == 'jpg' or isImage == 'gif'): f.close() return await ctx.send('```Successfully set profile wallpaper```') else: f.close() os.remove('data/users/backgrounds/{0}.png'.format(user.id)) return await ctx.send('```Something went wrong when setting your wallpaper. Perhaps the file you sent wasn\'t an image?```')
Example #21
Source File: resource.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def watcher_agent_start(request: web.Request, params: Any) -> web.Response: log.info('WATCHER_AGENT_START ()') watcher_info = await get_watcher_info(request, params['agent_id']) connector = aiohttp.TCPConnector() async with aiohttp.ClientSession(connector=connector) as sess: with _timeout(20.0): watcher_url = watcher_info['addr'] / 'agent/start' headers = {'X-BackendAI-Watcher-Token': watcher_info['token']} async with sess.post(watcher_url, headers=headers) as resp: if resp.status == 200: data = await resp.json() return web.json_response(data, status=resp.status) else: data = await resp.text() return web.Response(text=data, status=resp.status)
Example #22
Source File: __init__.py From trader with Apache License 2.0 | 5 votes |
def update_from_czce(day: datetime.datetime): try: async with aiohttp.ClientSession() as session: day_str = day.strftime('%Y%m%d') rst = await fetch_czce_page( session, 'http://{}/portal/DFSStaticFiles/Future/{}/{}/FutureDataDaily.txt'.format( czce_ip, day.year, day_str)) if rst is None: rst = await fetch_czce_page( session, 'http://{}/portal/exchange/{}/datadaily/{}.txt'.format( czce_ip, day.year, day_str)) for lines in rst.split('\r\n')[1:-3]: if '小计' in lines or '品种' in lines: continue inst_data = [x.strip() for x in lines.split('|' if '|' in lines else ',')] # error_data = inst_data """ [0'品种月份', 1'昨结算', 2'今开盘', 3'最高价', 4'最低价', 5'今收盘', 6'今结算', 7'涨跌1', 8'涨跌2', 9'成交量(手)', 10'空盘量', 11'增减量', 12'成交额(万元)', 13'交割结算价'] ['CF601', '11,970.00', '11,970.00', '11,970.00', '11,800.00', '11,870.00', '11,905.00', '-100.00', '-65.00', '13,826', '59,140', '-10,760', '82,305.24', ''] """ close = inst_data[5].replace(',', '') if Decimal(inst_data[5].replace(',', '')) > 0.1 \ else inst_data[6].replace(',', '') DailyBar.objects.update_or_create( code=inst_data[0], exchange=ExchangeType.CZCE, time=day, defaults={ 'expire_date': get_expire_date(inst_data[0], day), 'open': inst_data[2].replace(',', '') if Decimal(inst_data[2].replace(',', '')) > 0.1 else close, 'high': inst_data[3].replace(',', '') if Decimal(inst_data[3].replace(',', '')) > 0.1 else close, 'low': inst_data[4].replace(',', '') if Decimal(inst_data[4].replace(',', '')) > 0.1 else close, 'close': close, 'settlement': inst_data[6].replace(',', '') if Decimal(inst_data[6].replace(',', '')) > 0.1 else inst_data[1].replace(',', ''), 'volume': inst_data[9].replace(',', ''), 'open_interest': inst_data[10].replace(',', '')}) except Exception as e: print('update_from_czce failed: %s' % e)
Example #23
Source File: plugin_base.py From maubot with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, client: 'MaubotMatrixClient', loop: AbstractEventLoop, http: ClientSession, instance_id: str, log: Logger, config: Optional['BaseProxyConfig'], database: Optional[Engine], webapp: Optional['PluginWebApp'], webapp_url: Optional[str]) -> None: self.client = client self.loop = loop self.http = http self.id = instance_id self.log = log self.config = config self.database = database self.webapp = webapp self.webapp_url = URL(webapp_url) if webapp_url else None self._handlers_at_startup = []
Example #24
Source File: async_client.py From hsds with Apache License 2.0 | 5 votes |
def fetch(url): async with ClientSession() as session: async with session.get(url) as res: await res.read() delay = res.headers.get("DELAY") d = res.headers.get("DATE") retval = "{}:{} delay {}".format(d, res.url, delay) return retval
Example #25
Source File: httpUtil.py From hsds with Apache License 2.0 | 5 votes |
def http_delete(app, url, data=None, params=None): # TBD - do we really need a data param? log.info(f"http_delete('{url}')") #client = get_http_client(app) rsp_json = None timeout = config.get("timeout") import aiohttp try: async with aiohttp.ClientSession() as session: async with session.delete(url, json=data, params=params, timeout=timeout) as rsp: log.info(f"http_delete status: {rsp.status}") if rsp.status == 200: pass # expectred elif rsp.status == 404: log.info(f"NotFound response for DELETE for url: {url}") elif rsp.status == 503: log.warn(f"503 error for http_delete {url}") raise HTTPServiceUnavailable() else: log.error(f"DELETE request error for url: {url} - status: {rsp.status}") raise HTTPInternalServerError() #rsp_json = await rsp.json() #log.debug(f"http_delete({url}) response: {rsp_json}") except ClientError as ce: log.error(f"ClientError for http_delete({url}): {ce} ") raise HTTPInternalServerError() except CancelledError as cle: log.error(f"CancelledError for http_delete({url}): {cle}") raise HTTPInternalServerError() except ConnectionResetError as cre: log.error(f"ConnectionResetError for http_delete({url}): {cre}") raise HTTPInternalServerError() return rsp_json
Example #26
Source File: aiohttp_.py From uplink with MIT License | 5 votes |
def session(self): """Returns the underlying `aiohttp.ClientSession`.""" if isinstance(self._session, self.__ARG_SPEC): args, kwargs = self._session self._session = aiohttp.ClientSession(*args, **kwargs) self._auto_created_session = True return self._session
Example #27
Source File: aiohttp_.py From uplink with MIT License | 5 votes |
def __del__(self): if self._auto_created_session: # aiohttp v3.0 has made ClientSession.close a coroutine, # so we check whether it is one here and register it # to run appropriately at exit if asyncio.iscoroutinefunction(self._session.close): asyncio.get_event_loop().run_until_complete( self._session.close() ) else: self._session.close()
Example #28
Source File: test_clients.py From uplink with MIT License | 5 votes |
def aiohttp_session_mock(mocker): import aiohttp return mocker.Mock(spec=aiohttp.ClientSession)
Example #29
Source File: __init__.py From trader with Apache License 2.0 | 5 votes |
def update_from_dce(day: datetime.datetime): try: async with aiohttp.ClientSession() as session: await max_conn_dce.acquire() async with session.post('http://{}/publicweb/quotesdata/exportDayQuotesChData.html'.format(dce_ip), data={ 'dayQuotes.variety': 'all', 'dayQuotes.trade_type': 0, 'exportFlag': 'txt'}) as response: rst = await response.text() max_conn_dce.release() for lines in rst.split('\r\n')[1:-3]: if '小计' in lines or '品种' in lines: continue inst_data_raw = [x.strip() for x in lines.split('\t')] inst_data = [] for cell in inst_data_raw: if len(cell) > 0: inst_data.append(cell) """ [0'商品名称', 1'交割月份', 2'开盘价', 3'最高价', 4'最低价', 5'收盘价', 6'前结算价', 7'结算价', 8'涨跌', 9'涨跌1', 10'成交量', 11'持仓量', 12'持仓量变化', 13'成交额'] ['豆一', '1611', '3,760', '3,760', '3,760', '3,760', '3,860', '3,760', '-100', '-100', '2', '0', '0', '7.52'] """ if '小计' in inst_data[0]: continue DailyBar.objects.update_or_create( code=DCE_NAME_CODE[inst_data[0]] + inst_data[1], exchange=ExchangeType.DCE, time=day, defaults={ 'expire_date': inst_data[1], 'open': inst_data[2].replace(',', '') if inst_data[2] != '-' else inst_data[5].replace(',', ''), 'high': inst_data[3].replace(',', '') if inst_data[3] != '-' else inst_data[5].replace(',', ''), 'low': inst_data[4].replace(',', '') if inst_data[4] != '-' else inst_data[5].replace(',', ''), 'close': inst_data[5].replace(',', ''), 'settlement': inst_data[7].replace(',', '') if inst_data[7] != '-' else inst_data[6].replace(',', ''), 'volume': inst_data[10].replace(',', ''), 'open_interest': inst_data[11].replace(',', '')}) except Exception as e: print('update_from_dce failed: %s' % e)
Example #30
Source File: __init__.py From trader with Apache License 2.0 | 5 votes |
def is_trading_day(day: datetime.datetime): s = redis.StrictRedis( host=config.get('REDIS', 'host', fallback='localhost'), db=config.getint('REDIS', 'db', fallback=1), decode_responses=True) return day, day.strftime('%Y%m%d') in (s.get('TradingDay'), s.get('LastTradingDay')) # async with aiohttp.ClientSession() as session: # await max_conn_cffex.acquire() # async with session.get( # 'http://{}/fzjy/mrhq/{}/index.xml'.format(cffex_ip, day.strftime('%Y%m/%d')), # allow_redirects=False) as response: # max_conn_cffex.release() # return day, response.status != 302