Opened 5 years ago

Closed 3 years ago

#4017 enhancement closed fixed (fixed)

Add an IBodyProducer which produces bytes from a file-like object

Reported by: exarkun Owned by: exarkun
Priority: normal Milestone:
Component: web Keywords: httpclient IBodyProducer
Cc: Branch: branches/filebodyproducer-4017-2
(diff, github, buildbot, log)
Author: exarkun Launchpad Bug:

Description (last modified by exarkun)

#886 introduces an HTTP client API along with a new interface, IBodyProducer, for providing request bodies in a streaming manner. This interface isn't quite the same as IProducer, so existing producers don't quite work with it.

For convenience, we should provide an implementation of this interface based on file-like objects (along the lines of twisted.protocols.basic.FileSender). This will be useful both for the obvious reason (sending files as request bodies), as well as as an example of how to implement these, and will also be usable with StringIO instances to send data constructed in memory (eg simple form posts).

Change History (13)

comment:1 Changed 5 years ago by ivank

  • Keywords IBodyProducer added

comment:2 Changed 5 years ago by exarkun

  • Keywords httpclient added

comment:3 Changed 4 years ago by <automation>

  • Owner exarkun deleted

comment:4 Changed 4 years ago by djfroofy

  • Owner set to djfroofy
  • Status changed from new to assigned

comment:5 Changed 3 years ago by exarkun

  • Author set to exarkun
  • Branch set to branches/filebodyproducer-4017

(In [32067]) Branching to 'filebodyproducer-4017'

comment:6 Changed 3 years ago by exarkun

  • Keywords review added
  • Owner djfroofy deleted
  • Status changed from assigned to new

I took a crack at this because I thought it would be quick and easy. Oh well, I've certainly done worse than none out of two before.

Please find FileBodyProducer, tests, docs, and an example in the linked branch. Here are the build results.

comment:7 Changed 3 years ago by exarkun

  • Description modified (diff)
  • Summary changed from Add an IEntityBodyProducer which produces bytes from a file-like object to Add an IBodyProducer which produces bytes from a file-like object

comment:8 Changed 3 years ago by exarkun

One possible change that should be made to this code is that the producer should take responsibility for closing the file after it finishes reading from it or if stopProducing is called.

comment:9 Changed 3 years ago by therve

  • Owner set to exarkun
  • Instead of keeping a reference to the the cooperator object, you may only keep one to the cooperate method. It would prevent the need of that comment about the task module.
  • I don't have a really strong feeling for closing the file or not, but whatever you choose, it should be mentioned in the documentation (both the docstring and the howto).

Please merge!

comment:10 Changed 3 years ago by therve

  • Keywords review removed

comment:11 Changed 3 years ago by exarkun

(In [32154]) Address review feedback, plus

  • add docstrings to some test helpers
  • use the bound cooperate method instead of a Cooperator instance/cooperator module
  • close the input file when the FileBodyProducer is done with it (and update docs)

refs #4017

comment:12 Changed 3 years ago by exarkun

  • Branch changed from branches/filebodyproducer-4017 to branches/filebodyproducer-4017-2

(In [32155]) Branching to 'filebodyproducer-4017-2'

comment:13 Changed 3 years ago by exarkun

  • Resolution set to fixed
  • Status changed from new to closed

(In [32157]) Merge filebodyproducer-4017-2

Author: exarkun
Reviewer: therve
Fixes: #4017

Add twisted.web.client.FileBodyProducer for use with Agent to specify request
bodies based on file-like objects.

Note: See TracTickets for help on using tickets.