diff --git a/platformio/account/commands/destroy.py b/platformio/account/commands/destroy.py index 5a4f4dd6..aabd566f 100644 --- a/platformio/account/commands/destroy.py +++ b/platformio/account/commands/destroy.py @@ -19,18 +19,18 @@ from platformio.account.client import AccountClient, AccountNotAuthorized @click.command("destroy", short_help="Destroy account") def account_destroy_cmd(): - client = AccountClient() - click.confirm( - "Are you sure you want to delete the %s user account?\n" - "Warning! All linked data will be permanently removed and can not be restored." - % client.get_logged_username(), - abort=True, - ) - client.destroy_account() - try: - client.logout() - except AccountNotAuthorized: - pass + with AccountClient() as client: + click.confirm( + "Are you sure you want to delete the %s user account?\n" + "Warning! All linked data will be permanently removed and can not be restored." + % client.get_logged_username(), + abort=True, + ) + client.destroy_account() + try: + client.logout() + except AccountNotAuthorized: + pass click.secho( "User account has been destroyed.", fg="green", diff --git a/platformio/account/commands/forgot.py b/platformio/account/commands/forgot.py index 4af8f5fa..1cd987fb 100644 --- a/platformio/account/commands/forgot.py +++ b/platformio/account/commands/forgot.py @@ -20,8 +20,8 @@ from platformio.account.client import AccountClient @click.command("forgot", short_help="Forgot password") @click.option("--username", prompt="Username or email") def account_forgot_cmd(username): - client = AccountClient() - client.forgot_password(username) + with AccountClient() as client: + client.forgot_password(username) click.secho( "If this account is registered, we will send the " "further instructions to your email.", diff --git a/platformio/account/commands/login.py b/platformio/account/commands/login.py index b9471bfd..7c81fab9 100644 --- a/platformio/account/commands/login.py +++ b/platformio/account/commands/login.py @@ -21,6 +21,6 @@ from platformio.account.client import AccountClient @click.option("-u", "--username", prompt="Username or email") @click.option("-p", "--password", prompt=True, hide_input=True) def account_login_cmd(username, password): - client = AccountClient() - client.login(username, password) + with AccountClient() as client: + client.login(username, password) click.secho("Successfully logged in!", fg="green") diff --git a/platformio/account/commands/logout.py b/platformio/account/commands/logout.py index 243a7f14..d778a4b6 100644 --- a/platformio/account/commands/logout.py +++ b/platformio/account/commands/logout.py @@ -19,6 +19,6 @@ from platformio.account.client import AccountClient @click.command("logout", short_help="Log out of PlatformIO Account") def account_logout_cmd(): - client = AccountClient() - client.logout() + with AccountClient() as client: + client.logout() click.secho("Successfully logged out!", fg="green") diff --git a/platformio/account/commands/password.py b/platformio/account/commands/password.py index 4f129c08..300f94d1 100644 --- a/platformio/account/commands/password.py +++ b/platformio/account/commands/password.py @@ -21,6 +21,6 @@ from platformio.account.client import AccountClient @click.option("--old-password", prompt=True, hide_input=True) @click.option("--new-password", prompt=True, hide_input=True, confirmation_prompt=True) def account_password_cmd(old_password, new_password): - client = AccountClient() - client.change_password(old_password, new_password) + with AccountClient() as client: + client.change_password(old_password, new_password) click.secho("Password successfully changed!", fg="green") diff --git a/platformio/account/commands/register.py b/platformio/account/commands/register.py index 6fc20ce6..cedced1a 100644 --- a/platformio/account/commands/register.py +++ b/platformio/account/commands/register.py @@ -43,8 +43,8 @@ from platformio.account.validate import ( @click.option("--firstname", prompt=True) @click.option("--lastname", prompt=True) def account_register_cmd(username, email, password, firstname, lastname): - client = AccountClient() - client.registration(username, email, password, firstname, lastname) + with AccountClient() as client: + client.registration(username, email, password, firstname, lastname) click.secho( "An account has been successfully created. " "Please check your mail to activate your account and verify your email address.", diff --git a/platformio/account/commands/show.py b/platformio/account/commands/show.py index f107f4f5..6584d3df 100644 --- a/platformio/account/commands/show.py +++ b/platformio/account/commands/show.py @@ -25,8 +25,8 @@ from platformio.account.client import AccountClient @click.option("--offline", is_flag=True) @click.option("--json-output", is_flag=True) def account_show_cmd(offline, json_output): - client = AccountClient() - info = client.get_account_info(offline) + with AccountClient() as client: + info = client.get_account_info(offline) if json_output: click.echo(json.dumps(info)) return diff --git a/platformio/account/commands/token.py b/platformio/account/commands/token.py index 0f06e367..99835ca0 100644 --- a/platformio/account/commands/token.py +++ b/platformio/account/commands/token.py @@ -24,8 +24,8 @@ from platformio.account.client import AccountClient @click.option("--regenerate", is_flag=True) @click.option("--json-output", is_flag=True) def account_token_cmd(password, regenerate, json_output): - client = AccountClient() - auth_token = client.auth_token(password, regenerate) + with AccountClient() as client: + auth_token = client.auth_token(password, regenerate) if json_output: click.echo(json.dumps({"status": "success", "result": auth_token})) return diff --git a/platformio/account/commands/update.py b/platformio/account/commands/update.py index b3868938..f337c313 100644 --- a/platformio/account/commands/update.py +++ b/platformio/account/commands/update.py @@ -25,8 +25,8 @@ from platformio.account.validate import validate_email, validate_username @click.option("--firstname") @click.option("--lastname") def account_update_cmd(current_password, **kwargs): - client = AccountClient() - profile = client.get_profile() + with AccountClient() as client: + profile = client.get_profile() new_profile = profile.copy() if not any(kwargs.values()): for field in profile: diff --git a/platformio/account/org/commands/add.py b/platformio/account/org/commands/add.py index 55e658e7..9c2b4037 100644 --- a/platformio/account/org/commands/add.py +++ b/platformio/account/org/commands/add.py @@ -25,8 +25,8 @@ from platformio.account.client import AccountClient "username", ) def org_add_cmd(orgname, username): - client = AccountClient() - client.add_org_owner(orgname, username) + with AccountClient() as client: + client.add_org_owner(orgname, username) return click.secho( "The new owner `%s` has been successfully added to the `%s` organization." % (username, orgname), diff --git a/platformio/account/org/commands/create.py b/platformio/account/org/commands/create.py index b0c86bf5..e8fb3d10 100644 --- a/platformio/account/org/commands/create.py +++ b/platformio/account/org/commands/create.py @@ -30,8 +30,8 @@ from platformio.account.validate import validate_email, validate_orgname "--displayname", ) def org_create_cmd(orgname, email, displayname): - client = AccountClient() - client.create_org(orgname, email, displayname) + with AccountClient() as client: + client.create_org(orgname, email, displayname) return click.secho( "The organization `%s` has been successfully created." % orgname, fg="green", diff --git a/platformio/account/org/commands/destroy.py b/platformio/account/org/commands/destroy.py index 2a202f9a..56f7ea55 100644 --- a/platformio/account/org/commands/destroy.py +++ b/platformio/account/org/commands/destroy.py @@ -20,14 +20,14 @@ from platformio.account.client import AccountClient @click.command("destroy", short_help="Destroy organization") @click.argument("orgname") def org_destroy_cmd(orgname): - client = AccountClient() - click.confirm( - "Are you sure you want to delete the `%s` organization account?\n" - "Warning! All linked data will be permanently removed and can not be restored." - % orgname, - abort=True, - ) - client.destroy_org(orgname) + with AccountClient() as client: + click.confirm( + "Are you sure you want to delete the `%s` organization account?\n" + "Warning! All linked data will be permanently removed and can not be restored." + % orgname, + abort=True, + ) + client.destroy_org(orgname) return click.secho( "Organization `%s` has been destroyed." % orgname, fg="green", diff --git a/platformio/account/org/commands/list.py b/platformio/account/org/commands/list.py index 471814ea..4f3509f3 100644 --- a/platformio/account/org/commands/list.py +++ b/platformio/account/org/commands/list.py @@ -23,8 +23,8 @@ from platformio.account.client import AccountClient @click.command("list", short_help="List organizations and their members") @click.option("--json-output", is_flag=True) def org_list_cmd(json_output): - client = AccountClient() - orgs = client.list_orgs() + with AccountClient() as client: + orgs = client.list_orgs() if json_output: return click.echo(json.dumps(orgs)) if not orgs: diff --git a/platformio/account/org/commands/remove.py b/platformio/account/org/commands/remove.py index 9dee462a..0815568e 100644 --- a/platformio/account/org/commands/remove.py +++ b/platformio/account/org/commands/remove.py @@ -25,8 +25,8 @@ from platformio.account.client import AccountClient "username", ) def org_remove_cmd(orgname, username): - client = AccountClient() - client.remove_org_owner(orgname, username) + with AccountClient() as client: + client.remove_org_owner(orgname, username) return click.secho( "The `%s` owner has been successfully removed from the `%s` organization." % (username, orgname), diff --git a/platformio/account/org/commands/update.py b/platformio/account/org/commands/update.py index 86e7cffa..d342f811 100644 --- a/platformio/account/org/commands/update.py +++ b/platformio/account/org/commands/update.py @@ -31,8 +31,8 @@ from platformio.account.validate import validate_email, validate_orgname ) @click.option("--displayname") def org_update_cmd(cur_orgname, **kwargs): - client = AccountClient() - org = client.get_org(cur_orgname) + with AccountClient() as client: + org = client.get_org(cur_orgname) new_org = { key: value if value is not None else org[key] for key, value in kwargs.items() } diff --git a/platformio/account/team/commands/add.py b/platformio/account/team/commands/add.py index 854675e6..1b749731 100644 --- a/platformio/account/team/commands/add.py +++ b/platformio/account/team/commands/add.py @@ -29,8 +29,8 @@ from platformio.account.validate import validate_orgname_teamname ) def team_add_cmd(orgname_teamname, username): orgname, teamname = orgname_teamname.split(":", 1) - client = AccountClient() - client.add_team_member(orgname, teamname, username) + with AccountClient() as client: + client.add_team_member(orgname, teamname, username) return click.secho( "The new member %s has been successfully added to the %s team." % (username, teamname), diff --git a/platformio/account/team/commands/create.py b/platformio/account/team/commands/create.py index 164ca6ac..2b22b55a 100644 --- a/platformio/account/team/commands/create.py +++ b/platformio/account/team/commands/create.py @@ -29,8 +29,8 @@ from platformio.account.validate import validate_orgname_teamname ) def team_create_cmd(orgname_teamname, description): orgname, teamname = orgname_teamname.split(":", 1) - client = AccountClient() - client.create_team(orgname, teamname, description) + with AccountClient() as client: + client.create_team(orgname, teamname, description) return click.secho( "The team %s has been successfully created." % teamname, fg="green", diff --git a/platformio/account/team/commands/destroy.py b/platformio/account/team/commands/destroy.py index 7cd084a1..8745d13e 100644 --- a/platformio/account/team/commands/destroy.py +++ b/platformio/account/team/commands/destroy.py @@ -32,8 +32,8 @@ def team_destroy_cmd(orgname_teamname): ), abort=True, ) - client = AccountClient() - client.destroy_team(orgname, teamname) + with AccountClient() as client: + client.destroy_team(orgname, teamname) return click.secho( "The team %s has been successfully destroyed." % teamname, fg="green", diff --git a/platformio/account/team/commands/list.py b/platformio/account/team/commands/list.py index d0395d7c..0118dff8 100644 --- a/platformio/account/team/commands/list.py +++ b/platformio/account/team/commands/list.py @@ -24,19 +24,22 @@ from platformio.account.client import AccountClient @click.argument("orgname", required=False) @click.option("--json-output", is_flag=True) def team_list_cmd(orgname, json_output): - client = AccountClient() - data = {} - if not orgname: - for item in client.list_orgs(): - teams = client.list_teams(item.get("orgname")) - data[item.get("orgname")] = teams - else: - teams = client.list_teams(orgname) - data[orgname] = teams + with AccountClient() as client: + data = {} + if not orgname: + for item in client.list_orgs(): + teams = client.list_teams(item.get("orgname")) + data[item.get("orgname")] = teams + else: + teams = client.list_teams(orgname) + data[orgname] = teams + if json_output: return click.echo(json.dumps(data[orgname] if orgname else data)) + if not any(data.values()): return click.secho("You do not have any teams.", fg="yellow") + for org_name, teams in data.items(): for team in teams: click.echo() diff --git a/platformio/account/team/commands/remove.py b/platformio/account/team/commands/remove.py index 73c79602..f28db8d4 100644 --- a/platformio/account/team/commands/remove.py +++ b/platformio/account/team/commands/remove.py @@ -27,8 +27,8 @@ from platformio.account.validate import validate_orgname_teamname @click.argument("username") def team_remove_cmd(orgname_teamname, username): orgname, teamname = orgname_teamname.split(":", 1) - client = AccountClient() - client.remove_team_member(orgname, teamname, username) + with AccountClient() as client: + client.remove_team_member(orgname, teamname, username) return click.secho( "The %s member has been successfully removed from the %s team." % (username, teamname), diff --git a/platformio/account/team/commands/update.py b/platformio/account/team/commands/update.py index c4c2f8b4..8ecc8a17 100644 --- a/platformio/account/team/commands/update.py +++ b/platformio/account/team/commands/update.py @@ -34,8 +34,8 @@ from platformio.account.validate import validate_orgname_teamname, validate_team ) def team_update_cmd(orgname_teamname, **kwargs): orgname, teamname = orgname_teamname.split(":", 1) - client = AccountClient() - team = client.get_team(orgname, teamname) + with AccountClient() as client: + team = client.get_team(orgname, teamname) new_team = { key: value if value is not None else team[key] for key, value in kwargs.items() } diff --git a/platformio/home/rpc/handlers/account.py b/platformio/home/rpc/handlers/account.py index fcbc85ba..0c715115 100644 --- a/platformio/home/rpc/handlers/account.py +++ b/platformio/home/rpc/handlers/account.py @@ -21,4 +21,5 @@ class AccountRPC(BaseRPCHandler): @staticmethod def call_client(method, *args, **kwargs): - return getattr(AccountClient(), method)(*args, **kwargs) + with AccountClient() as client: + return getattr(client, method)(*args, **kwargs) diff --git a/platformio/home/rpc/handlers/os.py b/platformio/home/rpc/handlers/os.py index 368f6737..9eef4404 100644 --- a/platformio/home/rpc/handlers/os.py +++ b/platformio/home/rpc/handlers/os.py @@ -29,8 +29,6 @@ from platformio.http import HTTPSession, ensure_internet_on class OSRPC(BaseRPCHandler): NAMESPACE = "os" - _http_session = None - @classmethod def fetch_content(cls, url, data=None, headers=None, cache_valid=None): if not headers: @@ -44,27 +42,25 @@ class OSRPC(BaseRPCHandler): cache_key = ContentCache.key_from_args(url, data) if cache_valid else None with ContentCache() as cc: if cache_key: - result = cc.get(cache_key) - if result is not None: - return result + content = cc.get(cache_key) + if content is not None: + return content # check internet before and resolve issue with 60 seconds timeout ensure_internet_on(raise_exception=True) - if not cls._http_session: - cls._http_session = HTTPSession() + with HTTPSession() as session: + if data: + response = session.post(url, data=data, headers=headers) + else: + response = session.get(url, headers=headers) - if data: - r = cls._http_session.post(url, data=data, headers=headers) - else: - r = cls._http_session.get(url, headers=headers) - - r.raise_for_status() - result = r.text - if cache_valid: - with ContentCache() as cc: - cc.set(cache_key, result, cache_valid) - return result + response.raise_for_status() + content = response.text + if cache_valid: + with ContentCache() as cc: + cc.set(cache_key, content, cache_valid) + return content @classmethod def request_content(cls, uri, data=None, headers=None, cache_valid=None): diff --git a/platformio/home/rpc/handlers/registry.py b/platformio/home/rpc/handlers/registry.py index ee10d679..42f58bfa 100644 --- a/platformio/home/rpc/handlers/registry.py +++ b/platformio/home/rpc/handlers/registry.py @@ -21,4 +21,5 @@ class RegistryRPC(BaseRPCHandler): @staticmethod def call_client(method, *args, **kwargs): - return getattr(RegistryClient(), method)(*args, **kwargs) + with RegistryClient() as client: + return getattr(client, method)(*args, **kwargs) diff --git a/platformio/package/commands/publish.py b/platformio/package/commands/publish.py index c8b4ea77..e5186517 100644 --- a/platformio/package/commands/publish.py +++ b/platformio/package/commands/publish.py @@ -87,7 +87,8 @@ def package_publish_cmd( # pylint: disable=too-many-arguments, too-many-locals ): click.secho("Preparing a package...", fg="cyan") no_interactive = no_interactive or non_interactive - owner = owner or AccountClient().get_logged_username() + with AccountClient() as client: + owner = owner or client.get_logged_username() do_not_pack = ( not os.path.isdir(package) and isinstance(FileUnpacker.new_archiver(package), TARArchiver) @@ -145,9 +146,10 @@ def package_publish_cmd( # pylint: disable=too-many-arguments, too-many-locals fg="yellow", ) click.echo("Publishing...") - response = RegistryClient().publish_package( - owner, typex, archive_path, released_at, private, notify - ) + with RegistryClient() as client: + response = client.publish_package( + owner, typex, archive_path, released_at, private, notify + ) if not do_not_pack: os.remove(archive_path) click.secho(response.get("message"), fg="green") diff --git a/platformio/package/commands/search.py b/platformio/package/commands/search.py index ac71ef4c..d483595d 100644 --- a/platformio/package/commands/search.py +++ b/platformio/package/commands/search.py @@ -29,8 +29,8 @@ from platformio.registry.client import RegistryClient type=click.Choice(["relevance", "popularity", "trending", "added", "updated"]), ) def package_search_cmd(query, page, sort): - client = RegistryClient() - result = client.list_packages(query, page=page, sort=sort) + with RegistryClient() as client: + result = client.list_packages(query, page=page, sort=sort) if not result["total"]: click.secho("Nothing has been found by your request", fg="yellow") click.echo( diff --git a/platformio/package/commands/show.py b/platformio/package/commands/show.py index 5be6790c..a427e477 100644 --- a/platformio/package/commands/show.py +++ b/platformio/package/commands/show.py @@ -124,31 +124,31 @@ def package_show_cmd(spec, pkg_type): def fetch_package_data(spec, pkg_type=None): assert isinstance(spec, PackageSpec) - client = RegistryClient() - if pkg_type and spec.owner and spec.name: + with RegistryClient() as client: + if pkg_type and spec.owner and spec.name: + return client.get_package( + pkg_type, spec.owner, spec.name, version=spec.requirements + ) + qualifiers = {} + if spec.id: + qualifiers["ids"] = str(spec.id) + if spec.name: + qualifiers["names"] = spec.name.lower() + if pkg_type: + qualifiers["types"] = pkg_type + if spec.owner: + qualifiers["owners"] = spec.owner.lower() + packages = client.list_packages(qualifiers=qualifiers)["items"] + if not packages: + return None + if len(packages) > 1: + PackageManagerRegistryMixin.print_multi_package_issue( + click.echo, packages, spec + ) + return None return client.get_package( - pkg_type, spec.owner, spec.name, version=spec.requirements + packages[0]["type"], + packages[0]["owner"]["username"], + packages[0]["name"], + version=spec.requirements, ) - qualifiers = {} - if spec.id: - qualifiers["ids"] = str(spec.id) - if spec.name: - qualifiers["names"] = spec.name.lower() - if pkg_type: - qualifiers["types"] = pkg_type - if spec.owner: - qualifiers["owners"] = spec.owner.lower() - packages = client.list_packages(qualifiers=qualifiers)["items"] - if not packages: - return None - if len(packages) > 1: - PackageManagerRegistryMixin.print_multi_package_issue( - click.echo, packages, spec - ) - return None - return client.get_package( - packages[0]["type"], - packages[0]["owner"]["username"], - packages[0]["name"], - version=spec.requirements, - ) diff --git a/platformio/package/commands/unpublish.py b/platformio/package/commands/unpublish.py index 7d0e633e..74b4c7a7 100644 --- a/platformio/package/commands/unpublish.py +++ b/platformio/package/commands/unpublish.py @@ -36,11 +36,14 @@ from platformio.registry.client import RegistryClient ) def package_unpublish_cmd(package, type, undo): # pylint: disable=redefined-builtin spec = PackageSpec(package) - response = RegistryClient().unpublish_package( - owner=spec.owner or AccountClient().get_logged_username(), - type=type, - name=spec.name, - version=str(spec.requirements), - undo=undo, - ) - click.secho(response.get("message"), fg="green") + with AccountClient() as client: + owner = spec.owner or client.get_logged_username() + with RegistryClient() as client: + response = client.unpublish_package( + owner=owner, + type=type, + name=spec.name, + version=str(spec.requirements), + undo=undo, + ) + click.secho(response.get("message"), fg="green") diff --git a/platformio/registry/access/commands/grant.py b/platformio/registry/access/commands/grant.py index f9214476..ad957ebf 100644 --- a/platformio/registry/access/commands/grant.py +++ b/platformio/registry/access/commands/grant.py @@ -31,8 +31,8 @@ from platformio.registry.client import RegistryClient ) @click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") def access_grant_cmd(level, client, urn, urn_type): # pylint: disable=unused-argument - reg_client = RegistryClient() - reg_client.grant_access_for_resource(urn=urn, client=client, level=level) + with RegistryClient() as reg_client: + reg_client.grant_access_for_resource(urn=urn, client=client, level=level) return click.secho( "Access for resource %s has been granted for %s" % (urn, client), fg="green", diff --git a/platformio/registry/access/commands/list.py b/platformio/registry/access/commands/list.py index ce9d8a6a..a5bd72d8 100644 --- a/platformio/registry/access/commands/list.py +++ b/platformio/registry/access/commands/list.py @@ -25,8 +25,8 @@ from platformio.registry.client import RegistryClient @click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") @click.option("--json-output", is_flag=True) def access_list_cmd(owner, urn_type, json_output): # pylint: disable=unused-argument - reg_client = RegistryClient() - resources = reg_client.list_resources(owner=owner) + with RegistryClient() as client: + resources = client.list_resources(owner=owner) if json_output: return click.echo(json.dumps(resources)) if not resources: diff --git a/platformio/registry/access/commands/private.py b/platformio/registry/access/commands/private.py index 5211aeb3..11a835aa 100644 --- a/platformio/registry/access/commands/private.py +++ b/platformio/registry/access/commands/private.py @@ -25,8 +25,8 @@ from platformio.registry.client import RegistryClient ) @click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") def access_private_cmd(urn, urn_type): # pylint: disable=unused-argument - client = RegistryClient() - client.update_resource(urn=urn, private=1) + with RegistryClient() as client: + client.update_resource(urn=urn, private=1) return click.secho( "The resource %s has been successfully updated." % urn, fg="green", diff --git a/platformio/registry/access/commands/public.py b/platformio/registry/access/commands/public.py index 465dcf32..77e9fe6b 100644 --- a/platformio/registry/access/commands/public.py +++ b/platformio/registry/access/commands/public.py @@ -25,8 +25,8 @@ from platformio.registry.client import RegistryClient ) @click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") def access_public_cmd(urn, urn_type): # pylint: disable=unused-argument - client = RegistryClient() - client.update_resource(urn=urn, private=0) + with RegistryClient() as client: + client.update_resource(urn=urn, private=0) return click.secho( "The resource %s has been successfully updated." % urn, fg="green", diff --git a/platformio/registry/access/commands/revoke.py b/platformio/registry/access/commands/revoke.py index b6bb0008..d7518559 100644 --- a/platformio/registry/access/commands/revoke.py +++ b/platformio/registry/access/commands/revoke.py @@ -30,8 +30,8 @@ from platformio.registry.client import RegistryClient ) @click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") def access_revoke_cmd(client, urn, urn_type): # pylint: disable=unused-argument - reg_client = RegistryClient() - reg_client.revoke_access_from_resource(urn=urn, client=client) + with RegistryClient() as reg_client: + reg_client.revoke_access_from_resource(urn=urn, client=client) return click.secho( "Access for resource %s has been revoked for %s" % (urn, client), fg="green", diff --git a/platformio/registry/client.py b/platformio/registry/client.py index 6173f7df..eae58a90 100644 --- a/platformio/registry/client.py +++ b/platformio/registry/client.py @@ -34,7 +34,8 @@ class RegistryClient(HTTPClient): ] ) try: - info = AccountClient().get_account_info() or {} + with AccountClient() as client: + info = client.get_account_info() or {} for item in info.get("packages", []): if set(item.keys()) & private_permissions: return True diff --git a/platformio/remote/factory/client.py b/platformio/remote/factory/client.py index b9e3cd5a..dd9209c3 100644 --- a/platformio/remote/factory/client.py +++ b/platformio/remote/factory/client.py @@ -37,7 +37,8 @@ class RemoteClientFactory(pb.PBClientFactory, protocol.ReconnectingClientFactory auth_token = None try: - auth_token = AccountClient().fetch_authentication_token() + with AccountClient() as client: + auth_token = client.fetch_authentication_token() except Exception as exc: # pylint:disable=broad-except d = defer.Deferred() d.addErrback(self.clientAuthorizationFailed) diff --git a/tests/commands/test_account_org_team.py b/tests/commands/test_account_org_team.py index 4f23ff9b..3cef8036 100644 --- a/tests/commands/test_account_org_team.py +++ b/tests/commands/test_account_org_team.py @@ -19,11 +19,11 @@ import os import random import pytest -import requests from platformio.account.cli import cli as cmd_account from platformio.account.org.cli import cli as cmd_org from platformio.account.team.cli import cli as cmd_team +from platformio.http import HTTPSession pytestmark = pytest.mark.skipif( not all( @@ -60,12 +60,11 @@ def verify_account(email_contents): .split("This link will expire within 12 hours.")[0] .strip() ) - with requests.Session() as session: + with HTTPSession() as session: result = session.get(link).text link = result.split('