Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit fc8786d

Browse files
CorrosiveKidJasonStoltz
authored andcommitted
Client methods to get and update schema for an engine (#23)
Add client methods to get and create/update engine schema
1 parent 228d567 commit fc8786d

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ The client can be configured to use a managed deploy by adjusting the `base_endp
145145
[{'id': 'INscMGmhmX4','result': True}]
146146
```
147147

148+
### Get Schema
149+
150+
```python
151+
>>> engine_name = 'favorite-videos'
152+
>>> client.get_schema(engine_name)
153+
{'name':'text', 'square_km': 'number', 'square_mi': 'text'}
154+
```
155+
156+
### Create/Update Schema
157+
158+
```python
159+
>>> engine_name = 'favorite-videos'
160+
>>> client.update_schema(engine_name, {'square_km': 'text'})
161+
{'square_km': 'text'}
162+
>>> client.update_schema(engine_name, {'square_mi': 'text'})
163+
{'square_km': 'text', 'square_mi': 'text'}
164+
>>> client.update_schema(engine_name, {'square_km': 'number'})
165+
{'square_km': 'number', 'square_mi': 'text'}
166+
```
167+
148168
### List Engines
149169

150170
```python

swiftype_app_search/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ def destroy_documents(self, engine_name, document_ids):
107107
data = json.dumps(document_ids)
108108
return self.swiftype_session.request('delete', endpoint, data=data)
109109

110+
def get_schema(self, engine_name):
111+
"""
112+
Get current schema for an engine.
113+
114+
:param engine_name: Name of engine.
115+
:return: Schema.
116+
"""
117+
endpoint = "engines/{}/schema".format(engine_name)
118+
return self.swiftype_session.request('get', endpoint)
119+
120+
def update_schema(self, engine_name, schema):
121+
"""
122+
Create new schema fields or update the fields if they already exists.
123+
124+
:param engine_name: Name of engine.
125+
:param schema: Schema to be updated as dict.
126+
:return: Updated schema.
127+
"""
128+
endpoint = "engines/{}/schema".format(engine_name)
129+
data = json.dumps(schema)
130+
return self.swiftype_session.request('post', endpoint, data=data)
131+
110132
def list_engines(self, current=1, size=20):
111133
"""
112134
Lists engines that the api key has access to.

tests/test_client.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ def test_destroy_documents(self):
136136
response = self.client.destroy_documents(self.engine_name, [id])
137137
self.assertEqual(response, expected_return)
138138

139+
def test_get_schema(self):
140+
expected_return = {
141+
'square_km': 'text'
142+
}
143+
144+
with requests_mock.Mocker() as m:
145+
url = "{}/engines/{}/schema".format(self.client.swiftype_session.base_url, self.engine_name)
146+
m.register_uri('GET',
147+
url,
148+
json=expected_return,
149+
status_code=200
150+
)
151+
152+
response = self.client.get_schema(self.engine_name)
153+
self.assertEqual(response, expected_return)
154+
155+
def test_update_schema(self):
156+
expected_return = {
157+
'square_mi': 'number',
158+
'square_km': 'number'
159+
}
160+
161+
with requests_mock.Mocker() as m:
162+
url = "{}/engines/{}/schema".format(self.client.swiftype_session.base_url, self.engine_name)
163+
m.register_uri('POST',
164+
url,
165+
json=expected_return,
166+
status_code=200
167+
)
168+
169+
response = self.client.update_schema(self.engine_name, expected_return)
170+
self.assertEqual(response, expected_return)
171+
139172
def test_list_engines(self):
140173
expected_return = [
141174
{ 'name': 'myawesomeengine' }

0 commit comments

Comments
 (0)