async def _request(self, path, qs={}, payload=''): async with ClientSession() as session: async with session.get(headers=self.HEADERS, url=self.BASE_URL, path=path, params=qs, data=payload) as r: r = await r.read() print(r)
async def s3_client(loop, s3_url: URL): async with ClientSession(raise_for_status=True) as session: yield S3Client(url=s3_url, session=session)
async def start(self): """Start the outbound transport.""" self.client_session = ClientSession(cookie_jar=DummyCookieJar(), trust_env=True) return self
async def fetch_user_data_from_api(username): async with ClientSession() as session: response_as_dict = await _get_response('user', username, session) return response_as_dict
async def request( # pylint: disable=too-many-branches self, method: str, endpoint: str, **kwargs ) -> dict: """Make an API request.""" if ( self._access_token_expire and datetime.now() >= self._access_token_expire and not self._actively_refreshing ): _LOGGER.debug( "Need to refresh access token (expiration: %s)", self._access_token_expire, ) self._actively_refreshing = True await self.refresh_access_token(self._refresh_token) kwargs.setdefault("headers", {}) if self._access_token: kwargs["headers"]["Authorization"] = f"Bearer {self._access_token}" kwargs["headers"]["Content-Type"] = "application/json; charset=utf-8" kwargs["headers"]["Host"] = API_URL_HOSTNAME kwargs["headers"]["User-Agent"] = DEFAULT_USER_AGENT 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)) async with session.request( method, f"{API_URL_BASE}/{endpoint}", **kwargs ) as resp: try: data = await resp.json(content_type=None) except JSONDecodeError: message = await resp.text() data = {"error": message} _LOGGER.debug("Data received from /%s: %s", endpoint, data) try: resp.raise_for_status() except ClientError as err: if data.get("error") == "mfa_required": return data if data.get("type") == "NoRemoteManagement": raise EndpointUnavailable( f"Endpoint unavailable in plan: {endpoint}" ) from None if "401" in str(err): if self._actively_refreshing: raise InvalidCredentialsError( "Repeated 401s despite refreshing access token" ) from None if self._refresh_token: _LOGGER.info("401 detected; attempting refresh token") self._access_token_expire = datetime.now() return await self.request(method, endpoint, **kwargs) raise InvalidCredentialsError("Invalid username/password") from None if "403" in str(err): raise InvalidCredentialsError("Invalid username/password") from None raise RequestError( f"There was an error while requesting /{endpoint}: {err}" ) from None finally: if not use_running_session: await session.close() return data
def __init__(self) -> None: self.client = ClientSession()
command_prefix=when_mentioned_or( "self.", "bot." ), activity=Game( name="Help: bot.help()" ), help_attrs={ "name": "help()", "aliases": ["help"] }, formatter=Formatter(), case_insensitive=True ) # Global aiohttp session for all cogs - uses asyncio for DNS resolution instead of threads, so we don't *spam threads* bot.http_session = ClientSession(connector=TCPConnector(resolver=AsyncResolver())) # Internal/debug bot.load_extension("bot.cogs.logging") bot.load_extension("bot.cogs.security") bot.load_extension("bot.cogs.events") # Commands, etc bot.load_extension("bot.cogs.bot") bot.load_extension("bot.cogs.cogs") # Local setups usually don't have the clickup key set, # and loading the cog would simply spam errors in the console. if CLICKUP_KEY is not None: bot.load_extension("bot.cogs.clickup")
async def get_html(url: str): async with ClientSession() as session: async with session.get(url) as res: print(res.status) print(await res.text())
async def get_bytes(url): async with ClientSession() as session: async with session.get(url) as response: return await response.read()
async def readme( self, ctx: Context, operand: str = "", channel_id: str = "", msg_send_interval: int = 0, ): """ Allows generating, sending and manipulation of JSON file containing the info needed to create and send the embeds for the #readme channel. Only ROOT_ROLE_ID users have the permissions need to use this command. """ operand = operand.lower() # The supplied operand is incorrect. if not (operand in README_SEND_ALIASES + README_RECV_ALIASES): incorrect_operand_embed = Embed( colour=0x673AB7, description=":shrug: **Invalid readme operand supplied.**", ) await ctx.message.delete() await ctx.send(embed=incorrect_operand_embed) # User missed out the channel_id for certain commands. elif channel_id == "" and operand in README_SEND_ALIASES: misssing_channel_embed = Embed( colour=0xFF5722, description= ":facepalm: **Whoops, you missed out the channel ID! Try again.**", ) await ctx.message.delete() await ctx.send(embed=misssing_channel_embed) # Process the request. else: # Let's create a series of #readme-capable embeds. If something is uploaded, # It will attempt to use that file for the readme, if omitted, it will use # the default JSONifed readme file and send that into the channel instead. if operand in README_SEND_ALIASES: try: # Much pain was had fixing this. Please, get some help and install mypy type checking. channel_id: int = int(channel_id[2:-1] if channel_id[0] == "<" else channel_id) usr_confirmation_embed = Embed( colour=0x4CAF50, description= ":white_check_mark: **Creating readme using uploaded config file.**", ) # The user has uploaded a config. if ctx.message.attachments != []: json_file_location = [ _.url for _ in ctx.message.attachments ][0] # GETs the attachment data. async with ClientSession() as session: async with session.get( json_file_location) as response: if response.status == 200: resp_text = await response.text() json_config = load(StringIO(resp_text)) await ctx.send(embed=usr_confirmation_embed) # No config uploaded, just use default config file. else: with open("cdbot/data/readme.json", "rb") as default_json: json_config = load(default_json) usr_confirmation_embed.description = ( ":ballot_box_with_check: " "**Creating readme using default config file.**") await ctx.send(embed=usr_confirmation_embed) await ctx.message.delete() for section in json_config: # Initialise our message and embed variables each loop. # This is to prevent leftover data from being re-sent. msg_content, current_embed = None, None # The part which handles general messages. if "content" in json_config[section]: msg_content = json_config[section]["content"] # We have an embed. Call in the Seahawks. if "embed" in json_config[section]: current_embed = Embed() msg_embed = json_config[section]["embed"] if "title" in msg_embed: current_embed.title = msg_embed["title"] if "description" in msg_embed: current_embed.description = msg_embed[ "description"] if "color" in msg_embed: current_embed.colour = Colour( int(msg_embed["color"], 16)) # Parse the fields, if there are any. if "fields" in msg_embed: for current_field in msg_embed["fields"]: # Add the fields to the current embed. current_embed.add_field( name=current_field["name"], value=current_field["value"], ) # Send the message. requested_channel = self.bot.get_channel(channel_id) if msg_content is not None and current_embed is None: await requested_channel.send(content=msg_content) elif current_embed is not None and msg_content is None: await requested_channel.send(embed=current_embed) else: await requested_channel.send(content=msg_content, embed=current_embed) # User has requested a delay between each message being sent. if 0 < msg_send_interval < 901: await sleep(msg_send_interval) # Send the trailing embed message constant. await requested_channel.send(content=END_README_MESSAGE) except (Exception): parse_fail_embed = Embed( colour=0x673AB7, description= ":x: **Error parsing JSON file, please ensure its valid!**", ) await ctx.message.delete() await ctx.send(embed=parse_fail_embed) # Pull the readme JSON constant files and slide it into the user's DMs. elif operand in README_RECV_ALIASES: # Slide it to the user's DMs. requesting_user = await self.bot.fetch_user( ctx.message.author.id) await requesting_user.send( content="Hey, here's your config file!", file=File(fp="cdbot/data/readme.json", filename="readme.json"), ) await ctx.message.delete() await ctx.send(embed=Embed( colour=0x009688, description=":airplane: **Flying in, check your DMs!**", ))
async def get(url): # print ('start --------url',n) async with ClientSession() as session: async with session.get(url) as resp: return await resp.text()
async def on_startup(app): app.http_session_pool = ClientSession(timeout=ClientTimeout(total=30)) app.redis_connection = redis.Redis(host=app['REDIS_HOST'], port=app['REDIS_PORT'])
async def _http_client(app: web.Application): session = ClientSession() app['client'] = session yield await session.close()
async def upload(data: bytes, mimetype: str, filename: str) -> str: url = upload_url.with_query({"filename": filename}) headers = {"Content-Type": mimetype, "Authorization": f"Bearer {access_token}"} async with ClientSession() as sess, sess.post(url, data=data, headers=headers) as resp: return (await resp.json())["content_uri"]
async def test_github_get_statuses(): ref = "129ae457d5cd404ec76ab51ae70dbc137b4aae6d" async with ClientSession() as client_session: status = await h.github_get_status(client_session, TEST_OWNER, TEST_REPO, ref) assert status == 'failure'
async def single_command(self, cmd, vin): """Initialize connection and execute as single command.""" success = False self._session = ClientSession() self._ctrl = Controller( self._session, self._config["username"], self._config["password"], self._config["device_id"], self._config["pin"], self._config["device_name"], ) if await self._connect(interactive=False, vin=vin): try: if cmd == "status": success = await self._fetch() pprint(self._car_data) elif cmd == "lock": success = await self._ctrl.lock(self._current_vin) elif cmd == "unlock": success = await self._ctrl.unlock(self._current_vin) elif cmd == "lights": success = await self._ctrl.lights(self._current_vin) elif cmd == "horn": success = await self._ctrl.horn(self._current_vin) elif cmd == "locate": success = await self._ctrl.update(self._current_vin) await self._fetch() pprint(self._car_data.get("location")) elif cmd == "remote_start": if self._config.get("hvac") is None: LOGGER.error( "Remote start settings not found in config file. Configure settings interactively first" ) success = await self._ctrl.remote_start( self._current_vin, self._config["hvac"]["temp"], self._config["hvac"]["mode"], self._config["hvac"]["left_seat"], self._config["hvac"]["right_seat"], self._config["hvac"]["rear_defrost"], self._config["hvac"]["speed"], self._config["hvac"]["recirculate"], self._config["hvac"]["rear_ac"], ) elif cmd == "remote_stop": success = await self._ctrl.remote_stop(self._current_vin) elif cmd == "charge": success = await self._ctrl.charge_start(self._current_vin) else: LOGGER.error("Unsupported command") except SubaruException as exc: LOGGER.error("SubaruException caught: %s", exc.message) if success: print( f"{OK}Command '{cmd}' completed for {self._current_vin}{ENDC}") sys.exit(0) else: print( f"{FAIL}Command '{cmd}' failed for {self._current_vin}{ENDC}") sys.exit(1)
async def test_github_get_repo_topics(): async with ClientSession() as client_session: topics = await h.github_get_repo_topics(client_session, TEST_OWNER, TEST_REPO) assert len(topics) > 1 assert "testrepo" in topics
async def get_lessons(self, ruz_room_id: str): """ Get lessons in room for a specified period """ needed_date = (datetime.today() + timedelta(days=self.period)).strftime("%Y.%m.%d") today = datetime.today().strftime("%Y.%m.%d") params = dict(fromdate=today, todate=needed_date, auditoriumoid=str(ruz_room_id)) async with ClientSession() as session: res = await session.get(f"{self.url}/lessons", params=params) async with res: res = await res.json(content_type=None) lessons = [] for class_ in res: lesson = {} date = class_.pop("date") date = date.split(".") lesson["date"] = "-".join(date) lesson["start_time"] = class_.pop("beginLesson") lesson["end_time"] = class_.pop("endLesson") lesson["summary"] = class_["discipline"] lesson["location"] = f"{class_['auditorium']}/{class_['building']}" for key in class_: new_key = f"ruz_{camel_to_snake(key)}" lesson[new_key] = class_[key] lesson["ruz_url"] = lesson["ruz_url1"] if lesson["ruz_group"] is not None: stream = lesson["ruz_group"].split("#")[0] grp_emails = await self.nvr_api.get_course_emails(stream) if grp_emails != []: lesson["grp_emails"] = grp_emails else: logger.warning(f"Stream: {stream} has no groups") else: stream = "" lesson["course_code"] = stream lesson["description"] = ( f"Поток: {stream}\n" f"Преподаватель: {lesson['ruz_lecturer']}\n" f"Тип занятия: {lesson['ruz_kind_of_work']}\n") if lesson["ruz_url"]: lesson["description"] += f"URL: {lesson['ruz_url']}\n" if lesson.get("ruz_lecturer_email"): # None or "" lesson["miem_lecturer_email"] = ( lesson["ruz_lecturer_email"].split("@")[0] + "@miem.hse.ru") lessons.append(lesson) return lessons
async def _bulk(): loop = asyncio.get_event_loop() semaphore = Semaphore(self.service.max_connection) async with ClientSession(headers=self.service.headers_upload) as session: tasks = (_create_task(x, loop, semaphore, session) for x in paths) return await asyncio.gather(*tasks)
def __init__(self, *args, **kwargs): if MockClientSession.session is None: MockClientSession.session = ClientSession(*args, **kwargs)
async def fetch_post_data_from_api(unique_id): async with ClientSession() as session: response_as_dict = await _get_response('post', unique_id, session) return response_as_dict
def __init__( self, ident: str, http_port: int, admin_port: int, internal_host: str = None, external_host: str = None, genesis_data: str = None, seed: str = "random", label: str = None, color: str = None, prefix: str = None, tails_server_base_url: str = None, timing: bool = False, timing_log: str = None, postgres: bool = None, revocation: bool = False, extra_args=None, **params, ): self.ident = ident self.http_port = http_port self.admin_port = admin_port self.internal_host = internal_host or DEFAULT_INTERNAL_HOST self.external_host = external_host or DEFAULT_EXTERNAL_HOST self.genesis_data = genesis_data self.label = label or ident self.color = color self.prefix = prefix self.timing = timing self.timing_log = timing_log self.postgres = DEFAULT_POSTGRES if postgres is None else postgres self.tails_server_base_url = tails_server_base_url self.extra_args = extra_args self.trace_enabled = TRACE_ENABLED self.trace_target = TRACE_TARGET self.trace_tag = TRACE_TAG self.external_webhook_target = WEBHOOK_TARGET self.admin_url = f"http://{self.internal_host}:{admin_port}" if AGENT_ENDPOINT: self.endpoint = AGENT_ENDPOINT elif RUN_MODE == "pwd": self.endpoint = f"http://{self.external_host}".replace( "{PORT}", str(http_port)) else: self.endpoint = f"http://{self.external_host}:{http_port}" self.webhook_port = None self.webhook_url = None self.webhook_site = None self.params = params self.proc = None self.client_session: ClientSession = ClientSession() rand_name = str(random.randint(100_000, 999_999)) self.seed = (("my_seed_000000000000000000000000" + rand_name)[-32:] if seed == "random" else seed) self.storage_type = params.get("storage_type") self.wallet_type = params.get("wallet_type", "indy") self.wallet_name = (params.get("wallet_name") or self.ident.lower().replace(" ", "") + rand_name) self.wallet_key = params.get("wallet_key") or self.ident + rand_name self.did = None self.wallet_stats = []
async def registration(request): form = await forms.get('Registration').init(request) db = cfg.STORAGE while request.method == 'POST' and await form.validate(): user_ip = get_client_ip(request) if cfg.INVISIBLE_RECAPTCHA_ENABLED: post_dict = await request.post() recaptcha_response = post_dict.get('g-recaptcha-response', None) if recaptcha_response is None: log.error('Recaptcha code is not found') flash.error(request, cfg.MSG_RECAPTCHA_INCORRECT) return redirect('auth_registration') async with ClientSession(loop=request.app.loop) as session: query = { 'secret': cfg.INVISIBLE_RECAPTCHA_SITE_SECRET, 'response': recaptcha_response, 'remote_ip': user_ip } async with session.post( 'https://www.google.com/recaptcha/api/siteverify', params=query) as response: try: json_response = await response.json() success = json_response['success'] if success is not True: flash.error(request, cfg.MSG_RECAPTCHA_INCORRECT) return redirect('auth_registration') except Exception as e: log.error('Recaptcha verify failed', exc_info=e) user = await db.create_user({ 'name': form.email.data.split('@')[0], 'email': form.email.data, 'password': encrypt_password(form.password.data), 'status': ('confirmation' if cfg.REGISTRATION_CONFIRMATION_REQUIRED else 'active'), 'created_ip': user_ip, }) if not cfg.REGISTRATION_CONFIRMATION_REQUIRED: await authorize_user(request, user) flash.success(request, cfg.MSG_LOGGED_IN) return redirect(cfg.LOGIN_REDIRECT) confirmation = await db.create_confirmation(user, 'registration') link = await make_confirmation_link(request, confirmation) try: await render_and_send_mail( request, form.email.data, common_themed('registration_email.html'), { 'auth': { 'cfg': cfg, }, 'host': request.host, 'link': link, }) except Exception as e: log.error('Can not send email', exc_info=e) form.email.errors.append(cfg.MSG_CANT_SEND_MAIL) await db.delete_confirmation(confirmation) await db.delete_user(user) break return redirect('auth_registration_requested') return render_template( themed('registration.html'), request, { 'auth': { 'url_for': url_for, 'cfg': cfg, 'form': form, 'social_url': social_url(request), } })
def go(): self.sess = ClientSession() self.app = yield from self.initialize_application() yield from self.create_server()
def __init__(self, **kwargs): super().__init__(**kwargs) self.http_session = ClientSession(connector=TCPConnector( resolver=AsyncResolver(), family=socket.AF_INET))
# Sending dat via UDP from socket import socket, AF_INET, SOCK_DGRAM s = socket(AF_INET, SOCK_DGRAM) msg = ("Hello you there!").encode( 'utf-8') # socket.sendto() takes bytes as input, hence we must # encode the string first. s.sendto(msg, ('localhost', 6667)) # Web sockets. # Simple Echo with aiohttp import asyncio from aiohttp import ClientSession with ClientSession() as session: async def hello_world(): websocket = await session.ws_connect("wss://echo.websocket.org") websocket.send_str("Hello, world!") print("Received:", (await websocket.receive()).data) await websocket.close() loop = asyncio.get_event_loop() loop.run_until_complete(hello_world()) # Wrapper Class with aiohttp. import asyncio from aiohttp import ClientSession class EchoWebSocket(ClientSession): URL = "wss://echo.websocket.org"
async def _async_authenticated_post(cls, url: str, params_data: dict, cloud_creds: Optional[MerossCloudCreds] = None ) -> dict: nonce = _generate_nonce(16) timestamp_millis = int(round(time.time() * 1000)) login_params = _encode_params(params_data) # Generate the md5-hash (called signature) m = hashlib.md5() datatosign = '%s%s%s%s' % (_SECRET, timestamp_millis, nonce, login_params) m.update(datatosign.encode("utf8")) md5hash = m.hexdigest() headers = { "Authorization": "Basic" if cloud_creds is None else "Basic %s" % cloud_creds.token, "vender": "Meross", "AppVersion": "1.3.0", "AppLanguage": "EN", "User-Agent": "okhttp/3.6.0" } payload = { 'params': login_params, 'sign': md5hash, 'timestamp': timestamp_millis, 'nonce': nonce } # Perform the request. _LOGGER.debug(f"Performing HTTP request against {url}, headers: {headers}, post data: {payload}") async with ClientSession() as session: async with session.post(url, data=payload, headers=headers) as response: _LOGGER.debug(f"Response Status Code: {response.status}") # Check if that is ok. if response.status != 200: raise AuthenticatedPostException("Failed request to API. Response code: %d" % str(response.status)) # Save returned value jsondata = await response.json() code = jsondata.get('apiStatus') error = None try: error = ErrorCodes(code) except ValueError as e: raise AuthenticatedPostException(f"Unknown/Unhandled response code received from API. " f"Response was: {jsondata}") finally: if error is None: _LOGGER.error(f"Could not parse error code {code}.") elif error == ErrorCodes.CODE_NO_ERROR: return jsondata.get("data") elif error == ErrorCodes.CODE_TOKEN_EXPIRED: raise TokenExpiredException("The provided token has expired") elif error == ErrorCodes.CODE_TOO_MANY_TOKENS: raise TooManyTokensException("You have issued too many tokens without logging out and your " "account might have been temporarly disabled.") elif error in [ErrorCodes.CODE_WRONG_CREDENTIALS, ErrorCodes.CODE_UNEXISTING_ACCOUNT]: raise BadLoginException("Invalid username/Password combination") else: _LOGGER.error(f"Received non-ok API status code: {error.name}. " f"Failed request to API. Response was: {jsondata}") raise HttpApiError(error)
async def main(links): async with ClientSession() as session: tasks = [] for link in links: tasks.append(fetch(session, link)) return await asyncio.gather(*tasks)
async def http_client(): async with ClientSession() as session: yield AsyncHttpClient(session)
async def start(self, *args, **kwargs): self.session = ClientSession() await super().start(self.config["bot_key"], *args, **kwargs)