-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1689194
commit a0f0ca5
Showing
10 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .full import Spotify | ||
from .short_link import is_short_link |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from .._sender import Request, Response, send_and_process | ||
from .._sender.base import Sender | ||
from .base import SpotifyBase | ||
|
||
|
||
def is_short_link(url: str) -> bool: | ||
"""Determine if URL is a Spotify short link.""" | ||
return "spotify.link/" in url | ||
|
||
|
||
def _process_short_link(request: Request, response: Response) -> str: | ||
if response.status_code == 307: | ||
return response.headers["location"] | ||
else: | ||
return request.url | ||
|
||
|
||
@send_and_process(_process_short_link) | ||
def _send_short_link(self: Sender, request: Request): | ||
return request | ||
|
||
|
||
class SpotifyShortLink(SpotifyBase): | ||
"""Spotify short link.""" | ||
|
||
def follow_short_link(self, link: str) -> str: | ||
""" | ||
Follow redirect of a short link to get the underlying resource URL. | ||
Safely also accept a direct link, request a redirect and return | ||
the original URL. Also use the underlying sender for an unauthenticated request. | ||
Returns | ||
------- | ||
url | ||
result of the short link redirect | ||
""" | ||
request = self._request("HEAD", link) | ||
return _send_short_link(self.sender, request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
from tekore import from_url, is_short_link | ||
|
||
from ._resources import short_link, track_id | ||
|
||
|
||
class TestSpotifyShortLink: | ||
def test_random_string_is_not_short_link(self): | ||
assert not is_short_link("asdf") | ||
|
||
def test_random_url_is_not_short_link(self): | ||
assert not is_short_link("https://spotify.org/resource") | ||
|
||
def test_valid_short_link(self): | ||
assert is_short_link("https://spotify.link/resource") | ||
|
||
def test_short_link_does_not_need_https(self): | ||
assert is_short_link("spotify.link/resource") | ||
|
||
def test_follow_short_link(self, app_client): | ||
resolved = app_client.follow_short_link(short_link) | ||
assert short_link != resolved | ||
from_url(resolved) | ||
|
||
@pytest.mark.asyncio | ||
async def test_async_follow_short_link(self, app_aclient): | ||
resolved = await app_aclient.follow_short_link(short_link) | ||
assert short_link != resolved | ||
from_url(resolved) | ||
|
||
def test_follow_short_link_already_resolved(self, app_client): | ||
link = "https://open.spotify.com/track/" + track_id | ||
resolved = app_client.follow_short_link(link) | ||
assert link == resolved | ||
from_url(resolved) | ||
|
||
def test_follow_short_link_does_not_authorise(self, app_client, httpx_mock): | ||
httpx_mock.add_response(200) | ||
app_client.follow_short_link(short_link) | ||
(request,) = httpx_mock.get_requests() | ||
assert "authorization" not in request.headers |