joalla/discogs_client

Error 400 still occurring on some configurations

hellricer opened this issue · 4 comments

There are apparently still issues with error 400 on post requests since #18. Seems to happen only on some setups for some reason - might be related to different versions of dependencies?

Python 3.8.3 & 3.9.1
requests 2.25.1
oauthlib 3.1.0

Test code:

import discogs_client
ds = discogs_client.Client('DevApp/1.0', user_token='...')
me = ds.identity()
me.wantlist.add(89411)

Okay, I think I found the issue. When using the user_token it uses UserTokenRequestsFetcher, which does not convert the data to json string.

class UserTokenRequestsFetcher(Fetcher):
"""Fetches via HTTP from the Discogs API using user_token authentication"""
def __init__(self, user_token):
self.user_token = user_token
def fetch(self, client, method, url, data=None, headers=None, json=True):
resp = requests.request(method, url, params={'token':self.user_token},
data=data, headers=headers)

I changed the fetch method to match the oauth one like so

def fetch(self, client, method, url, data=None, headers=None, json_format=True):
        data = json.dumps(data) if json_format and data else data
        resp = requests.request(method, url, params={'token':self.user_token},
                                data=data, headers=headers)
       ...

Hope this is the only culprit

Maybe this method should be implemented in the base Fetcher class? Nvm each class implements it differently 🤦

Good solution to this could be implementing the method properly in RequestsFetcher class and inheriting it in OAuth2Fetcher and UserTokenRequestsFetcher (I know, annoying mismatched naming, lol)
Arghh, thinking too fast without inspecting the code properly

Great find, thanks. Would you mind creating the PR, or do you want to include it in #27?

I'll create a new PR, as it's not really related to changes made in #27

@AnssiAhola @hellricer PR was merged and package with change has been released - version 2.3.8. I'm going to close this issue but feel free to open it again if the problem arises. Thanks for the quick work guys.