square/connect-python-sdk

getbatch_token broken in Python 3

Closed this issue · 5 comments

I'm opening this issue in reference to an existing one (i.e., issue #53), since the two are technically only tangentially related and this may increase the visibility of an easy fix.

Long story short, batch_tokens functionality is broken in certain V1 endpoints when using Python 3 due to the deprecation of has_key() in Python 3. A simple change to getbatch_token in rest.py resolves it, as pointed out by @MHova (see below). As it currently stands, the "Pagination of V1 Endpoints" example in the readme fails in Python 3 with the error:

python: 'dict' object has no attribute 'has_key'

The relevant comment and quote from issue #53 appear below:

1. Great writeup @cschweiger. One nit: I think `has_key()` was removed in Python3 in favor of `in` so some adjustments will have to be made to `if parameters.has_key('batch_token'):`.

2. Any movement on this issue, admins? This seems like a pretty big and easily-solvable problem. I'm surprised nobody else has chimed in on this.

Regarding @MHova's point 1, I ran into this issue as well and implemented the simple fix you suggested for Python 3 compatibility (i.e., in vs. has_key):

def getbatch_token(self):
        link_header = self.getheader('Link')
        if link_header != None:
            match = re.match("^<([^>]+)>;rel='next'$", link_header)
            if match != None:
                next_url = match.group(1)
                parsed_url = urlparse(next_url)
                parameters = parse_qs(parsed_url.query)
                if 'batch_token' in parameters:
                    return parameters['batch_token'][0]
return None

Originally posted by @davedgd in #53 (comment)

Thanks for letting us know, and we apologize for the delay in following up on this. We'll look into what it would take to get this fixed, and let you know one we have more information.

Thanks for the quick reply @StephenJosey -- much appreciated!

@StephenJosey or @shaofu88: Any chance there might be an update on this one line fix of the code to remove the deprecated usage of has_key in rest.py and allow for Python 3 compatibility?

I've been keeping my fork with this change up to date for almost six months: I'm hoping this issue might get resolved soon -- it would close #86, #53, and #88.

@davedgd We apologize for the delay, but the fix has just been merged for version 2.20190710.1. Thank you for using Square :)

@shaofu88 Awesome, I see the change: thank you so much! =)

I'll go ahead and close this issue along with my PR (#88).