Skip to content

Commit 6953878

Browse files
committed
add platform support for resources on behalf of users
1 parent f807adb commit 6953878

7 files changed

+126
-4
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: '{"document": {"external_id": "doc_id"}}'
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.0.0]
5+
- !!python/tuple [Authorization, Bearer 6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines/myusersengine/document_types/videos/documents.json
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '{"external_id":"doc_id","engine_id":"52cca3f13ae740f4af000023","document_type_id":"52cca4a43ae740f4af000024","id":"52cca54f3ae740f4af000026","_id":"52cca54f3ae740f4af000026","updated_at":"2014-01-08T01:09:35Z"}'}
14+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
15+
content-type: application/json; charset=utf-8, etag: '"2f47ab1d3d64e1f401758e0c1adba376"',
16+
server: thin 1.5.0 codename Knife, x-request-id: e3747b602f410044648906559a79ce4f,
17+
x-runtime: '0.176010', x-ua-compatible: IE=Edge}
18+
status: {code: 200, message: OK}
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: '{"document_type": {"name": "videos"}}'
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.0.0]
5+
- !!python/tuple [Authorization, Bearer 6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines/myusersengine/document_types.json
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '{"id":"52cca4a43ae740f4af000024","name":"videos","slug":"videos","engine_id":"52cca3f13ae740f4af000023","updated_at":"2014-01-08T01:06:44Z","document_count":0,"field_mapping":{}}'}
14+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
15+
content-type: application/json; charset=utf-8, etag: '"18cf4a2934702f8d1872273ee78b544c"',
16+
server: thin 1.5.0 codename Knife, x-request-id: 5df4547c828f6c5f32852192d3fefe12,
17+
x-runtime: '0.131149', x-ua-compatible: IE=Edge}
18+
status: {code: 200, message: OK}
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": [{"external_id": "doc_id1"}, {"external_id": "doc_id2"}]}'
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.0.0]
5+
- !!python/tuple [Authorization, Bearer 6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines/myusersengine/document_types/videos/documents/bulk_create.json
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '[true,true]'}
14+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
15+
content-type: application/json; charset=utf-8, etag: '"bb009c6dcb5c622766ad5aa1ea1562d9"',
16+
server: thin 1.5.0 codename Knife, x-request-id: 2453d94eb997eaa1501e4afcb91b1372,
17+
x-runtime: '0.143837', x-ua-compatible: IE=Edge}
18+
status: {code: 200, message: OK}

fixtures/platform_engine_create.yaml

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: '{"engine": {"name": "myusersengine"}}'
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.0.0]
5+
- !!python/tuple [Authorization, Bearer 6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: POST
9+
path: /api/v1/engines.json
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '{"name":"myusersengine","slug":"myusersengine","key":"wLDDEvF4GetXAkx6xiss","id":"52ccaa333ae740f4af00002b","_id":"52ccaa333ae740f4af00002b","updated_at":"2014-01-08T01:30:27Z","document_count":0}'}
14+
headers: {cache-control: 'max-age=0, private, must-revalidate', connection: close,
15+
content-type: application/json; charset=utf-8, etag: '"8240b4e5c212143eb3e570e548c4d8d5"',
16+
server: thin 1.5.0 codename Knife, x-request-id: 7ba05f6eb9c0d05aad3ce4ca5b1d8598,
17+
x-runtime: '0.146829', x-ua-compatible: IE=Edge}
18+
status: {code: 200, message: OK}

fixtures/platform_engine_destroy.yaml

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: ''
3+
headers: !!python/object/apply:__builtin__.frozenset
4+
- - !!python/tuple [User-Agent, Swiftype-Python/1.0.0]
5+
- !!python/tuple [Authorization, Bearer 6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e]
6+
- !!python/tuple [Content-Type, application/json]
7+
host: localhost
8+
method: DELETE
9+
path: /api/v1/engines/myuserengine.json
10+
port: 3000
11+
protocol: http
12+
response:
13+
body: {string: '{"error":"Permission denied."}'}
14+
headers: {cache-control: no-cache, connection: close, content-type: application/json;
15+
charset=utf-8, server: thin 1.5.0 codename Knife, x-request-id: 1ce7984333c15a3fb5c158dadddb61e8,
16+
x-runtime: '0.021690', x-ua-compatible: IE=Edge}
17+
status: {code: 403, message: Forbidden}

swiftype/swiftype.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
class Client(object):
1515

16-
def __init__(self, username=None, password=None, api_key=None, client_id=None, client_secret=None, host=DEFAULT_API_HOST):
16+
def __init__(self, username=None, password=None, api_key=None, access_token=None, client_id=None, client_secret=None, host=DEFAULT_API_HOST):
1717
self.client_id = client_id
1818
self.client_secret = client_secret
19-
self.conn = Connection(username=username, password=password, api_key=api_key, host=host, base_path=DEFAULT_API_BASE_PATH)
19+
self.conn = Connection(username=username, password=password, api_key=api_key, access_token=access_token, host=host, base_path=DEFAULT_API_BASE_PATH)
2020

2121
def engines(self, page=None, per_page=None):
2222
return self.conn._get(self.__engines_path(), self.__pagination_params(page, per_page))
@@ -170,10 +170,11 @@ def __init__(self, status, msg):
170170

171171
class Connection(object):
172172

173-
def __init__(self, username=None, password=None, api_key=None, host=None, base_path=None):
173+
def __init__(self, username=None, password=None, api_key=None, access_token=None, host=None, base_path=None):
174174
self.__username = username
175175
self.__password = password
176176
self.__api_key = api_key
177+
self.__access_token = access_token
177178
self.__host = host
178179
self.__base_path = base_path
179180

@@ -190,7 +191,6 @@ def _put(self, path, params={}, data={}):
190191
return self._request('PUT', path, params=params, data=data)
191192

192193
def _request(self, method, path, params={}, data={}):
193-
194194
headers = {}
195195
headers['User-Agent'] = USER_AGENT
196196
headers['Content-Type'] = 'application/json'
@@ -200,6 +200,9 @@ def _request(self, method, path, params={}, data={}):
200200
base64_credentials = base64.encodestring(credentials)
201201
authorization = "Basic %s" % base64_credentials[:-1]
202202
headers['Authorization'] = authorization
203+
elif self.__access_token is not None and self.__api_key is None:
204+
params.pop('auth_token', None)
205+
headers['Authorization'] = "Bearer %s" % self.__access_token
203206
elif self.__api_key is not None:
204207
params['auth_token'] = self.__api_key
205208
else:

tests/test_swiftype.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,35 @@ def test_create_user(self):
302302
response = self.client.create_user()
303303
self.assertEqual(response['status'], 200)
304304

305+
class TestPlatformResources(unittest.TestCase):
306+
307+
def setUp(self):
308+
access_token = '6cf7fbd297f00a8e3863a0595f55ff7d141cbef2fcbe00159d0f7403649b384e'
309+
self.engine = 'myusersengine'
310+
self.document_type = 'videos'
311+
self.client = swiftype.Client(access_token=access_token, host='localhost:3000')
312+
313+
def test_platform_engine_create(self):
314+
with vcr.use_cassette('fixtures/platform_engine_create.yaml'):
315+
response = self.client.create_engine(self.engine)
316+
self.assertEqual(response['body']['name'], self.engine)
317+
318+
def test_platform_create_document_type(self):
319+
with vcr.use_cassette('fixtures/platform_create_document_type.yaml'):
320+
response = self.client.create_document_type(self.engine, self.document_type)
321+
self.assertEqual(response['body']['slug'], self.document_type)
322+
323+
def test_platform_create_document(self):
324+
with vcr.use_cassette('fixtures/platform_create_document.yaml'):
325+
doc_id = 'doc_id'
326+
id = self.client.create_document(self.engine, self.document_type, {'external_id': doc_id})['body']['external_id']
327+
self.assertEqual(id, doc_id)
328+
329+
def test_platform_create_documents(self):
330+
with vcr.use_cassette('fixtures/platform_create_documents.yaml'):
331+
docs = [{'external_id': 'doc_id1'}, {'external_id': 'doc_id2'}]
332+
stati = self.client.create_documents(self.engine, self.document_type, docs)['body']
333+
self.assertEqual(stati, [True, True])
334+
305335
if __name__ == '__main__':
306336
unittest.main()

0 commit comments

Comments
 (0)