Skip to content

Commit e58406b

Browse files
author
Daniel Pereira Sequeira
committed
added collections
1 parent 557b6f6 commit e58406b

8 files changed

+209
-26
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ Here's a list of all the methods available, separated by module:
8282
* `saveNewMetaproperty(object)`
8383
* `deleteMetaproperty(object)`
8484

85+
### Collections
86+
* `getCollections(queryObject)`
87+
* `getCollection(queryObject)`
88+
8589
### Tags
8690
* `getTags()`
8791

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"name": "bynder-js-sdk",
2+
"name": "@bynder/bynder-js-sdk",
33
"version": "1.0.0",
44
"description": "Bynder Javascript SDK",
5-
"main": "bynder-js-sdk.js",
5+
"main": "./dist/bynder-js-sdk.js",
66
"scripts": {
7-
"test": "jasmine tests/*"
7+
"test": "jasmine tests/*",
8+
"build": "gulp babel",
9+
"prepublish": "npm run build"
810
},
911
"author": "Bynder",
1012
"license": "MIT",

samples/collections.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8">
4+
<title>Collections sample</title>
5+
<script src="../dist/bundle.js"></script>
6+
</head>
7+
<body>
8+
<ul id="collections-container">
9+
</ul>
10+
11+
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
12+
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
13+
<script src="https://cure53.de/purify.js"></script>
14+
<script type="text/babel">
15+
const Bynder = Bynder.default;
16+
17+
function appendCollections(collections) {
18+
const collectionsContainer = document.getElementById('collections-container');
19+
collections.forEach((collection) => {
20+
collectionsContainer.innerHTML += `<li>${DOMPurify.sanitize(collection.name)}</li>`;
21+
});
22+
}
23+
24+
axios('../secret.json')
25+
.then((response) => {
26+
if (response.status < 300) {
27+
return response.data;
28+
}
29+
return Promise.reject(response);
30+
})
31+
.then((configs) => {
32+
const bynder = new Bynder(configs);
33+
return bynder.getCollections();
34+
})
35+
.then((data) => {
36+
appendCollections(data);
37+
console.log(data);
38+
})
39+
.catch((error) => {
40+
console.error(error);
41+
});
42+
</script>
43+
</body>
44+
</html>

samples/collections.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Bynder = require('../dist/bynder-js-sdk.js').default;
2+
const configs = require('../secret.json');
3+
4+
const bynder = new Bynder(configs);
5+
6+
bynder.getCollections({
7+
type: 'image',
8+
limit: 9,
9+
page: 1
10+
})
11+
.then((data) => {
12+
console.log('getCollections', data, '\n\n');
13+
const collectionId = data[0].id || '';
14+
return bynder.getCollection({
15+
id: collectionId
16+
});
17+
})
18+
.then((data) => {
19+
console.log('getCollection', data, '\n\n');
20+
})
21+
.catch((error) => {
22+
console.log(error);
23+
});

src/bynder-js-sdk.js

+47
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ export default class Bynder {
501501
);
502502
return request.send();
503503
}
504+
504505
/**
505506
* Get all the tags
506507
* @see {@link http://docs.bynder.apiary.io/#reference/tags/tags-access/retrieve-entry-point|API Call}
@@ -520,4 +521,50 @@ export default class Bynder {
520521
);
521522
return request.send();
522523
}
524+
525+
/**
526+
* Get collections according to the parameters provided
527+
* @see {@link http://docs.bynder.apiary.io/#reference/tags/tags-operations-on-assets/retrieve-collections|API Call}
528+
* @return {Promise} Collections - Returns a Promise that, when fulfilled, will either return an Array with the
529+
* collections or an Error with the problem.
530+
*/
531+
getCollections(queryObject = {}) {
532+
if (!this.validURL()) {
533+
return rejectURL();
534+
}
535+
const request = new APICall(
536+
this.baseURL,
537+
'v4/collections/',
538+
'GET',
539+
this.consumerToken,
540+
this.accessToken,
541+
queryObject
542+
);
543+
return request.send();
544+
}
545+
546+
/**
547+
* Get the collection information according to the id provided.
548+
* @see {@link http://docs.bynder.apiary.io/#reference/tags/tags-operations-on-assets/retrieve-specific-collection|API Call}
549+
* @param {Object} queryObject={} - An object containing the id of the desired collection.
550+
* @param {String} queryObject.id - The id of the desired collection.
551+
* @return {Promise} Collection - Returns a Promise that, when fulfilled, will either return an Object with the
552+
* collection or an Error with the problem.
553+
*/
554+
getCollection(queryObject) {
555+
if (!this.validURL()) {
556+
return rejectURL();
557+
}
558+
if (!queryObject.id) {
559+
return rejectValidation('collection', 'id');
560+
}
561+
const request = new APICall(
562+
this.baseURL,
563+
`v4/collections/${queryObject.id}/`,
564+
'GET',
565+
this.consumerToken,
566+
this.accessToken
567+
);
568+
return request.send();
569+
}
523570
}

tests/collections.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-env jasmine */
2+
3+
const Bynder = require('../dist/bynder-js-sdk.js').default;
4+
const configs = require('../secret.json');
5+
6+
let randomCollectionId;
7+
8+
describe('Get collections', () => {
9+
let bynder;
10+
let collections;
11+
12+
beforeEach((done) => {
13+
bynder = new Bynder(configs);
14+
15+
bynder.getCollections()
16+
.then((data) => {
17+
collections = data;
18+
const randomIndex = Math.floor(Math.random() * collections.length);
19+
randomCollectionId = collections[randomIndex].id;
20+
done();
21+
})
22+
.catch((error) => {
23+
collections = error;
24+
done();
25+
});
26+
});
27+
28+
it('Get all the collections', () => {
29+
expect(Array.isArray(collections)).toEqual(true);
30+
expect(randomCollectionId).not.toBeUndefined();
31+
});
32+
});
33+
34+
describe('Get collection', () => {
35+
let bynder;
36+
let collection;
37+
38+
beforeEach((done) => {
39+
bynder = new Bynder(configs);
40+
41+
bynder.getCollection({
42+
id: randomCollectionId
43+
}).then((data) => {
44+
collection = data;
45+
done();
46+
})
47+
.catch((error) => {
48+
collection = error;
49+
done();
50+
});
51+
});
52+
53+
it('Get one collection', () => {
54+
expect(collection.constructor).toEqual(Object);
55+
const collectionKeys = Object.keys(collection);
56+
expect(collectionKeys).toContain('name');
57+
expect(collectionKeys).toContain('link');
58+
expect(collectionKeys).toContain('views');
59+
expect(collectionKeys).toContain('datecreated');
60+
});
61+
});

tests/media.js

+24-23
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,30 @@ describe('Media batch testing 1', () => {
221221

222222
beforeAll((done) => {
223223
bynder = new Bynder(configs);
224-
bynder.getAllMediaItems()
225-
.then((data) => {
226-
getAllAssetsResponse = data;
227-
return bynder.getMediaList();
228-
})
229-
.then((data) => {
230-
getAssetsResponse = data;
231-
return bynder.getMediaTotal();
232-
})
233-
.then((data) => {
234-
getAssetsTotal = data;
235-
testAssetRandomIndex = Math.floor(Math.random() * getAllAssetsResponse.length);
236-
testAssetId = getAllAssetsResponse[testAssetRandomIndex].id;
237-
testAssetDescriptionBackup = getAllAssetsResponse[testAssetRandomIndex].description;
238-
return bynder.editMedia({
239-
id: testAssetId,
240-
description: testDescriptionString
224+
bynder
225+
.getAllMediaItems()
226+
.then((data) => {
227+
getAllAssetsResponse = data;
228+
return bynder.getMediaList();
229+
})
230+
.then((data) => {
231+
getAssetsResponse = data;
232+
return bynder.getMediaTotal();
233+
})
234+
.then((data) => {
235+
getAssetsTotal = data;
236+
testAssetRandomIndex = 2;// Math.floor(Math.random() * getAllAssetsResponse.length);
237+
testAssetId = getAllAssetsResponse[testAssetRandomIndex].id;
238+
testAssetDescriptionBackup = getAllAssetsResponse[testAssetRandomIndex].description;
239+
return bynder.editMedia({
240+
id: testAssetId,
241+
description: testDescriptionString
242+
});
243+
})
244+
.then((data) => {
245+
editMediaResult = data;
246+
done();
241247
});
242-
})
243-
.then((data) => {
244-
editMediaResult = data;
245-
done();
246-
});
247248
});
248249

249250
it('The variable getAllAssets should be defined', () => {
@@ -284,7 +285,7 @@ describe('Media batch testing 2', () => {
284285
getAssetResponse = data;
285286
done();
286287
});
287-
}, 5000);
288+
}, 5000); // Sometimes the API returns the old value if the query call is made immediately after the edit call
288289
});
289290

290291
it('Description was edited successfully', () => {

0 commit comments

Comments
 (0)