-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTI.js
91 lines (78 loc) · 2.58 KB
/
TI.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
'use strict';
//The library is compliant with requirejs
//http://stackoverflow.com/questions/17524364/make-my-javascript-function-requirejs-amd-friendly
//http://stackoverflow.com/questions/15613577/correct-way-to-implement-jquery-with-require-js/15649611
(function(module) {
if (typeof define === "function" && define.amd) {
define("tiapi", ['jquery'], function($) {
return module.define_TILibrary();
});
} else {
window.TI = module.define_TILibrary();
}
}({
define_TILibrary: function() {
var TILibrary = {};
TILibrary.version = "0.1"
TILibrary.token = ""
TILibrary.backend = "https://beta-api.travel-intelligence.com/"
TILibrary.configure = function(backend) {
TILibrary.backend = backend;
}
TILibrary.setToken = function(token) {
TILibrary.token = token;
}
TILibrary.login = function(email, pass, callback) {
$.ajax({
type: "POST",
url: TILibrary.backend + "api/v1/session",
data: {
"session": {
"email": email,
"password": pass
}
}
}).done(function(msg) {
var token = msg.session.auth_token
TILibrary.token = token
callback(msg)
})
}
//Logout
TILibrary.logout = function(callback) {
//Logged out
$.ajax({
type: "DELETE",
url: TILibrary.backend + "api/v1/session",
headers: {
"Authorization": "Token " + TILibrary.token
}
}).done(function(msg) {
callback()
});
}
TILibrary.callWS = function(service, params, callback, errorCallback) {
var url = TILibrary.backend;
url += service
if (params) {
url += "?" + Object.keys(params).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(params[k])
}).join('&')
}
$.ajax({
type: "GET",
url: url,
headers: {
"Authorization": "Token " + TILibrary.token
}
})
.done(callback)
.fail(function(error) {
if (errorCallback){
errorCallback(error)
}
});
}
return TILibrary;
}
}));