Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the track/list_by_mbid API route #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jbosboom
Copy link

@jbosboom jbosboom commented Jan 7, 2020

Implements #56.

There's not much structure to the returned data so this function parses the response itself instead of having a separate parse function.

I copied the error-raising code from submit. Maybe that should be folded into _api_request?

AcoustIDs can be disabled (soft-deleted). When the caller asks to include the disabled ids with disabled=True we return a pair of lists instead of a list of (id, disabled) pairs. Neither option feels great, but I expect anyone who wants the disabled ids to treat them differently from the enabled ones, so I went with the pair of lists. I am willing to change this if you think list-of-pairs is better.

I'm not a "native" Python developer so bikeshedding and style nits are appreciated.

@jbosboom jbosboom changed the title Support the track/list_by_id API route Support the track/list_by_mbid API route Jan 8, 2020
Copy link
Member

@sampsyo sampsyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome! My sincerest apologies for letting this PR slip through the cracks—I don't usually make a habit out of letting things go for months at a time. 😳

This seems good already. One design thing, however, is that I generally think it can be pretty confusing when functions can substantially change their output format (i.e., return type) depending on the values of the arguments. The current implementation does this in two ways (not your fault—this is the way the underlying API works): batch vs. single, and whether to include disabled MBIDs. So what do you think about moving the core functionality to a helper function and providing wrappers with consistent return types?

One way to do this would be to have a batch function and single-ID function. That seems nice but it doesn't solve the disabledness problem, which is trickier. Maybe we should just live with that one.

message = response['error']['message']
except KeyError:
raise WebServiceError("response: {0}".format(response))
raise WebServiceError("error {0}: {1}".format(code, message))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, as you mentioned, maybe this can be factored out into a function to avoid duplication. The two options are for it to go into _api_request or into a new, separate function. The former would be more appropriate if we only ever use _api_request this way; the latter might be better if some calls to _api_request need to process stuff differently.

mbids = response['mbids']
if disabled:
return {m['mbid']:
([x['id'] for x in m['tracks'] if 'disabled' not in x or not x['disabled']],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit:

'disabled' not in x or not x['disabled']

is equivalent to this slightly shorter expression:

not x.get('disabled')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants