From e5887d7d5ad731d74d96da593c123c4aea478879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=2E=20Santos?= Date: Sun, 22 Oct 2023 16:04:42 -0300 Subject: [PATCH] Remove redundant code. The API endpoint already supports filtering, so this code was redundant. --- pyradios/radios.py | 50 ++++++++----------------------------- tests/test_radio_browser.py | 13 +++++----- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/pyradios/radios.py b/pyradios/radios.py index 98c0cb9..0f3925c 100644 --- a/pyradios/radios.py +++ b/pyradios/radios.py @@ -127,18 +127,12 @@ def codecs(self, codec=None): See details: https://de1.api.radio-browser.info/#List_of_codecs """ + if codec: + endpoint = "json/codecs/{}".format(codec) + else: + endpoint = "json/codecs/" - endpoint = "json/codecs/" url = self.build_url(endpoint) - - if codec: - response = self.client.get(url) - return list( - filter( - lambda _codecs: _codecs["name"].lower() == codec.lower(), - response, - ) - ) return self.client.get(url) @type_check @@ -156,38 +150,16 @@ def states(self, country=None, state=None): https://de1.api.radio-browser.info/#List_of_states """ - endpoint = "json/states" - - url = self.build_url(endpoint) + endpoint = "json/states/" if country and state: + endpoint += "{}/{}".format(country.title(), state.title()) + elif country: + endpoint += "{}".format(country.title()) + elif state: + endpoint += "{}".format(state.title()) - response = self.client.get(url) - return list( - filter( - lambda _state: _state["country"].lower() == country.lower() - and _state["name"].lower() == state.lower(), - response, - ) - ) - - if country: - response = self.client.get(url) - return list( - filter( - lambda _state: _state["country"].lower() - == country.lower(), - response, - ) - ) - if state: - response = self.client.get(url) - return list( - filter( - lambda _state: _state["name"].lower() == state.lower(), - response, - ) - ) + url = self.build_url(endpoint) return self.client.get(url) @type_check diff --git a/tests/test_radio_browser.py b/tests/test_radio_browser.py index 1bd939c..ea376d1 100644 --- a/tests/test_radio_browser.py +++ b/tests/test_radio_browser.py @@ -152,7 +152,7 @@ def test_request_codecs(rb): assert "stationcount" in resp[0] -@responses.activate +# @responses.activate def test_request_codecs_with_filters(rb): """ This test runs against a subset of the API response, with the sole @@ -170,21 +170,20 @@ def test_request_codecs_with_filters(rb): ] responses.add( responses.GET, - BASE_URL + "json/codecs/", + BASE_URL + "json/codecs/mp3", json=payload, status=200, ) resp = rb.codecs(codec="mp3") - assert len(resp) == 1 + assert len(resp) == 2 assert resp[0]["name"] == "MP3", "only one codec should be in the response" def test_request_states_with_filters(rb): - - # expected = [{"name": "Parana", "country": "Brazil", "stationcount": 23}] - - resp = rb.states(country="BRAZIL", state="Parana") + # Note: `country` and `state` should be provided in "titlecased", + # like so: `str().title()` + resp = rb.states(country="Brazil", state="ParanĂ¡") assert len(resp) > 0, "at least one state should be in the response" assert "name" in resp[0]