itsjafer / schwab-api

A python library for placing trades on Charles Schwab
MIT License
213 stars 66 forks source link

Allow session to be cached to disk #53

Closed tristanpenman closed 5 months ago

tristanpenman commented 8 months ago

Thanks for your work on this library. I was so glad to find it!

I'm currently using the library in a few different ways - some smaller scripts, but also a long-running server. In both cases, it has been useful to cache the session data between runs, to avoid the login step. The way I've implemented this is to write the cookies and headers to a JSON file, and to read those before attempting to log in.

I've recently rebased my changes against @4rumprom's async branch, and you can see the diff here: https://github.com/4rumprom/schwab-api/compare/Async...tristanpenman:schwab-api:cache-session?expand=1

If this is something that you would be interested in supporting, I can raise a PR once @4rumprom's changes are merged.

4rumprom commented 8 months ago

That would be really cool actually as it could help structure my code a bit better!

itsjafer commented 8 months ago

I tried implementing this a while ago and found it didn't persist correctly. How long can you keep login data cached, in your testing? I'd love to be able to make the login step a one-time cost of using the API

tristanpenman commented 8 months ago

I believe it will depend on the user's individual security settings in the Charles Schwab portal. I'll take a closer look at the contents of the cookies to see that is exposed in any way. Otherwise I would probably just use a periodic refresh of the token, based on the minimum web session timeout.

itsjafer commented 8 months ago

Hi @tristanpenman, I've merged in the async changes from @4rumprom's PR. I would love it if you could create a PR for persisting login data!

tristanpenman commented 8 months ago

I've raised a draft PR for this: https://github.com/itsjafer/schwab-api/pull/56

I'll do some more testing with my particular use cases, with a focus on error cases. Error handling is pretty much untested right now, so expect one or two more commits on the basis of that testing 😅

itsjafer commented 5 months ago

Thank you for your contribution @tristanpenman! Your PR has been merged and should be available in v0.4.1 :)