Skip to content

Commit

Permalink
Always re-login if a request fails
Browse files Browse the repository at this point in the history
This should silently fix various re-connect issues.
  • Loading branch information
postlund committed Dec 12, 2017
1 parent 852b0a4 commit dcf2693
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
8 changes: 3 additions & 5 deletions pyatv/daap.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _login_request():
session=False, login_id=True),
headers=_DMAP_HEADERS)

resp = yield from self._do(_login_request)
resp = yield from self._do(_login_request, is_login=True)
self._session_id = dmap.first(resp, 'mlog', 'mlid')
_LOGGER.info('Logged in and got session id %s', self._session_id)
return self._session_id
Expand Down Expand Up @@ -93,10 +93,8 @@ def _do(self, action, retry=True, is_login=False, is_daap=True):
self._log_response(str(action.__name__) + ': %s', resp, is_daap)
if status >= 200 and status < 300:
return resp

# When a 403 is received we are likely logged out, so a new
# login must be performed to get a new session id
if status == 403:
elif not is_login:
# If a request fails, try to login again before retrying
_LOGGER.info('implicitly logged out, logging in again')
yield from self.login()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def read(fname):
zip_safe=False,
platforms='any',
install_requires=[
'aiohttp>=1.3.5, <3',
'aiohttp>=2.3.0, <3',
'cryptography>=1.8.1',
'curve25519-donna>=1.3, <2',
'ed25519>=1.4, <2',
Expand Down
11 changes: 2 additions & 9 deletions tests/test_airplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ def tearDown(self):
def get_application(self, loop=None):
self.fake_atv = FakeAppleTV(self.loop, 0, 0, 0, self)
self.usecase = AppleTVUseCases(self.fake_atv)

# Import TestServer here and not globally, otherwise py.test will
# complain when running:
#
# test_functional.py cannot collect test class 'TestServer'
# because it has a __init__ constructor
from aiohttp.test_utils import TestServer
return TestServer(self.fake_atv)
return self.fake_atv

@asyncio.coroutine
def fake_asyncio_sleep(self, time, loop):
Expand All @@ -55,7 +48,7 @@ def test_play_video(self):

session = ClientSession(loop=self.loop)
aplay = player.AirPlayPlayer(
self.loop, session, '127.0.0.1', port=self.app.port)
self.loop, session, '127.0.0.1', port=self.server.port)
yield from aplay.play_url(STREAM, position=START_POSITION)

self.assertEqual(self.fake_atv.last_airplay_url, STREAM)
Expand Down
17 changes: 5 additions & 12 deletions tests/test_airplay_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@ def tearDown(self):
@asyncio.coroutine
def get_application(self, loop=None):
self.fake_atv = FakeAppleTV(self.loop, 0, 0, 0, self)

# Import TestServer here and not globally, otherwise py.test will
# complain when running:
#
# test_functional.py cannot collect test class 'TestServer'
# because it has a __init__ constructor
from aiohttp.test_utils import TestServer
return TestServer(self.fake_atv)
return self.fake_atv

@unittest_run_loop
def test_verify_invalid(self):
http = HttpSession(
self.session, 'http://127.0.0.1:{0}/'.format(self.app.port))
self.session, 'http://127.0.0.1:{0}/'.format(self.server.port))
handler = srp.SRPAuthHandler()
handler.initialize(INVALID_AUTH_KEY)

Expand All @@ -56,7 +49,7 @@ def test_verify_invalid(self):
@unittest_run_loop
def test_verify_authenticated(self):
http = HttpSession(
self.session, 'http://127.0.0.1:{0}/'.format(self.app.port))
self.session, 'http://127.0.0.1:{0}/'.format(self.server.port))
handler = srp.SRPAuthHandler()
handler.initialize(binascii.unhexlify(DEVICE_AUTH_KEY))

Expand All @@ -66,7 +59,7 @@ def test_verify_authenticated(self):
@unittest_run_loop
def test_auth_successful(self):
http = HttpSession(
self.session, 'http://127.0.0.1:{0}/'.format(self.app.port))
self.session, 'http://127.0.0.1:{0}/'.format(self.server.port))
handler = srp.SRPAuthHandler()
handler.initialize(INVALID_AUTH_KEY)

Expand All @@ -79,7 +72,7 @@ def test_auth_successful(self):
@unittest_run_loop
def test_auth_failed(self):
http = HttpSession(
self.session, 'http://127.0.0.1:{0}/'.format(self.app.port))
self.session, 'http://127.0.0.1:{0}/'.format(self.server.port))
handler = srp.SRPAuthHandler()
handler.initialize(binascii.unhexlify(DEVICE_AUTH_KEY))

Expand Down
16 changes: 5 additions & 11 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,12 @@ def get_application(self, loop=None):
self.fake_atv = FakeAppleTV(
self.loop, HSGID, PAIRING_GUID, SESSION_ID, self)
self.usecase = AppleTVUseCases(self.fake_atv)

# Import TestServer here and not globally, otherwise py.test will
# complain when running:
#
# test_functional.py cannot collect test class 'TestServer'
# because it has a __init__ constructor
from aiohttp.test_utils import TestServer
return TestServer(self.fake_atv)
return self.fake_atv

def get_connected_device(self, identifier):
details = AppleTVDevice(
'Apple TV', '127.0.0.1', identifier, self.app.port, self.app.port)
'Apple TV', '127.0.0.1', identifier,
self.server.port, self.server.port)
return connect_to_apple_tv(details, self.loop)

# This is not a pretty test and it does crazy things. Should probably be
Expand Down Expand Up @@ -109,7 +103,7 @@ def test_play_url(self):
self.usecase.airplay_playback_idle()

yield from self.atv.airplay.play_url(
AIRPLAY_STREAM, port=self.app.port)
AIRPLAY_STREAM, port=self.server.port)

self.assertEqual(self.fake_atv.last_airplay_url, AIRPLAY_STREAM)

Expand All @@ -123,7 +117,7 @@ def test_play_url_authenticated(self):
yield from self.atv.airplay.load_credentials(DEVICE_CREDENTIALS)

yield from self.atv.airplay.play_url(
AIRPLAY_STREAM, port=self.app.port)
AIRPLAY_STREAM, port=self.server.port)

self.assertEqual(self.fake_atv.last_airplay_url, AIRPLAY_STREAM)

Expand Down

0 comments on commit dcf2693

Please sign in to comment.