From 513bc6eadf6c6eb4383d828e425dd7ff8dde6a48 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 25 Jul 2019 20:31:10 +0200 Subject: [PATCH] Move Client._do_refresh to State --- fbchat/_client.py | 10 ++-------- fbchat/_state.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index bd7dc08..708bf93 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -108,12 +108,6 @@ class Client(object): INTERNAL REQUEST METHODS """ - def _do_refresh(self): - # TODO: Raise the error instead, and make the user do the refresh manually - # It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! - log.warning("Refreshing state and resending request") - self._state = State.from_session(session=self._state._session) - def _get(self, url, params, error_retries=3): params.update(self._state.get_params()) r = self._state._session.get(prefix_url(url), params=params) @@ -123,7 +117,7 @@ class Client(object): handle_payload_error(j) except FBchatPleaseRefresh: if error_retries > 0: - self._do_refresh() + self._state._do_refresh() return self._get(url, params, error_retries=error_retries - 1) raise return j @@ -143,7 +137,7 @@ class Client(object): return j except FBchatPleaseRefresh: if error_retries > 0: - self._do_refresh() + self._state._do_refresh() return self._post( url, data, diff --git a/fbchat/_state.py b/fbchat/_state.py index 351f95b..fd6e501 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -191,3 +191,13 @@ class State(object): session = session_factory(user_agent=user_agent) session.cookies = requests.cookies.merge_cookies(session.cookies, cookies) return cls.from_session(session=session) + + def _do_refresh(self): + # TODO: Raise the error instead, and make the user do the refresh manually + # It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! + _util.log.warning("Refreshing state and resending request") + new = State.from_session(session=self._session) + self.fb_dtsg = new.fb_dtsg + self._revision = new._revision + self._counter = new._counter + self._logout_h = new._logout_h or self._logout_h