Added full_url method into the API wrapper. It converts an URL starting with the API prefix into a full URL. May be useful in some cases

This commit is contained in:
Manuel Cortez 2019-05-20 10:48:08 -05:00
parent f266e247dc
commit c6b15c7f7b
2 changed files with 19 additions and 0 deletions

View File

@ -87,3 +87,10 @@ class API(object):
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return self._session.method(self._method, *args, **kwargs) return self._session.method(self._method, *args, **kwargs)
def full_url(self, method):
""" Converts a partial URL such as /api/v1/listen/{id} into https://instance.endpoint/api/v1/listen/track_id """
# check if the method is valid.
if method.startswith(self._session.API_PREFIX) == False:
raise ValueError("the method passed seems to be an invalid URL.")
return self._session.instance_endpoint+method

View File

@ -45,5 +45,17 @@ class testApi(unittest.TestCase):
self.assertTrue(artist["id"] == 1) self.assertTrue(artist["id"] == 1)
libraries = api.albums.libraries.get(1) libraries = api.albums.libraries.get(1)
def test_full_url(self):
""" Testing conversion of some URLS provided by the funkwhale API into full URLS available for other applications to do something with those. """
api = self.session.get_api()
track = api.tracks.get(1)
# Make sure the API URL is still valid.
self.assertTrue(track["listen_url"].startswith(self.session.API_PREFIX))
url = api.full_url(track["listen_url"])
self.assertTrue(url.startswith(self.session.instance_endpoint))
track["listen_url"] = url
self.assertRaises(ValueError, api.full_url, track["listen_url"])
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()