forked from mozilla/gecko-dev
/
client.js
113 lines (101 loc) · 3.44 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global loop*/
var loop = loop || {};
loop.Client = (function($) {
"use strict";
/**
* Loop server client.
*
* @param {Object} settings Settings object.
*/
function Client(settings) {
settings = settings || {};
if (!settings.hasOwnProperty("baseApiUrl")) {
throw new Error("missing required baseApiUrl");
}
this.settings = settings;
}
Client.prototype = {
/**
* Requests a call URL to the Loop server.
*
* @param {String} simplepushUrl a registered Simple Push URL
* @param {string} nickname the nickname of the future caller
* @param {Function} cb Callback(err, callUrl)
*/
requestCallUrl: function(nickname, cb) {
var endpoint = this.settings.baseApiUrl + "/call-url/",
reqData = {callerId: nickname};
function validate(callUrlData) {
if (typeof callUrlData !== "object" ||
!callUrlData.hasOwnProperty("call_url")) {
var message = "Invalid call url data received";
console.error(message, callUrlData);
throw new Error(message);
}
return callUrlData.call_url;
}
var req = $.post(endpoint, reqData, function(callUrlData) {
try {
cb(null, validate(callUrlData));
} catch (err) {
cb(err);
}
}, "json");
req.fail(function(jqXHR, testStatus, errorThrown) {
var error = "Unknown error.";
if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.error) {
error = jqXHR.responseJSON.error;
}
var message = "HTTP error " + jqXHR.status + ": " +
errorThrown + "; " + error;
console.error(message);
cb(new Error(message));
});
},
/**
* Requests call information from the server
*
* @param {Function} cb Callback(err, calls)
*/
requestCallsInfo: function(cb) {
var endpoint = this.settings.baseApiUrl + "/calls";
// We do a basic validation here that we have an object,
// and pass the full information back.
function validate(callsData) {
if (typeof callsData !== "object" ||
!callsData.hasOwnProperty("calls")) {
var message = "Invalid calls data received";
console.error(message, callsData);
throw new Error(message);
}
return callsData.calls;
}
// XXX We'll want to figure out a way to store the version from each
// request here. As this is typically the date, we just need to store the
// time last requested.
// XXX It is likely that we'll want to move some of this to whatever
// opens the chat window.
var req = $.get(endpoint + "?version=0", function(callsData) {
try {
cb(null, validate(callsData));
} catch (err) {
cb(err);
}
}, "json");
req.fail(function(jqXHR, testStatus, errorThrown) {
var error = "Unknown error.";
if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.error) {
error = jqXHR.responseJSON.error;
}
var message = "HTTP error " + jqXHR.status + ": " +
errorThrown + "; " + error;
console.error(message);
cb(new Error(message));
});
}
};
return Client;
})(jQuery);