Debian Codegraph @

doc/mime.rst

raw
1 .. _mime:
2
3 CurlMime Objects
4 ============
5
6 PycURL exposes libcurl's MIME tree API via ``CurlMime`` and ``CurlMimePart`` classes.
7
8 ``CurlMime`` offers both low-level wrappers and higher-level builder helpers:
9
10 - Low-level: ``addpart()`` and ``CurlMimePart`` methods such as ``name()``,
11 ``data()``, ``data_cb()``, ``filedata()`` and ``subparts()``.
12 - Builder helpers: ``add()``, ``add_field()``, ``add_file()`` and
13 ``add_multipart()``.
14 - ``CurlMimePart.data()`` also accepts objects exposing Python's buffer protocol
15 (for example ``bytearray`` and ``memoryview``), not only ``bytes``/ASCII ``str``.
16 - ``CurlMimePart.data_cb(datasize, read, seek=None, free=None, userdata=None)`` maps to libcurl
17 ``curl_mime_data_cb()`` for streaming content from callbacks.
18 ``read(userdata, max_bytes)`` uses the same return conventions as ``READFUNCTION``.
19 Optional ``seek(userdata, offset, origin)`` follows ``SEEKFUNCTION`` semantics and
20 should return ``SEEKFUNC_OK``, ``SEEKFUNC_FAIL`` or ``SEEKFUNC_CANTSEEK``.
21 Optional ``free(userdata)`` is called when libcurl releases the callback-backed part.
22
23 Example::
24
25 import pycurl
26
27 curl = pycurl.Curl()
28 mime = pycurl.CurlMime(curl)
29
30 mime.add_field("field1", "value1")
31 mime.add_file("upload", "/tmp/example.txt", content_type="text/plain")
32
33 nested = mime.add_multipart(name="attachments", subtype="mixed")
34 nested.add_field("meta", "nested-value")
35
36 .. note::
37
38 This is a first-draft API. It currently documents MIME object construction
39 and nesting. End-to-end request attachment via Curl options is documented
40 separately as that integration is finalized.
41
42 .. note::
43
44 Ownership and handle constraints:
45
46 - ``CurlMime`` objects passed to ``Curl.setopt(pycurl.MIMEPOST, mime)`` must be
47 top-level/owning MIME trees (not already attached via ``subparts()``).
48 - ``CurlMimePart.subparts(child)`` requires both ``CurlMime`` objects to use the
49 same ``Curl`` handle.
50 - A ``CurlMime`` currently set as ``MIMEPOST`` cannot be attached as ``subparts()``.
51 - ``Curl.duphandle()`` duplicates callback-backed MIME parts and shares the
52 callback ``userdata`` pointer between handles, matching libcurl behavior.
53
54
55 CurlMime
56 ----
57
58 .. autoclass:: pycurl.CurlMime
59
60 CurlMime objects have the following methods:
61
62 .. automethod:: pycurl.CurlMime.close
63
64 .. automethod:: pycurl.CurlMime.closed
65
66 .. automethod:: pycurl.CurlMime.addpart
67
68 .. automethod:: pycurl.CurlMime.add
69
70 .. automethod:: pycurl.CurlMime.add_field
71
72 .. automethod:: pycurl.CurlMime.add_file
73
74 .. automethod:: pycurl.CurlMime.add_multipart
75
76
77 CurlMimePart
78 --------
79
80 .. autoclass:: pycurl.CurlMimePart
81
82 CurlMimePart objects have the following methods:
83
84 .. automethod:: pycurl.CurlMimePart.name
85
86 .. automethod:: pycurl.CurlMimePart.data
87
88 .. automethod:: pycurl.CurlMimePart.data_cb
89
90 .. automethod:: pycurl.CurlMimePart.filedata
91
92 .. automethod:: pycurl.CurlMimePart.filename
93
94 .. automethod:: pycurl.CurlMimePart.type
95
96 .. automethod:: pycurl.CurlMimePart.encoder
97
98 .. automethod:: pycurl.CurlMimePart.headers
99
100 .. automethod:: pycurl.CurlMimePart.subparts