Commit 5aed83aa authored by Sven Franck's avatar Sven Franck

pass token as query parameter

parent f7257450
/** /**
* Youtube (Data) Storage. Type = "youtube". * Youtube (Data) Storage. Type = "youtube".
*/ */
/*global jIO, RSVP, UriTemplate, JSON */ /*global jIO, RSVP, UriTemplate, JSON, Query */
/*jslint nomen: true*/ /*jslint nomen: true*/
(function (jIO, RSVP, UriTemplate, JSON) { (function (jIO, RSVP, UriTemplate, JSON, Query) {
"use strict"; "use strict";
var GET_URL = "https://www.googleapis.com/youtube/v3/videos?" + var GET_URL = "https://www.googleapis.com/youtube/v3/videos?" +
...@@ -13,6 +13,22 @@ ...@@ -13,6 +13,22 @@
"part=snippet{&pageToken}&q={search}&type=video&maxResults=10{&key}", "part=snippet{&pageToken}&q={search}&type=video&maxResults=10{&key}",
ALLDOCS_TEMPLATE = UriTemplate.parse(ALLDOCS_URL); ALLDOCS_TEMPLATE = UriTemplate.parse(ALLDOCS_URL);
function handleError(error, id) {
if (error.target && error.target.status === 404) {
throw new jIO.util.jIOError("Cannot find document: " + id, 404);
}
throw error;
}
function getValue(query_list, key) {
var i;
for (i = 0; i < query_list.length; i += 1) {
if (query_list[i].key === key) {
return query_list[i].value;
}
}
}
/** /**
* The JIO Youtube Storage extension * The JIO Youtube Storage extension
* *
...@@ -53,15 +69,33 @@ ...@@ -53,15 +69,33 @@
}; };
YoutubeStorage.prototype.buildQuery = function (options) { YoutubeStorage.prototype.buildQuery = function (options) {
var key = this._api_key; var key,
query;
if (options.query === undefined) {
throw new jIO.util.jIOError("query parameter is required",
400);
}
key = this._api_key;
query = Query.parseStringToObject(options.query);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
var token,
search;
if (query.type === "simple") {
search = query.value;
token = "";
} else {
search = getValue(query.query_list, "q");
token = getValue(query.query_list, "token");
}
return jIO.util.ajax({ return jIO.util.ajax({
"type": "GET", "type": "GET",
"url": ALLDOCS_TEMPLATE.expand({ "url": ALLDOCS_TEMPLATE.expand({
"search": options.query, "search": search,
"key": key, "key": key,
"pageToken": options.token || "" "pageToken": token
}) })
}); });
}) })
...@@ -73,10 +107,8 @@ ...@@ -73,10 +107,8 @@
} }
obj.items.nextPageToken = obj.nextPageToken; obj.items.nextPageToken = obj.nextPageToken;
return obj.items; return obj.items;
})
.push(undefined, function (error) { }, handleError);
throw error;
});
}; };
YoutubeStorage.prototype.hasCapacity = function (name) { YoutubeStorage.prototype.hasCapacity = function (name) {
...@@ -85,4 +117,4 @@ ...@@ -85,4 +117,4 @@
jIO.addStorage('youtube', YoutubeStorage); jIO.addStorage('youtube', YoutubeStorage);
}(jIO, RSVP, UriTemplate, JSON)); }(jIO, RSVP, UriTemplate, JSON, Query));
/*jslint nomen: true */ /*jslint nomen: true */
/*global Blob, sinon, JSON*/ /*global Blob, sinon, JSON, Query, SimpleQuery, ComplexQuery*/
(function (jIO, QUnit, sinon, JSON) { (function (jIO, QUnit, sinon, JSON, Query, SimpleQuery, ComplexQuery) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
function getItem(id) { function getItem(id) {
return {"id": id, "value": {}}; return {"id": id, "value": {}};
} }
function setQuery(key, value) {
return new SimpleQuery({"key": key, "value": value, "type": "simple"});
}
function getResponse(item, has_token) { function getResponse(item, has_token) {
var obj = {"data": {"rows": [], "total_rows": 1}}; var obj = {"data": {"rows": [], "total_rows": 1}};
obj.data.rows.push(item); obj.data.rows.push(item);
...@@ -50,7 +53,6 @@ ...@@ -50,7 +53,6 @@
} }
return obj; return obj;
} }
function error404Tester(fun, encl, blob) { function error404Tester(fun, encl, blob) {
stop(); stop();
expect(3); expect(3);
...@@ -153,6 +155,23 @@ ...@@ -153,6 +155,23 @@
delete this.server; delete this.server;
} }
}); });
test("allDocs without query", function () {
stop();
expect(3);
this.jio.allDocs()
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "query parameter is required");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get all docs", function () { test("get all docs", function () {
var object_result_1 = getResponse(getItem("0B4kh3jbjOf5LamRlVCYXM")), var object_result_1 = getResponse(getItem("0B4kh3jbjOf5LamRlVCYXM")),
...@@ -163,7 +182,7 @@ ...@@ -163,7 +182,7 @@
stop(); stop();
expect(7); expect(7);
this.jio.allDocs() this.jio.allDocs({"query": Query.objectToSearchText(setQuery("q", ""))})
.then(function (res) { .then(function (res) {
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
...@@ -195,8 +214,9 @@ ...@@ -195,8 +214,9 @@
object_result_2 = getResponse(getItem("0B4kh3jbjOf5LamRlX21MZ", true)); object_result_2 = getResponse(getItem("0B4kh3jbjOf5LamRlX21MZ", true));
context.jio.allDocs() context.jio.allDocs({"query": Query.objectToSearchText(setQuery("q", ""))})
.then(function (res) { .then(function (res) {
var query;
equal(server.requests.length, 1); equal(server.requests.length, 1);
equal(server.requests[0].method, "GET"); equal(server.requests[0].method, "GET");
equal(server.requests[0].url, listUrl("")); equal(server.requests[0].url, listUrl(""));
...@@ -205,8 +225,18 @@ ...@@ -205,8 +225,18 @@
equal(server.requests[0].responseText, getSample()); equal(server.requests[0].responseText, getSample());
deepEqual(res, object_result_2); deepEqual(res, object_result_2);
// build complex query with query and token
query = Query.objectToSearchText(new ComplexQuery({
"operator": "AND",
"type": "complex",
"query_list": [
setQuery("q", ""),
setQuery("token", token)
]
}));
object_result_2 = getResponse(getItem("0B4kh3jbjOf5LamRlVCYXM")); object_result_2 = getResponse(getItem("0B4kh3jbjOf5LamRlVCYXM"));
return context.jio.allDocs({"token": token}); return context.jio.allDocs({"query": query});
}) })
.then(function (res) { .then(function (res) {
equal(server.requests.length, 2); equal(server.requests.length, 2);
...@@ -225,4 +255,4 @@ ...@@ -225,4 +255,4 @@
}); });
}); });
}(jIO, QUnit, sinon, JSON)); }(jIO, QUnit, sinon, JSON, Query, SimpleQuery, ComplexQuery));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment