canvasapi icon indicating copy to clipboard operation
canvasapi copied to clipboard

#629 Implement default request timeouts

Open ArhanChaudhary opened this issue 1 year ago • 3 comments

Proposed Changes

Adds a default API request timeout. I originally wanted to have a timeout keyword argument for every Requester.request call, but there were far too many instances of self._requester.request in the codebase, so I've instead insisted on a global default timeout.

I was unsure how to write the unit test for the mocked request wait and raise a ReadTimeout, so I'll just have to leave that up to a reviewer.

Fixes #629 .

ArhanChaudhary avatar Jun 15 '23 06:06 ArhanChaudhary

A couple of preliminary thoughts:

  1. I like the idea of having a timeout, but the nuance of timeouts in requests may trip people up. According to the docs, providing an int will apply to the read and write timeouts. Would a tuple make more sense so the user explicitly has to set both values?
  2. Related, this is not an upper limit on download time, just time to first bytes received. Does that matter?
  3. Do we need to subclass CanvasException to handle the Timeout exception emitted by requests?
  4. A unit test with a sleep function greater than the timeout set on Canvas would be fine to make sure the error is captured correctly.

bennettscience avatar Jun 15 '23 14:06 bennettscience

Hello @bennettscience, thanks for your thoughts. I'll commit and address your insights once I get feedback on my response:

  1. Sure, as it's consistent with the requests module.
  2. No, for the sake of consistency. Considering how widely used the requests module is for example with other API wrappers, having a timeout functionality that has consistent behavior is easier to understand. On second thought, maybe it's better to rename default_timeout to just timeout to emphasize that its interface mirrors the requests module's.
  3. Definitely. I didn't think about that initially. Though I would prefer if it instead subclassed ReadTimeout for safety, as that's what's explicitly thrown when the request timeout exceeds.
  4. Agreed. Would it work if I add a exc=None kwarg to register_uris and pass that into requests_mocker.register_uri so ReadTimeouts can be caught in a with self.assertRaises(ReadTimeout): block?

ArhanChaudhary avatar Jun 15 '23 19:06 ArhanChaudhary