Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to query account details including personal data #159

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pytr/accountdetails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json

class Accountdetails:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Accountdetails:
class AccountDetails:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this class has any right to be here, it's such a thin wrapper. :)

What would be neat is this was a TypedDict instead that describes the fields returned by TradeRepublicApi.get_account_details().

def __init__(self, tr):
self.tr = tr
self.data = None

def get(self):
self.data = self.tr.get_account_details()
print(f"{self.data}")
return self.data

def data_to_file(self, output_path):
with open(output_path, "w", encoding="utf-8") as f:
json.dump(self.data, f)

7 changes: 7 additions & 0 deletions pytr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ def resume_websession(self):
return True
return False

def get_account_details(self):
r = self._websession.get(
f"{self._host}/api/v2/auth/account",
)
j = r.json()
return j

def _web_request(self, url_path, payload=None, method="GET"):
if self._web_session_token_expires_at < time.time():
r = self._websession.get(f"{self._host}/api/v1/auth/web/session")
Expand Down
20 changes: 20 additions & 0 deletions pytr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pytr.portfolio import Portfolio
from pytr.alarms import Alarms
from pytr.details import Details
from pytr.accountdetails import Accountdetails


def get_main_parser():
Expand Down Expand Up @@ -200,6 +201,18 @@ def formatter(prog):
help='Two letter language code or "auto" for system language',
default="auto",
)
# account details
info = "Show account details"
parser_accountdetails = parser_cmd.add_parser(
"accountdetails",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
parents=[parser_login_args],
help=info,
description=info,
)
parser_accountdetails.add_argument(
"-j", "--jsonoutput", help="Output path of JSON file", metavar="OUTPUT", type=Path
)

info = "Print shell tab completion"
parser_completion = parser_cmd.add_parser(
Expand Down Expand Up @@ -287,6 +300,13 @@ def main():
installed_version = version("pytr")
print(installed_version)
check_version(installed_version)
elif args.command == "accountdetails":
ad = Accountdetails(
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin),
)
ad.get()
if args.jsonoutput is not None:
ad.data_to_file(args.jsonoutput)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif args.command == "accountdetails":
ad = Accountdetails(
login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin),
)
ad.get()
if args.jsonoutput is not None:
ad.data_to_file(args.jsonoutput)
elif args.command == "accountdetails":
tr = login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)
account_details = tr.get_account_details()
args.jsonoutput.write(json.dumps(account_details))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question here: this would write the json args.jsonoutput without checking if this parameter is actually set.
Are the any implicit checks I'm not aware of? Or should I change this to

    elif args.command == "accountdetails":
        tr = login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)
        account_details = tr.get_account_details()
        if args.jsonoutput is not None:
          args.jsonoutput.write(json.dumps(account_details))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other change above I suggested makes it so args.jsonpath is always set, to a file-like object in write mode. If no argument is specified, it defaults to - which argparse.FileType interprets as "stdout".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right! I wasn't aware initially that those -o parameter are per command. It's a nice solution.

else:
parser.print_help()

Expand Down