From 16787da057fac46e4eaa8e5fbca231121894261d Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Tue, 31 Dec 2024 00:21:05 -0300 Subject: [PATCH 1/2] feat: adiciona enums --- pymusixmatch/enums.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/pymusixmatch/enums.py b/pymusixmatch/enums.py index e69de29..0c9aeeb 100644 --- a/pymusixmatch/enums.py +++ b/pymusixmatch/enums.py @@ -0,0 +1,74 @@ +from enum import Enum + + +class Route(Enum): + CHART_ARTISTS_GET = "chart.artists.get" + + +class Country(Enum): + AR = "ar" + AU = "au" + AT = "at" + BE = "be" + BR = "br" + BG = "bg" + CA = "ca" + CL = "cl" + CO = "co" + CR = "cr" + CZ = "cz" + DK = "dk" + DO = "do" + EC = "ec" + SV = "sv" + FI = "fi" + FR = "fr" + DE = "de" + GR = "gr" + GT = "gt" + HN = "hn" + HK = "hk" + HU = "hu" + IS = "is" + IN = "in" + ID = "id" + IE = "ie" + IL = "il" + IT = "it" + JP = "jp" + LV = "lv" + LT = "lt" + LU = "lu" + MY = "my" + MX = "mx" + NL = "nl" + NZ = "nz" + NI = "ni" + NO = "no" + PA = "pa" + PY = "py" + PE = "pe" + PH = "ph" + PL = "pl" + PT = "pt" + RO = "ro" + RU = "ru" + SG = "sg" + SK = "sk" + ZA = "za" + ES = "es" + SE = "se" + CH = "ch" + TW = "tw" + TH = "th" + TR = "tr" + UA = "ua" + GB = "gb" + US = "us" + UY = "uy" + VN = "vn" + + +class Format(Enum): + JSON = "json" + XML = "xml" From b91db88899db72990894621268ac83fc7708afdf Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Tue, 31 Dec 2024 00:21:44 -0300 Subject: [PATCH 2/2] refactor: add enums for validate params in method chart_artists --- pymusixmatch/musixmatch.py | 29 +++++++++++++++++++++++------ tests/test_musixmatch.py | 24 ++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pymusixmatch/musixmatch.py b/pymusixmatch/musixmatch.py index 2c37134..e4ad467 100644 --- a/pymusixmatch/musixmatch.py +++ b/pymusixmatch/musixmatch.py @@ -1,5 +1,8 @@ +from typing import Optional import requests +from pymusixmatch.enums import Country, Format, Route + class Musixmatch(object): BASE_URL = "http://api.musixmatch.com/ws" @@ -61,20 +64,34 @@ def _set_page_size(self, page_size: int) -> int: page_size = 1 return page_size - def chart_artists(self, page, page_size, country="us", _format="json"): + def chart_artists( + self, + page: int, + page_size: int, + country: Optional[Country] = Country.US.value, + _format: Optional[Format] = Format.JSON.value, + ) -> dict: """This api provides you the list of the top artists 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). - 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). + country (str): 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.artists.get?page={page}&page_size={self._set_page_size(page_size)}&country={country}&format={_format}", + f"{Route.CHART_ARTISTS_GET.value}?page={page}&page_size={self._set_page_size(page_size)}&country={country}&format={_format}", ), ) return request diff --git a/tests/test_musixmatch.py b/tests/test_musixmatch.py index b111035..46e31ee 100644 --- a/tests/test_musixmatch.py +++ b/tests/test_musixmatch.py @@ -17,8 +17,12 @@ def test_get_url(self) -> None: == f"{self.url}chart.artists.get?page=1&page_size=1&country=us&format=json&apikey=test" ) - def test_apikey(self): - assert self.musixmatch._apikey == "test" + @pytest.mark.parametrize( + "page_size, expected", + [(-1, 1), (0, 1), (1, 1), (10, 10), (100, 100), (101, 100)], + ) + def test_set_page_size(self, page_size: int, expected: int) -> None: + assert self.musixmatch._set_page_size(page_size) == expected 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" @@ -26,6 +30,22 @@ def test_chart_artists(self, requests_mock, chart_artists: dict) -> None: request = self.musixmatch.chart_artists(1, 1) assert chart_artists == request + 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" + requests_mock.get(url=url, json=chart_artists) + with pytest.raises(ValueError): + self.musixmatch.chart_artists(1, 1, country="invalid") + + 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" + 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" requests_mock.get(url=url, json=tracks)