1 | REST = {
|
---|
2 |
|
---|
3 | resquests: {},
|
---|
4 |
|
---|
5 | taksResquests: {},
|
---|
6 |
|
---|
7 | isProcessRefreshToken: false,
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * This method is used to read resources from the server.
|
---|
11 | *
|
---|
12 | * @param uri Uri of the resource that gonna be readed.
|
---|
13 | * @param callback A function that is called when the resource is loaded successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
14 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
15 | * @return The target resource when the callback is ommitted ( made it synchronous )
|
---|
16 | */
|
---|
17 |
|
---|
18 | get: function( uri, data, callback, accept ){
|
---|
19 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'get', accept || 'json' ], data, callback, !!!callback);
|
---|
20 |
|
---|
21 | },
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This method is used to create resources from the server.
|
---|
25 | *
|
---|
26 | * @param uri Uri of the resource that gonna be readed.
|
---|
27 | * @param callback A function that is called when the resource is created on the server successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
28 | * @param accept The attribute accept that is used to ask the target format to the server.
|
---|
29 | * @return The result response of the create from the server when the callback is ommitted ( made it synchronous )
|
---|
30 | */
|
---|
31 |
|
---|
32 | post: function( uri, data, callback, accept ){
|
---|
33 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'post', accept || 'json' ], data, callback, !!!callback);
|
---|
34 | },
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * This method is used to update resources from the server.
|
---|
38 | *
|
---|
39 | * @param uri Uri of the resource that gonna be readed.
|
---|
40 | * @param callback A function that is called when the resource is update on the server successfully. When the parameter is ignored the request is made synchrounsly.
|
---|
41 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
42 | * @return The result response of the update from the server when the callback is ommitted ( made it synchronous )
|
---|
43 | */
|
---|
44 |
|
---|
45 | put: function( uri, data, callback, accept ){
|
---|
46 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'put', accept || 'json' ], data, callback, !!!callback);
|
---|
47 |
|
---|
48 | },
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This method is used to delete resources from the server.
|
---|
52 | *
|
---|
53 | * @param uri Uri of the resource that gonna be readed.
|
---|
54 | * @param callback A function that is called when the resource is deleted successfully in the server. When the parameter is ignored the request is made synchrounsly.
|
---|
55 | * @param accept The attribute accept that is used to ask the target format to the server
|
---|
56 | * @return The result response of the delete from the server when the callback is ommitted ( made it synchronous )
|
---|
57 | */
|
---|
58 |
|
---|
59 | "delete": function( uri, callback, accept ){
|
---|
60 | return this.send( (this.dispatchPath + '../rest' + uri), [ 'delete', accept || 'json' ], false, callback, !!!callback);
|
---|
61 | },
|
---|
62 |
|
---|
63 | send: function( url, type, data, callback, sync, extraOptions ){
|
---|
64 | this.id = url;
|
---|
65 |
|
---|
66 | if(REST.isProcessRefreshToken && !!callback){
|
---|
67 |
|
---|
68 | REST.taksResquests[REST.taksResquests.length] = {
|
---|
69 | url: url,
|
---|
70 | type: type,
|
---|
71 | data: data,
|
---|
72 | callback: callback,
|
---|
73 | sync: sync,
|
---|
74 | extraOptions: extraOptions
|
---|
75 | };
|
---|
76 |
|
---|
77 | }else if(REST.isProcessRefreshToken)
|
---|
78 | setTimeout(REST.send(url, type, data, callback, sync, extraOptions), 100);
|
---|
79 |
|
---|
80 | REST.resquests[url] = {
|
---|
81 | url: url,
|
---|
82 | type: type,
|
---|
83 | data: data,
|
---|
84 | callback: callback,
|
---|
85 | sync: sync,
|
---|
86 | extraOptions: extraOptions
|
---|
87 | };
|
---|
88 |
|
---|
89 | var result = false;
|
---|
90 | var envelope = {
|
---|
91 |
|
---|
92 | async: ( typeof sync !== "undefined" ? !sync : !!callback ),
|
---|
93 | url: url,
|
---|
94 | success: function( dt, textStatus, jqXHR ){
|
---|
95 |
|
---|
96 | if(REST.isInvalidToken(dt)){
|
---|
97 | REST.load('', true);
|
---|
98 |
|
---|
99 | if(!REST.me)
|
---|
100 | return {error: 'Empty session', title: 'Error in refresh token', description: 'Error in refresh token.'};
|
---|
101 |
|
---|
102 | var ref = REST.resquests[this.url];
|
---|
103 | result = REST.send(ref.url, ref.type, ref.data, ref.callback, ref.sync, ref.extraOptions)
|
---|
104 | }
|
---|
105 | delete REST.resquests[this.url];
|
---|
106 |
|
---|
107 | if(result == false){
|
---|
108 |
|
---|
109 | if( callback )
|
---|
110 | {
|
---|
111 | result = callback( dt, textStatus, jqXHR );
|
---|
112 | }
|
---|
113 | else
|
---|
114 | result = dt;
|
---|
115 | }
|
---|
116 |
|
---|
117 | },
|
---|
118 | error: function( dt, textStatus, jqXHR ){
|
---|
119 |
|
---|
120 | var response = {error: 'error', status: dt.status, description: dt.responseText, statusText: dt.responseText};
|
---|
121 |
|
---|
122 | if( callback )
|
---|
123 | {
|
---|
124 | result = callback( response, textStatus, jqXHR );
|
---|
125 | }
|
---|
126 | else
|
---|
127 | result = response;
|
---|
128 |
|
---|
129 | },
|
---|
130 |
|
---|
131 | type: $.isArray( type ) ? type[0] : type,
|
---|
132 | data: data
|
---|
133 |
|
---|
134 | };
|
---|
135 |
|
---|
136 | if( $.isArray( type ) && type[1] )
|
---|
137 | envelope['dataType'] = type[1];
|
---|
138 |
|
---|
139 | if(REST.me)
|
---|
140 | envelope = $.extend( envelope, {
|
---|
141 | beforeSend: function (xhr){
|
---|
142 | xhr.setRequestHeader('Authorization', "OAUTH Bearer " + REST.me.token)
|
---|
143 | }
|
---|
144 | });
|
---|
145 |
|
---|
146 | if( extraOptions )
|
---|
147 | envelope = $.extend( envelope, extraOptions );
|
---|
148 |
|
---|
149 | $.ajax( envelope );
|
---|
150 |
|
---|
151 | return( result );
|
---|
152 | },
|
---|
153 |
|
---|
154 | isInvalidToken: function(data){
|
---|
155 | return ((data) && (data.error && data.error == 'invalid_grant') && (data.error_description == 'The access token provided has expired.')) ? true : false
|
---|
156 | },
|
---|
157 |
|
---|
158 | dispatch: function( dispatcher, data, callback, isPost, dataType ){
|
---|
159 | return this.send( this.dispatchPath + dispatcher + ".php",
|
---|
160 | [ ( isPost ? 'post' : 'get' ), dataType || 'json' ],
|
---|
161 | data, callback );
|
---|
162 | },
|
---|
163 |
|
---|
164 | dispatchPath: '../prototype',
|
---|
165 |
|
---|
166 | load: function(url, isRefresh){
|
---|
167 | this.me = this.dispatch( (url || '') + "me", (isRefresh ? {
|
---|
168 | refreshToken: true
|
---|
169 | } : {}), false, true)
|
---|
170 | }
|
---|
171 |
|
---|
172 | } |
---|