Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #22 from cherimarie/curations
Browse files Browse the repository at this point in the history
Add functions for Curation API endpoints
  • Loading branch information
JasonStoltz committed Feb 15, 2019
2 parents c5078ee + aada4ec commit dfa4486
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 0 deletions.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,88 @@ client
.catch(error => console.log(error.errorMessages))
```

##### Listing Curations

```javascript
const engineName = 'favorite-videos'

client
.listCurations(engineName)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))

// Pagination details are optional
const paginationDetails = {
page: {
current: 2,
size: 10
}
}

client
.listCurations(engineName, paginationDetails)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))
```

##### Retrieving Curations

```javascript
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'

client
.getCuration(engineName, curationId)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))
```

##### Creating Curations

```javascript
const engineName = 'favorite-videos'
const newCuration = {
queries: ['cat blop'],
promoted: ['Jdas78932'],
hidden: ['INscMGmhmX4', 'JNDFojsd02']
}

client
.createCuration(engineName, newCuration)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))
```

##### Updating Curations

```javascript
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'
// "queries" is required, either "promoted" or "hidden" is required.
// Values sent for all fields will overwrite existing values.
const newDetails = {
queries: ['cat blop'],
promoted: ['Jdas78932', 'JFayf782']
}

client
.updateCuration(engineName, curationId, newDetails)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))
```

##### Deleting Curations

```javascript
const engineName = 'favorite-videos'
const curationId = 'cur-7438290'

client
.destroyCuration(engineName, curationId)
.then(response => console.log(response))
.catch(error => console.log(error.errorMessages))
```

## Running tests

The specs in this project use [node-replay](https://github.com/assaf/node-replay) to capture responses.
Expand Down
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775139235186519
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GET /api/as/v1/engines/swiftype-api-example/curations
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 18:56:32 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 401 Unauthorized
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: c3f4f569d971ad431d1287b3caa52c31
x-runtime: 0.035655
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"meta":{"page":{"current":1,"total_pages":1,"total_results":2,"size":25}},"results":[{"id":"cur-9382","queries":["mew hiss"],"promoted":["INscMGmhmX4"],"hidden":["JNDFojsd02"]},{"id":"cur-378291","queries":["grrr scratch"],"promoted":["JNDFojsd02"],"hidden":["INscMGmhmX4"]}]}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775139267473451
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GET /api/as/v1/engines/swiftype-api-example/curations?page[current]=2&page[size]=1
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 18:56:32 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
vary: Accept-Encoding, Accept-Encoding, Origin
status: 200 OK
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
cache-control: no-cache
x-request-id: 03632a579705aa38573c9fcbd29c0be2
x-runtime: 0.021305
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"meta":{"page":{"current":2,"total_pages":2,"total_results":2,"size":1}},"results":[{"id":"cur-378291","queries":["grrr scratch"],"promoted":["JNDFojsd02"],"hidden":["INscMGmhmX4"]}]}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775207819050958
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GET /api/as/v1/engines/swiftype-api-example/curations/cur-9382
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 19:07:58 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 200 OK
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: 401c83bae4b9e9be82acf7366a8a15bd
x-runtime: 0.017829
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"meta":{"page":{"current":1,"total_pages":1,"total_results":1,"size":25}},"results":[{"id":"cur-9382","queries":["mew hiss"],"promoted":["INscMGmhmX4"],"hidden":["JNDFojsd02"]}]}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775369860896529
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
POST /api/as/v1/engines/swiftype-api-example/curations
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {\"queries\":[\"arf arf\"],\"promoted\":[],\"hidden\":[\"INscMGmhmX4\",\"JNDFojsd02\"]}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 19:34:58 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 200 OK
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: ffc905295c0530ef21f71cf5c322ba91
x-runtime: 0.021898
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"id": "cur-9043829"}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775388696063661
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DELETE /api/as/v1/engines/swiftype-api-example/curations/cur-378291
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 19:38:06 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 200 OK
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: 83fb305ee12bfe40282325524789753f
x-runtime: 0.024097
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web01.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web01.dal05

{"deleted": true}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775411564681581
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PUT /api/as/v1/engines/swiftype-api-example/curations/cur-9382
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {\"queries\":[\"mew hiss\",\"sideways hop\"],\"promoted\":[\"INscMGmhmX4\"],\"hidden\":[\"JNDFojsd02\"]}

HTTP/1.1 200 OK
date: Thu, 17 Jan 2019 19:41:55 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 200 OK
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: c7f73cec2a3c5ac9177e93cf6ac32032
x-runtime: 0.020304
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"id": "cur-9382"}
29 changes: 29 additions & 0 deletions fixtures/host-c5s2mj.api.swiftype.com-443/154775710380210466
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GET /api/as/v1/engines/swiftype-api-example/curations/cur-0000
x-swiftype-client: swiftype-app-search-node
x-swiftype-client-version: 0.5.0
content-type: application/json
host: host-c5s2mj.api.swiftype.com
authorization: Bearer api-mu75psc5egt9ppzuycnc2mc3
accept: application/json
body: {}

HTTP/1.1 404 Not Found
date: Thu, 17 Jan 2019 20:31:43 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
status: 404 Not Found
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
www-authenticate: Basic realm="Swiftype"
vary: Origin
cache-control: no-cache
x-request-id: fc44266bdab7628ed1cdef8e6af334b0
x-runtime: 0.030466
x-swiftype-frontend-datacenter: dal05
x-swiftype-frontend-node: web02.dal05
x-swiftype-edge-datacenter: dal05
x-swiftype-edge-node: web02.dal05

{"errors":["Curation not found with id: cur-0000"]}
4 changes: 4 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Client {
post(path, params) {
return this._jsonRequest('POST', path, params)
}

put(path, params) {
return this._jsonRequest('PUT', path, params)
}

delete(path, params) {
return this._jsonRequest('DELETE', path, params)
Expand Down
61 changes: 61 additions & 0 deletions lib/swiftypeAppSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,67 @@ class SwiftypeAppSearchClient {
return this.client.delete(`engines/${encodeURIComponent(engineName)}`, {})
}


/**
* List all Curations
*
* @param {String} engineName unique Engine name
* @returns {Promise<Object>} a Promise that returns a result {Object} when resolved, otherwise throws an Error.
*/
listCurations(engineName, options = {}) {
const pagingParams = buildPagingParams(options.page || {});
const prefix = pagingParams.length ? '?' : '';

return this.client.get(`engines/${encodeURIComponent(engineName)}/curations${prefix}${pagingParams.join('&')}`, {})
}

/**
* Retrieve a Curation by id
*
* @param {String} engineName unique Engine name
* @param {String} curationId unique Curation id
* @returns {Promise<Object>} a Promise that returns a result {Object} when resolved, otherwise throws an Error.
*/
getCuration(engineName, curationId) {

return this.client.get(`engines/${encodeURIComponent(engineName)}/curations/${encodeURIComponent(curationId)}`, {})
}

/**
* Create a new Curation
*
* @param {String} engineName unique Engine name
* @param {Object} newCuration body of the Curation object
* @returns {Promise<Object>} a Promise that returns a result {Object} when resolved, otherwise throws an Error.
*/
createCuration(engineName, newCuration) {
return this.client.post(`engines/${encodeURIComponent(engineName)}/curations`, newCuration)
}

/**
* Update an existing curation
*
* @param {String} engineName unique Engine name
* @param {String} curationId unique Curation id
* @param {Object} newCuration body of the Curation object
* @returns {Promise<Object>} a Promise that returns a result {Object} when resolved, otherwise throws an Error.
*/
updateCuration(engineName, curationId, newCuration) {
return this.client.put(`engines/${encodeURIComponent(engineName)}/curations/${encodeURIComponent(curationId)}`, newCuration)
}

/**
* Delete a curation
*
* @param {String} engineName unique Engine name
* @param {String} curationId unique Curation name
* @returns {Promise<Object>} a Promise that returns a result {Object} when resolved, otherwise throws an Error.
*/
destroyCuration(engineName, curationId) {
return this.client.delete(`engines/${encodeURIComponent(engineName)}/curations/${encodeURIComponent(curationId)}`, {})
}


/**
* Creates a jwt search key that can be used for authentication to enforce a set of required search options.
*
Expand Down

0 comments on commit dfa4486

Please sign in to comment.