From 7e00b6c814c7da73a9468db0a0d92650652a2d26 Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Thu, 2 Jan 2025 01:01:01 -0300 Subject: [PATCH 1/4] feat: add types for params --- pymusixmatch/musixmatch.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pymusixmatch/musixmatch.py b/pymusixmatch/musixmatch.py index 42ac777..bbe36ce 100644 --- a/pymusixmatch/musixmatch.py +++ b/pymusixmatch/musixmatch.py @@ -78,7 +78,7 @@ def chart_artists( page (int): Define the page number for paginated results. page_size (int): Define the page size for paginated results (range 1 - 100). - country (str): A valid country code (default US). + country (Country): A valid country code (default US). format (Format): Decide the output type json or xml (default json). """ if country not in Country._value2member_map_: @@ -98,23 +98,31 @@ def chart_artists( def chart_tracks_get( self, - page, - page_size, - f_has_lyrics, - country="us", - _format="json", + page: int, + page_size: int, + f_has_lyrics: bool, + country: Optional[Country] = Country.US.value, + _format: Optional[Format] = Format.JSON.value, ): """This api provides you the list of the top songs of a given country. Parameters: - page - Define the page number for paginated results. - page_size - Define the page size for paginated results (range 1 - 100). - f_has_lyrics - When set, filter only contents with lyrics. - country - A valid country code (default US). - format - Decide the output type json or xml (default json). + page (int): Define the page number for paginated results. + page_size (int): Define the page size for paginated results (range 1 - 100). + f_has_lyrics (bool): When set, filter only contents with lyrics. + country (Country): A valid country code (default US). + format (Format): Decide the output type json or xml (default json). """ + if country not in Country._value2member_map_: + raise ValueError( + f"Invalid country code: {country}, please use a valid country code." + ) + + if _format not in Format._value2member_map_: + raise ValueError(f"Invalid format: {_format}, please use a valid format.") + request = self._request( self._get_url( f"chart.tracks.get?page={page}&page_size={self._set_page_size(page_size)}&country={country}&format={_format}&f_has_lyrics={f_has_lyrics}", From 66f4b100e3771591fb873bc9f589ac89d04e42de Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Thu, 2 Jan 2025 01:01:18 -0300 Subject: [PATCH 2/4] test: add tests for validate country and format --- tests/test_musixmatch.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_musixmatch.py b/tests/test_musixmatch.py index 9cabc2f..8612d20 100644 --- a/tests/test_musixmatch.py +++ b/tests/test_musixmatch.py @@ -65,6 +65,22 @@ def test_chart_tracks_get(self, requests_mock, tracks: dict) -> None: request = self.musixmatch.chart_tracks_get(1, 1, 1) assert tracks == request + def test_chart_tracks_get_with_invalid_country( + self, requests_mock, tracks: dict + ) -> None: + url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=invalid&format=json&f_has_lyrics=1&apikey=test" + requests_mock.get(url=url, json=tracks) + with pytest.raises(ValueError): + self.musixmatch.chart_tracks_get(1, 1, 1, country="invalid") + + def test_chart_tracks_get_with_invalid_format( + self, requests_mock, tracks: dict + ) -> None: + url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=invalid&f_has_lyrics=1&apikey=test" + requests_mock.get(url=url, json=tracks) + with pytest.raises(ValueError): + self.musixmatch.chart_tracks_get(1, 1, 1, _format="invalid") + @pytest.mark.skip("Refactor test") def test_track_search(self): self.assertEqual( From a303b76f5b0e07cf772fb22e3690dc83ccfca399 Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Thu, 2 Jan 2025 01:03:58 -0300 Subject: [PATCH 3/4] fix: change http to https --- pymusixmatch/musixmatch.py | 2 +- tests/test_musixmatch.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pymusixmatch/musixmatch.py b/pymusixmatch/musixmatch.py index bbe36ce..18b8200 100644 --- a/pymusixmatch/musixmatch.py +++ b/pymusixmatch/musixmatch.py @@ -5,7 +5,7 @@ class Musixmatch(object): - BASE_URL = "http://api.musixmatch.com/ws" + BASE_URL = "https://api.musixmatch.com/ws" VERSION = "1.1" def __init__(self, api_key: str) -> None: diff --git a/tests/test_musixmatch.py b/tests/test_musixmatch.py index 8612d20..ea3c09c 100644 --- a/tests/test_musixmatch.py +++ b/tests/test_musixmatch.py @@ -7,7 +7,7 @@ class TestMusixmatch: @classmethod def setup_class(cls) -> None: cls.musixmatch = Musixmatch("test") - cls.url = "http://api.musixmatch.com/ws/1.1/" + cls.url = "https://api.musixmatch.com/ws/1.1/" def test_get_url(self) -> None: assert ( @@ -54,13 +54,13 @@ def test_chart_artists_with_invalid_country( def test_chart_artists_with_invalid_format( self, requests_mock, chart_artists: dict ) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=invalid&apikey=test" + url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=invalid&apikey=test" requests_mock.get(url=url, json=chart_artists) with pytest.raises(ValueError): self.musixmatch.chart_artists(1, 1, _format="invalid") def test_chart_tracks_get(self, requests_mock, tracks: dict) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=json&f_has_lyrics=1" + url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=json&f_has_lyrics=1" requests_mock.get(url=url, json=tracks) request = self.musixmatch.chart_tracks_get(1, 1, 1) assert tracks == request @@ -68,7 +68,7 @@ def test_chart_tracks_get(self, requests_mock, tracks: dict) -> None: def test_chart_tracks_get_with_invalid_country( self, requests_mock, tracks: dict ) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=invalid&format=json&f_has_lyrics=1&apikey=test" + url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=invalid&format=json&f_has_lyrics=1&apikey=test" requests_mock.get(url=url, json=tracks) with pytest.raises(ValueError): self.musixmatch.chart_tracks_get(1, 1, 1, country="invalid") @@ -76,7 +76,7 @@ def test_chart_tracks_get_with_invalid_country( def test_chart_tracks_get_with_invalid_format( self, requests_mock, tracks: dict ) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=invalid&f_has_lyrics=1&apikey=test" + url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=invalid&f_has_lyrics=1&apikey=test" requests_mock.get(url=url, json=tracks) with pytest.raises(ValueError): self.musixmatch.chart_tracks_get(1, 1, 1, _format="invalid") @@ -110,13 +110,13 @@ def test_track_get(self): ) def test_track_lyrics_get(self, requests_mock, tracks: dict) -> None: - url = "http://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=12345" + url = "https://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=12345" requests_mock.get(url=url, json=tracks) request = self.musixmatch.track_lyrics_get(12345) assert tracks == request def test_track_snippet_get(self, requests_mock, track_snippet: dict) -> None: - url = "http://api.musixmatch.com/ws/1.1/track.snippet.get?track_id=12345" + url = "https://api.musixmatch.com/ws/1.1/track.snippet.get?track_id=12345" requests_mock.get(url=url, json=track_snippet) request = self.musixmatch.track_snippet_get(12345) assert track_snippet == request From 0d8d3ebd89065f936b07848bebcce31e6b46aa97 Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Thu, 2 Jan 2025 01:06:38 -0300 Subject: [PATCH 4/4] test: fix test http to https --- tests/test_musixmatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_musixmatch.py b/tests/test_musixmatch.py index ea3c09c..ab23195 100644 --- a/tests/test_musixmatch.py +++ b/tests/test_musixmatch.py @@ -38,7 +38,7 @@ def test_set_page_size_with_invalid_page_size( ) def test_chart_artists(self, requests_mock, chart_artists: dict) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=json" + url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=json" requests_mock.get(url=url, json=chart_artists) request = self.musixmatch.chart_artists(1, 1) assert chart_artists == request @@ -46,7 +46,7 @@ def test_chart_artists(self, requests_mock, chart_artists: dict) -> None: def test_chart_artists_with_invalid_country( self, requests_mock, chart_artists: dict ) -> None: - url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=invalid&format=json&apikey=test" + url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=invalid&format=json&apikey=test" requests_mock.get(url=url, json=chart_artists) with pytest.raises(ValueError): self.musixmatch.chart_artists(1, 1, country="invalid")