Skip to content

Commit 58783d8

Browse files
committed
add support for bulk_create_or_update_verbose endpoint
1 parent 513f4eb commit 58783d8

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- request: !!python/object:vcr.request.Request
2+
body: '{"documents": [{"fields": [{"type": "string", "name": "title"}], "external_id":
3+
"1"}]}'
4+
headers: !!python/object/apply:__builtin__.frozenset
5+
- - !!python/tuple [User-Agent, Swiftype-Python/1.1.0]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines/api-test/document_types/books/documents/bulk_create_or_update.json?auth_token=a-test-api-key
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '[false]'}
14+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
15+
content-type: application/json; charset=utf-8, etag: '"f9e544f77b7eac7add281ef28ca5559f"',
16+
server: thin 1.5.0 codename Knife, x-request-id: cb1c48ce2c3e88f890261ff3a53469b2,
17+
x-runtime: '0.024775', x-ua-compatible: IE=Edge}
18+
status: {code: 200, message: OK}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- request: !!python/object:vcr.request.Request
2+
body: '{"documents": [{"external_id": "1"}, {"external_id": "2"}]}'
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.1.0]
5+
- !!python/tuple [Content-Type, application/json]
6+
host: localhost
7+
method: POST
8+
path: /api/v1/engines/api-test/document_types/books/documents/bulk_create_or_update_verbose.json?auth_token=a-test-api-key
9+
port: 3000
10+
protocol: http
11+
response:
12+
body: {string: '[true,true]'}
13+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
14+
content-type: application/json; charset=utf-8, etag: '"bb009c6dcb5c622766ad5aa1ea1562d9"',
15+
server: thin 1.5.0 codename Knife, x-request-id: 8253f4aa4737371ea7e64c5ad42d3056,
16+
x-runtime: '0.041665', x-ua-compatible: IE=Edge}
17+
status: {code: 200, message: OK}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- request: !!python/object:vcr.request.Request
2+
body: '{"documents": [{"fields": [{"type": "string", "name": "title"}], "external_id":
3+
"1"}]}'
4+
headers: !!python/object/apply:__builtin__.frozenset
5+
- - !!python/tuple [User-Agent, Swiftype-Python/1.1.0]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines/api-test/document_types/books/documents/bulk_create_or_update_verbose.json?auth_token=a-test-api-key
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '["Invalid field definition: Field definition requires a ''value''
14+
attribute"]'}
15+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
16+
content-type: application/json; charset=utf-8, etag: '"45d92cba10f804a2e430c32f79c06377"',
17+
server: thin 1.5.0 codename Knife, x-request-id: 6d1f8c3bbce03e76f9391eaa0085a305,
18+
x-runtime: '0.023121', x-ua-compatible: IE=Edge}
19+
status: {code: 200, message: OK}

swiftype/swiftype.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def create_documents(self, engine_id, document_type_id, documents=[]):
6363
def create_or_update_documents(self, engine_id, document_type_id, documents=[]):
6464
return self.conn._post(self.__documents_path(engine_id, document_type_id) + '/bulk_create_or_update', data={'documents':documents})
6565

66+
def create_or_update_documents_verbose(self, engine_id, document_type_id, documents=[]):
67+
return self.conn._post(self.__documents_path(engine_id, document_type_id) + '/bulk_create_or_update_verbose', data={'documents':documents})
68+
6669
def update_document(self, engine_id, document_type_id, document_id, fields={}):
6770
return self.conn._put(self.__document_path(engine_id, document_type_id, document_id) + '/update_fields', data={'fields':fields})
6871

tests/test_swiftype.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ def test_create_or_update_documents(self):
9696
stati = self.client.create_or_update_documents(self.engine, self.document_type, docs)['body']
9797
self.assertEqual(stati, [True, True])
9898

99+
def test_create_or_update_documents_failure(self):
100+
with vcr.use_cassette('fixtures/create_or_update_documents_failure.yaml'):
101+
docs = [{'external_id': '1', 'fields': [{'type': 'string', 'name': 'title'}]}] # <= missing 'value'
102+
stati = self.client.create_or_update_documents(self.engine, self.document_type, docs)['body']
103+
self.assertEqual(stati, [False])
104+
105+
def test_create_or_update_documents_verbose(self):
106+
with vcr.use_cassette('fixtures/create_or_update_documents_verbose.yaml'):
107+
docs = [{'external_id': '1'}, {'external_id': '2'}]
108+
stati = self.client.create_or_update_documents_verbose(self.engine, self.document_type, docs)['body']
109+
self.assertEqual(stati, [True, True])
110+
111+
def test_create_or_update_documents_verbose_failure(self):
112+
with vcr.use_cassette('fixtures/create_or_update_documents_verbose_failure.yaml'):
113+
docs = [{'external_id': '1', 'fields': [{'type': 'string', 'name': 'title'}]}] # <= missing 'value'
114+
stati = self.client.create_or_update_documents_verbose(self.engine, self.document_type, docs)['body']
115+
self.assertRegexpMatches(stati[0], r'^Invalid field definition')
116+
99117
def test_update_document(self):
100118
with vcr.use_cassette('fixtures/update_document.yaml'):
101119
document_id = '2'

0 commit comments

Comments
 (0)