Skip to content

Commit

Permalink
Merge pull request #29 from hudsonbrendon/fix/chart-artists
Browse files Browse the repository at this point in the history
refactor: add enums for validate params in method chart_artists
  • Loading branch information
hudsonbrendon authored Dec 31, 2024
2 parents 4a317b1 + b91db88 commit 9b90b04
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
74 changes: 74 additions & 0 deletions pymusixmatch/enums.py
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 23 additions & 6 deletions pymusixmatch/musixmatch.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions tests/test_musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@ 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"
requests_mock.get(url=url, json=chart_artists)
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)
Expand Down

0 comments on commit 9b90b04

Please sign in to comment.