async def async_start(self, any_zone): """Start the event listener listening on the local machine under the lock. Args: any_zone (SoCo): Any Sonos device on the network. It does not matter which device. It is used only to find a local IP address reachable by the Sonos net. """ if not self.start_lock: self.start_lock = asyncio.Lock() async with self.start_lock: if self.is_running: return # Use configured IP address if there is one, else detect # automatically. ip_address = get_listen_ip(any_zone.ip_address) if not ip_address: log.exception("Could not start Event Listener: check network.") # Otherwise, no point trying to start server return port = await self.async_listen(ip_address) if not port: return self.address = (ip_address, port) client_timeout = ClientTimeout(total=10) self.session = ClientSession(raise_for_status=True, timeout=client_timeout) self.is_running = True log.debug("Event Listener started")
async def _async_request(self, method: str, endpoint: str, **kwargs) -> ElementTree: """Make a request to the API and return the XML response.""" kwargs.setdefault("params", {}) kwargs["params"]["auth_token"] = self._api_token use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout(total=DEFAULT_TIMEOUT)) try: async with session.request( method, f"{API_URL_BASE}/{endpoint}", **kwargs ) as resp: resp.raise_for_status() body = await resp.text() _LOGGER.debug("Response text for %s: %s", endpoint, body) response_root = ElementTree.fromstring(body.encode("utf-8")) raise_on_response_error(response_root) return response_root except ClientError as err: raise RequestError(err) from None finally: if not use_running_session: await session.close()
async def get_content_async(data): """ Asynchronously hit each hacker news article item for download. :param data: tuple of url and article type :return: list of json responses """ parent_tasks = [] child_tasks = [] timeout = ClientTimeout(total=7 * 60) connector = TCPConnector(ssl=False, family=socket.AF_INET) async with ClientSession(loop=asyncio.get_event_loop(), timeout=timeout, connector=connector) as session: for url, article_type in data: parent_tasks.append(asyncio.create_task(fetch(url, session))) parent_results = await asyncio.gather(*parent_tasks) for index, section in enumerate(parent_results): _, article_type = data[index] for article_id in section: child_tasks.append( asyncio.create_task( fetch( f'https://hacker-news.firebaseio.com/v0/item/{article_id}.json', session, article_type))) return await asyncio.gather(*child_tasks)
async def async_request(self, method: str, endpoint: str) -> dict: """Make a request against the SmartWeather API.""" use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout( total=DEFAULT_TIMEOUT)) try: async with session.request(method, endpoint) as resp: resp.raise_for_status() data = await resp.read() decoded_content = data.decode("utf-8") return decoded_content except asyncio.TimeoutError: raise RequestError("Request to endpoint timed out: {endpoint}") except ClientError as err: if err.message == "Unauthorized": raise InvalidCredentials( "Your Username/Password combination is not correct") elif err.message == "Not Found": raise ResultError( "The Meteobridge cannot not be found on this IP Address") else: raise RequestError( f"Error requesting data from {endpoint}: {err}") from None finally: if not use_running_session: await session.close()
async def check_is_router( cls, address: str, port: int, semaphore=Semaphore()) -> BaseIndustrialRouter: """ Check if a certain router is an industrial router, given the headers defined at class level. :param address: IP address of the router to check. :param port: Port of the web interface of the device to check. :param semaphore: Asyncio semaphore to be used for concurrency limitation. :return: A :class:`aztarna.industrialrouters.scanner.BaseIndustrialRouter` object if the checked device is a router. None otherwise. """ context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE context.options &= ~ssl.OP_NO_SSLv3 async with semaphore: async with aiohttp.ClientSession( timeout=ClientTimeout(2)) as client: uri = 'http://{}:{}'.format(address, port) print('[+] Connecting to {}'.format(address)) async with client.get(uri, ssl=context) as response: for field, values in cls.possible_headers: if response.headers.get(field) in values: router = cls.router_cls() router.address = address router.port = port return router else: return None
async def _async_request(self, method: str, url: str, data: str = "", **kwargs) -> list: """Make a request against the API.""" kwargs.setdefault("headers", {}) kwargs.setdefault("proxy", SYSTEM_PROXY) use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout( total=DEFAULT_TIMEOUT)) try: async with session.request(method, url, data=data, **kwargs) as resp: resp.raise_for_status() return await resp.json(content_type=None) except ClientError as err: _LOGGER.error(f"Error requesting data from {url}: {err}") raise RequestError(f"Error requesting data from {url}: {err}") except Exception as e: _LOGGER.error(f"Error requesting data from {url}: {e}") finally: if not use_running_session: await session.close()
def _init_session(self, timeout: int) -> ClientSession: return ClientSession(headers={ 'Accept': 'application/json', 'User-Agent': 'aioidex/python', }, timeout=ClientTimeout(total=timeout), loop=self._loop)
async def _portainer_request( url: URL, app_session: ClientSession, method: str, **kwargs ) -> str: attribute = getattr(app_session, method.lower()) async with attribute( url, timeout=ClientTimeout( total=60, connect=None, sock_connect=None, sock_read=None ), **kwargs, ) as resp: log.debug("request received with code %s", resp.status) if resp.status == 200: data = await resp.json() return data if resp.status == 404: log.error("could not find route in %s", url) raise ConfigurationError( "Could not reach Portainer app in {}:\n {}".format( url, await resp.text() ) ) log.error("Unknown error") raise AutoDeployAgentException( "Unknown error ({}) while accessing Portainer app in {}:\n {}".format( str(resp.status), url, await resp.text() ) )
async def async_step_user(self, info): if not info: return self._show_form() url = info.get(CONF_URL) try: async with request('GET', f"{url}/getSystemData", timeout=ClientTimeout(total=5)) as resp: assert resp.status == 200 data = await resp.json(content_type=None) except ServerTimeoutError as err: _LOGGER.error(f"Connection timed out: {err}") return self._show_form({"base": "timeout_error"}) except ClientError as err: _LOGGER.error(f"Unable to connect to MyAir: {err}") return self._show_form({"base": "connection_error"}) if ('aircons' not in data): return self._show_form({"base": "data_error"}) return self.async_create_entry( title=data['system']['name'], data=info, )
async def search(username: str, config, init, connector: aiohttp.TCPConnector = None, session=None, ua=default_user_agent, proxy: str = None) -> dict: """ Composes search parameters and issues request. :param username: :param config: search configuration. config.TweetsPortionSize sets tweets count for query :param init: start index in tweet feed :param connector: :param session: :param ua: User-Agent header value :param proxy: proxy string :returns: JSON response as string """ headers = create_headers(config.BearerToken, config.GuestToken, ua) search_params = search_url(username, config, init) return await request_json(search_params.url, params=search_params.params, connector=connector, session=session, headers=headers, proxy=proxy, timeout=ClientTimeout(config.AiohttpTimeout))
async def _resolve(self, endpoint, hostname, family, timeout=5) -> List[str]: params = { "name": hostname, "type": "AAAA" if family == socket.AF_INET6 else "A", "do": "false", "cd": "false", } async with aiohttp.ClientSession() as session: async with session.get( endpoint, params=params, headers={"accept": "application/dns-json"}, timeout=ClientTimeout(total=timeout)) as resp: if resp.status == 200: return await self.parse_result(hostname, await resp.text()) else: raise Exception( "Failed to resolve {} with {}: HTTP Status {}".format( hostname, endpoint, resp.status))
async def async_request(self, method: str, endpoint: str, rawdata: bool = False) -> dict: """Make a request against the SmartWeather API.""" use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout( total=DEFAULT_TIMEOUT)) try: async with session.request(method, endpoint) as resp: resp.raise_for_status() data = await resp.read() if not rawdata: decoded_content = data.decode("utf-8") return decoded_content else: return data except asyncio.TimeoutError: raise RequestError("Request to endpoint timed out: {endpoint}") except ClientError as err: raise RequestError(f"Error requesting data from {endpoint}: {err}") except: raise RequestError(f"Error occurred: {sys.exc_info()[1]}") finally: if not use_running_session: await session.close()
def __init__(self, endpoint: QnAMakerEndpoint, options: QnAMakerOptions = None, http_client: ClientSession = None, telemetry_client: BotTelemetryClient = None, log_personal_information: bool = None): if not isinstance(endpoint, QnAMakerEndpoint): raise TypeError( 'QnAMaker.__init__(): endpoint is not an instance of QnAMakerEndpoint' ) if endpoint.host.endswith('v2.0'): raise ValueError( 'v2.0 of QnA Maker service is no longer supported in the Bot Framework. Please upgrade your QnA Maker service at www.qnamaker.ai.' ) self._endpoint: str = endpoint self._is_legacy_protocol: bool = self._endpoint.host.endswith('v3.0') self._options = options or QnAMakerOptions() self._validate_options(self._options) instance_timeout = ClientTimeout(total=self._options.timeout / 1000) self._req_client = http_client or ClientSession( timeout=instance_timeout) self._telemetry_client: Union[ BotTelemetryClient, NullTelemetryClient] = telemetry_client or NullTelemetryClient() self._log_personal_information = log_personal_information or False
async def __fetch(pn: int) -> None: nonlocal net par = params.copy() par["pn"] = pn async with ClientSession(timeout=ClientTimeout( total=timeout)) as session: try: async with session.get( Crawler.__BASE_URL, params=par, headers=Crawler.__HEADERS, ) as res: if res.status == 200: text = await res.text() logger.debug(text) text = loads(text.replace(r"\'", ""), strict=False) for img in text["data"]: if "thumbURL" in img and img["thumbURL"] != "": urls.append( Crawler.solve_imgdata(img, original)) else: logger.debug("status" + str(res.status)) net = False except (aioClientError, asyncio.TimeoutError): net = False
async def assert_steady_rate_in_5_seconds(endpoint_to_check: str, average: int, period_sec: int, **_) -> float: """Creates a requests at a continuous rate without considering burst limits""" # run tests for at least 5 seconds max_rate = period_sec / average # reqs/ sec requests_to_make = int(math.ceil(max_rate * 5)) sleep_interval = max_rate log.info( "Steady rate params: sleep_interval=%s, max_rate=%s, requests_to_make=%s", sleep_interval, max_rate, requests_to_make, ) timeout = ClientTimeout(total=10, connect=1, sock_connect=1) async with ClientSession(timeout=timeout) as client: for i in range(requests_to_make): log.info("Request %s", i) result = await get_request_result(client, endpoint_to_check) assert is_rate_limit_reached(result) is False log.info("Sleeping for %s s", sleep_interval) await asyncio.sleep(sleep_interval) return sleep_interval
def __init__( self, endpoint: QnAMakerEndpoint, options: QnAMakerOptions = None, http_client: ClientSession = None, telemetry_client: BotTelemetryClient = None, log_personal_information: bool = None, ): super().__init__(log_personal_information, telemetry_client) if not isinstance(endpoint, QnAMakerEndpoint): raise TypeError( "QnAMaker.__init__(): endpoint is not an instance of QnAMakerEndpoint" ) self._endpoint: str = endpoint opt = options or QnAMakerOptions() self._validate_options(opt) instance_timeout = ClientTimeout(total=opt.timeout / 1000) self._http_client = http_client or ClientSession( timeout=instance_timeout) self.telemetry_client: Union[ BotTelemetryClient, NullTelemetryClient] = telemetry_client or NullTelemetryClient() self.log_personal_information = log_personal_information or False self._generate_answer_helper = GenerateAnswerUtils( self.telemetry_client, self._endpoint, options, self._http_client) self._active_learning_train_helper = TrainUtils( self._endpoint, self._http_client)
async def async_request(self, method: str, endpoint: str) -> dict: """Make a request against the SmartWeather API.""" use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout( total=DEFAULT_TIMEOUT)) try: async with session.request(method, f"{BASE_URL}/{endpoint}") as resp: resp.raise_for_status() data = await resp.json() return data except asyncio.TimeoutError as timeout_err: raise RequestError( "Request to endpoint timed out: {endpoint}") from timeout_err except ClientError as err: if "Unauthorized" in str(err): raise InvalidApiKey( "Your API Key is invalid or does not support this operation" ) from err if "Not Found" in str(err): raise ResultError("The Station ID does not exist") from err raise RequestError( f"Error requesting data from {endpoint}: {err}") from None finally: if not use_running_session: await session.close()
async def main(): queue = asyncio.Queue() async with ClientSession(timeout=ClientTimeout(total=10)) as sess: futures = [asyncio.ensure_future(crawl(sess, queue))] futures += [asyncio.ensure_future(scrap(sess, queue)) for _ in range(50)] await asyncio.wait(futures)
async def task(self) -> None: """Implements frontend signalization asynchronous task.""" api_key = self._config.api_key.get_secret_value() headers = {"Authorization": f"apikey {api_key}"} async with ClientSession(headers=headers, timeout=ClientTimeout(total=10)) as session: while True: message: Union[OPCMessage, HeartBeatMessage] try: message = await asyncio.wait_for(self._queue.get(), timeout=HEARTBEAT_TIMEOUT) except asyncio.TimeoutError: message = HeartBeatMessage() command = { "method": "publish", "params": { "channel": message.message_type.value, "data": message.frontend_data, }, } try: async with session.post(self._config.api_url, json=command) as resp: resp.raise_for_status() resp_data = await resp.json() if (error := resp_data.get("error")) is not None: _logger.error( "%s - Centrifugo API error: %s %s", self.purpose, error["code"], error["message"], ) except ClientError as err: _logger.error("%s error: %s", self.purpose, err)
async def _crawl_and_reduce( self, urls: Iterable[str], parse_fn: Optional[Callable[[str], T]] = None, callback_fn: Optional[Callable[[Optional[T]], None]] = None, ): # Create a semaphore to limit the number of concurrent tasks, a process-pool # executor to run `parse_fn` in parallel and a http client session for # asynchronous HTTP requests. sem = Semaphore(self.concurrent_tasks) pool = ProcessPoolExecutor(max_workers=self.num_parsing_processes) sess = ClientSession( headers=self.request_headers, timeout=ClientTimeout(total=self.request_timeout), ) futures = [] for url in urls: await sem.acquire() # Create a fetching future. f = asyncio.ensure_future( self._fetch_and_parse(sem, pool, sess, url, parse_fn)) # Add done-callback function to the future. if callback_fn is not None: f.add_done_callback(lambda f: callback_fn(f.result())) futures.append(f) # Wait for the tasks to be complete and close the http client session and # process-pool executor await asyncio.wait(futures) await sess.close() pool.shutdown(wait=True)
async def async_request(self, method: str, endpoint: str) -> dict: """Make a request against the Weatherbit API.""" use_running_session = self._session and not self._session.closed if use_running_session: session = self._session else: session = ClientSession(timeout=ClientTimeout( total=DEFAULT_TIMEOUT)) try: async with session.request(method, f"{BASE_URL}/{endpoint}") as resp: resp.raise_for_status() data = await resp.json() return data except asyncio.TimeoutError: raise RequestError( f"Request to endpoint timed out: {BASE_URL}/{endpoint}") except ClientError as err: if "Forbidden" in str(err): raise InvalidApiKey( "Your API Key is invalid or does not support this operation" ) else: raise RequestError( f"Error requesting data from {BASE_URL}: {str(err)}") except: raise RequestError(f"Error occurred: {sys.exc_info()[1]}") finally: if not use_running_session: await session.close()
async def async_set_data(change): nonlocal ready nonlocal queue queue = update(queue, change) if ready: ready = False while queue: while queue: payload = queue queue = {} # try: async with request( "GET", f"{url}/setAircon", params={"json": json.dumps(payload)}, timeout=ClientTimeout(total=4), ) as resp: data = await resp.json(content_type=None) # except ClientError as err: # raise UpdateFailed(err) if data["ack"] == False: ready = True raise Exception(data["reason"]) await asyncio.sleep(1) await coordinator.async_refresh( ) # Request refresh once queue is empty ready = ( True # Ready only once refresh has finished and queue is still empty ) return
def aiohttp_client_adapter( url: str, timeout: Optional[int] = None, sender: Optional[Callable[[ClientSession, Call, Type[T]], Awaitable[T]]] = None, ) -> AsyncContextManager[ClientAdapter]: if sender is None: client_timeout = ClientTimeout(total=timeout) async def s(session, call, type): async with session.post( url, data=dumps(converter.unstructure(call)), timeout=client_timeout, ) as resp: return converter.structure(loads(await resp.read()), type) else: s = sender @asynccontextmanager async def aiohttp_adapter() -> AsyncGenerator[ClientAdapter, None]: session = ClientSession() try: yield partial(s, session) finally: await session.close() return aiohttp_adapter()
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Bond from a config entry.""" host = entry.data[CONF_HOST] token = entry.data[CONF_ACCESS_TOKEN] bond = Bond(host=host, token=token, timeout=ClientTimeout(total=_API_TIMEOUT)) hub = BondHub(bond) try: await hub.setup() except (ClientError, AsyncIOTimeoutError, OSError) as error: raise ConfigEntryNotReady from error hass.data[DOMAIN][entry.entry_id] = hub if not entry.unique_id: hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id) device_registry = await dr.async_get_registry(hass) device_registry.async_get_or_create( config_entry_id=entry.entry_id, identifiers={(DOMAIN, hub.bond_id)}, manufacturer="Olibra", name=hub.bond_id, model=hub.target, sw_version=hub.fw_ver, ) for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component)) return True
async def _request_json(self, method, url, **kwargs): """Request json data for requests.""" if self.sma_sid: kwargs.setdefault("params", {}) kwargs["params"]["sid"] = self.sma_sid _LOGGER.debug("Sending %s request to %s: %s", method, url, kwargs) try: res = await self._aio_session.request( method, self._url + url, timeout=ClientTimeout(total=DEFAULT_TIMEOUT), **kwargs, ) except client_exceptions.ClientError as exc: raise SmaConnectionException( f"Could not connect to SMA at {self._url}" ) from exc try: res_json = await res.json() except (client_exceptions.ContentTypeError, json.decoder.JSONDecodeError): _LOGGER.warning("Request to %s did not return a valid json.", url) return {} return res_json or {}
async def fetch_all(output, modules): loop = asyncio.get_event_loop() tasks = [] fetched_modules = [] # In case of errors (Too many open files), just change limit_per_host connector = TCPConnector(limit=100, limit_per_host=0) async with ClientSession(loop=loop, timeout=ClientTimeout(total=TIMEOUT), connector=connector) as client: for filename, debug_id, code_file, code_id, has_code in modules: tasks.append(fetch_and_write(output, client, filename, debug_id)) if has_code: tasks.append( fetch_and_write(output, client, code_file, code_id)) res = await asyncio.gather(*tasks) res = iter(res) for filename, debug_id, code_file, code_id, has_code in modules: fetched_pdb = next(res) if has_code: has_code = next(res) if fetched_pdb: fetched_modules.append( (filename, debug_id, code_file, code_id, has_code)) return fetched_modules
async def run(cod_stops, fetch_conf): """Async function that generates a aiohttp ClientSession and fetches the given stops. Arguments: cod_stops (list): List of stop codes in CRTM's format (e.g. 8_17491). fetch_conf (dict): Dictionary with configuration parameters for fetching the content. Returns: list: The responses. """ tasks = [] # Fetch all responses within one Client session, # keep connection alive for all requests. connector = TCPConnector(limit=fetch_conf['max_connections']) timeout = ClientTimeout(total=fetch_conf['timeout']) async with ClientSession(connector=connector, timeout=timeout) as session: for cod_stop in cod_stops: task = asyncio.ensure_future(fetch(cod_stop, session, fetch_conf)) tasks.append(task) responses = await asyncio.gather(*tasks) # you now have all response bodies in this variable return responses
def __init__(self, settings: Settings, loop): self._settings = settings self._client = ClientSession( timeout=ClientTimeout(total=30), loop=loop, auth=BasicAuth('nosht', settings.donorfy_access_key), )
async def async_get_response_from_post_request( endpoint_uri: URI, *args: Any, **kwargs: Any ) -> ClientResponse: kwargs.setdefault('timeout', ClientTimeout(10)) session = await get_async_session(endpoint_uri) response = await session.post(endpoint_uri, *args, **kwargs) return response
async def check_new_version_api(self, version_check_url): try: async with ClientSession(raise_for_status=True) as session: response = await session.get( version_check_url, timeout=ClientTimeout(total=VERSION_CHECK_TIMEOUT)) response_dict = await response.json(content_type=None) version = response_dict['name'][1:] if LooseVersion(version) > LooseVersion(version_id): self.session.notifier.notify(NTFY.TRIBLER_NEW_VERSION, version) return True return False except (ServerConnectionError, ClientConnectionError) as e: self._logger.error( "Error when performing version check request: %s", e) return False except ClientResponseError as e: self._logger.warning( "Got response code %s when performing version check request", e.status) return False except ContentTypeError: self._logger.warning("Response was not in JSON format") return False except asyncio.TimeoutError: self._logger.warning("Checking for new version failed for %s", version_check_url) return False except ValueError as ve: raise ValueError( f"Failed to parse Tribler version response.\nError:{ve}")