Repository: watson-developer-cloud/python-sdk

Issue:
WDS add document only allows files
The add_document method for discovery only allows file objects.  It should also allow the addition of data in a string buffer as well as file objects. 

Discussion:

Patch:
diff --git a/watson_developer_cloud/discovery_v1.py b/watson_developer_cloud/discovery_v1.py
index 829baee4..c90e971d 100644
--- a/watson_developer_cloud/discovery_v1.py
+++ b/watson_developer_cloud/discovery_v1.py
@@ -215,7 +215,13 @@ class DiscoveryV1(WatsonDeveloperCloudService):
                             params={'version': latest_version},
                             accept_json=True)
 
-    def add_document(self, environment_id, collection_id, fileinfo, metadata=None):
+    def add_document(self,
+                     environment_id,
+                     collection_id,
+                     file_info=None,
+                     file_data=None,
+                     mime_type=None,
+                     metadata=None):
         url_string = '/v1/environments/{0}/collections/{1}/documents'.format(
             environment_id, collection_id)
 
@@ -224,15 +230,24 @@ class DiscoveryV1(WatsonDeveloperCloudService):
         if metadata is None:
             metadata = {}
 
-        mime_type = mimetypes.guess_type(
-            fileinfo.name)[0] or 'application/octet-stream'
+        file_tuple = None
+
+        if file_info:
+            mime_type = mime_type or mimetypes.guess_type(
+                file_info.name)[0]
+            file_tuple = (file_info.name, file_info, mime_type)
+        elif file_data:
+            file_tuple = ('tmpfile', file_data, mime_type or
+                          'application/html')
 
         return self.request(method='POST',
                             url=url_string,
                             params=params,
                             data=metadata,
-                            files={'file': (fileinfo.name, fileinfo, mime_type),
-                                   'metadata': (None, json.dumps(metadata), 'application/json')},
+                            files={'file': file_tuple,
+                                   'metadata': (None,
+                                                json.dumps(metadata),
+                                                'application/json')},
                             accept_json=True)
 
     def test_document(self, environment_id, fileinfo, configuration_id=None, metadata=None):


Test patch:
diff --git a/test/test_discovery_v1.py b/test/test_discovery_v1.py
index 3c22464d..faab8148 100644
--- a/test/test_discovery_v1.py
+++ b/test/test_discovery_v1.py
@@ -330,9 +330,7 @@ def test_document():
                   content_type='application/json')
 
     with open(os.path.join(os.getcwd(), 'resources', 'simple.html')) as fileinfo:
-        conf_id = discovery.add_document(environment_id='envid',
-                                         collection_id='collid',
-                                         fileinfo=fileinfo)
+        conf_id = discovery.add_document(environment_id='envid', collection_id='collid', file_info=fileinfo)
         assert conf_id != None
 
     assert len(responses.calls) == 4
@@ -341,4 +339,20 @@ def test_document():
                               collection_id='collid',
                               document_id='docid')
 
-    assert len(responses.calls) == 5
\ No newline at end of file
+    assert len(responses.calls) == 5
+
+
+    conf_id = discovery.add_document(environment_id='envid', collection_id='collid', file_data='my string of file')
+
+
+    assert len(responses.calls) == 6
+
+    conf_id = discovery.add_document(environment_id='envid', collection_id='collid', file_data='my string of file',
+                                     mime_type='application/html')
+
+    assert len(responses.calls) == 7
+
+    conf_id = discovery.add_document(environment_id='envid', collection_id='collid', file_data='my string of file',
+                                     mime_type='application/html', metadata={'stuff': 'woot!'})
+
+    assert len(responses.calls) == 8