Re: try/except in loop

2020-11-27 Thread Cameron Simpson
I had to do this with a different API the toehr year, and made this 
method:

def auth_token(self):
''' Obtain a current auth token [...]
Refreshes the cached token if stale.
'''
while True:
with self._auth_token_lock:
token = self._auth_token
if (token and token.timestamp + FM_TOKEN_LIFESPAN -
FM_TOKEN_LIFESPAN_SKEW > time.time()):
break
try:
token = self._get_new_auth_token()
except Exception as e:
error("error fetching token: %s", e)
else:
if token:
self._auth_token = token
break
error("token not refreshed, delay then retry")
time.sleep(self.DEFAULT_RETRY_DELAY)
return token

The "while True" bit was because the server was flakey and sometime you 
just had to wait for it to come back.

Then I just called this to obtain a current token whenever I needed a 
token. in the API, for example:

headers = {'Authorization': 'Bearer ' + self.auth_token().token}

Means you don't have to embed verbose checks all through your code - 
just grab "the token" and proceeed.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try/except in loop

2020-11-27 Thread Jason Friedman
>
>
>> I'm using the Box API (
>> https://developer.box.com/guides/tooling/sdks/python/).
>> I can get an access token, though it expires after a certain amount of
>> time. My plan is to store the access token on the filesystem and use it
>> until it expires, then fetch a new one. In the example below assume I have
>> an expired access token.
>>
>> # This next line does not throw an error:
>> client.folder('0').get_items()
>>
>> # But iteration does (maybe this is a lazy fetch?)
>> for _ in client.folder('0').get_items():
>> logger.debug("Using existing access token.")
>> return access_token
>>
>> # So I use try/except
>> try:
>> for _ in client.folder('0').get_items():
>> logger.debug("Using existing access token.")
>> return access_token
>> except boxsdk.exception.BoxAPIException:
>> pass # access token invalid, let's get one
>> else:
>> pass # access token invalid, let's get one
>>
>> # When running the debugger the except clause seems to catch the first
>> throw, but the loop I think continues, throws the error again, and that
>> second throw is not caught.
>>
>
> It would appear that get items is a generator which uses the token exactly
> once when it is first started; subsequent calls to the generator all use
> the same token. You need to test the token; if it fails,
> obtain a new one, then start the loop.
>

# Here's the solution I came up with:
try:
list(client.folder('0').get_items())
logger.debug("Using existing access token.")
return access_token
except boxsdk.exception.BoxAPIException:
pass # access token invalid, let's get one

# And for those who happen to be using the Box API, this command avoids all
that and is less expensive:
try:
client.user().get()
logger.debug("Using existing access token.")
return access_token
except boxsdk.exception.BoxAPIException:
pass # access token invalid, let's get one
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try/except in loop

2020-11-27 Thread Bob Gailer
On Fri, Nov 27, 2020, 6:06 PM Jason Friedman  wrote:

> I'm using the Box API (
> https://developer.box.com/guides/tooling/sdks/python/).
> I can get an access token, though it expires after a certain amount of
> time. My plan is to store the access token on the filesystem and use it
> until it expires, then fetch a new one. In the example below assume I have
> an expired access token.
>
>
> # This next line does not throw an error:
>
> client.folder('0').get_items()
>
>
> # But iteration does (maybe this is a lazy fetch?)
>
> for _ in client.folder('0').get_items():
>
> logger.debug("Using existing access token.")
>
> return access_token
>
>
> # So I use try/except
>
> try:
>
> for _ in client.folder('0').get_items():
>
> logger.debug("Using existing access token.")
>
> return access_token
>
> except boxsdk.exception.BoxAPIException:
>
> pass # access token invalid, let's get one
>
> else:
>
> pass # access token invalid, let's get one
>
>
> # When running the debugger the except clause seems to catch the first
> throw, but the loop I think continues, throws the error again, and that
> second throw is not caught.
>

It would appear that get items is a generator which uses the token exactly
once when it is first started; subsequent calls to the generator all use
the same token. You need to test the token; if it fails,
obtain a new one, then start the loop.

Bob Gailer

>
-- 
https://mail.python.org/mailman/listinfo/python-list


try/except in loop

2020-11-27 Thread Jason Friedman
I'm using the Box API (https://developer.box.com/guides/tooling/sdks/python/).
I can get an access token, though it expires after a certain amount of
time. My plan is to store the access token on the filesystem and use it
until it expires, then fetch a new one. In the example below assume I have
an expired access token.


# This next line does not throw an error:

client.folder('0').get_items()


# But iteration does (maybe this is a lazy fetch?)

for _ in client.folder('0').get_items():

logger.debug("Using existing access token.")

return access_token


# So I use try/except

try:

for _ in client.folder('0').get_items():

logger.debug("Using existing access token.")

return access_token

except boxsdk.exception.BoxAPIException:

pass # access token invalid, let's get one

else:

pass # access token invalid, let's get one


# When running the debugger the except clause seems to catch the first
throw, but the loop I think continues, throws the error again, and that
second throw is not caught.
-- 
https://mail.python.org/mailman/listinfo/python-list