From b83644f3e6b725248fcc2686644b1d4d4e76b199 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Fri, 14 Jun 2019 15:35:06 +0000
Subject: [PATCH] Ensure file-like objects are opened in binary mode for XML
 uploads

---
 README.rst         | 2 +-
 arkindex/client.py | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/README.rst b/README.rst
index cf59484..a998a6e 100644
--- a/README.rst
+++ b/README.rst
@@ -201,7 +201,7 @@ Import transcriptions for a page from files in PAGE XML format
 
 .. code:: python
 
-   cli.send_xml('ImportTranskribusTranscriptions', id='PAGE_ID', body=open('file.xml'))
+   cli.send_xml('ImportTranskribusTranscriptions', id='PAGE_ID', body=open('file.xml', 'rb'))
 
 Download full logs for each Ponos task in a workflow
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/arkindex/client.py b/arkindex/client.py
index c93b72a..a90e86c 100644
--- a/arkindex/client.py
+++ b/arkindex/client.py
@@ -8,6 +8,7 @@ import yaml
 from arkindex.auth import TokenSessionAuthentication
 from arkindex.pagination import ResponsePaginator
 from arkindex.encoders import XMLEncoder
+from io import FileIO
 from time import sleep
 from urllib.parse import urlsplit, urlunsplit
 
@@ -178,9 +179,13 @@ class ArkindexClient(apistar.Client):
 
         :param str operation_id: An OpenAPI operation ID.
         :param body: The XML body.
-        :type body: str or bytes
+        :type body: str, bytes or a file-like object opened in binary mode
         :param kwargs: Other arguments sent to :meth:`ArkindexClient.custom_request`.
         """
+
+        if isinstance(body, FileIO):
+            assert body.mode == 'rb', 'File-like objects should be opened in binary mode'
+
         return self.custom_request(
             operation_id,
             content=body,
-- 
GitLab