-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreshvine.js
199 lines (177 loc) · 6.65 KB
/
freshvine.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*!
* Fresh Vine API javascript library
* A simple javascript library that makes it easy to use the Fresh Vine API.
*
* @author Paul Prins
* @link http://freshvine.co/
* @version 1.0
* @requires jQuery v1.7 or later
*
* Find instructions and source on GitHub: https://github.com/FreshVine/Fresh-Vine-API-js
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
if( typeof FV_API_CLIENT_ID == 'undefined' || typeof FV_API_CLIENT_SECRET == 'undefined' ){ throw new Error('You must define your FV_API_CLIENT_ID to use fv-api.js'); }
if( typeof FV_API_VERSION == 'undefined'){ var FV_API_VERSION = '1'; }
var FV_connection_api_status = true; // Boolean if the API is accessible
if( typeof FV_API_AUTH_APPROACH === 'undefined' ){
var FV_API_AUTH_APPROACH = 'key'; // Valid values are 'key', or 'token'
}
//
// This is the Fresh Vine API for javascript based applications
var FV_MakeRequest = function( Endpoint, Method, RequestData, SuccessFunction, ErrorFunction, excludeAuthData ){
// Ensure it is a full URI
if( Endpoint.toLowerCase().indexOf("https://api.freshvine.co/") === -1 ){
if( Endpoint.indexOf('auth/') == -1 ){
Endpoint = FV_API_VERSION + '/' + Endpoint;
}
Endpoint = 'https://api.freshvine.co/' + Endpoint; // Prepend the Fresh Vine API domain to the endpoint
}
// If Endpoint starts with 'auth/' we don't append the api version
var activeAuth = true;
if( FV_API_AUTH_APPROACH === 'key' && ( FV_getItem('FV_api_key') === null || FV_getItem('FV_api_key').length == 0 ) ){
activeAuth = false;
}
if( FV_API_AUTH_APPROACH === 'token' && ( FV_getItem('FV_access_token') === null || FV_getItem('FV_access_token').length == 0 ) ){
activeAuth = false;
}
if( !activeAuth && Endpoint.toLowerCase().indexOf('https://api.freshvine.co/auth/') === -1 ){
if( typeof FV_code_response != 'object' ){
if( typeof FV_DeviceCodeRegister === 'function' ){
FV_DeviceCodeRegister(); // We haven't started polling yet
}
}
ErrorFunction( 401, 'Unable to Process Request, Not yet Authenticated' );
return false; // Requests cannot be made until the API is connected
}
if( Method.toUpperCase() !== 'GET' && Method.toUpperCase() !== 'POST' )
return false;
Method = Method.toUpperCase();
//
// Add in the auth header data to the RequestData object
if( typeof RequestData === 'undefined')
RequestData = {};
if( typeof excludeAuthData === 'undefined' || excludeAuthData == false ){
RequestData['FVappid'] = FV_API_CLIENT_ID;
// RequestData['FVsecret'] = FV_API_CLIENT_SECRET;
// Set the Auth Approach
if( FV_API_AUTH_APPROACH === 'key' ){
RequestData['FVkey'] = FV_getItem('FV_api_key');
}else if( FV_API_AUTH_APPROACH === 'token' ){
RequestData['oauth_token'] = FV_getItem('FV_access_token');
}
}
$.ajax({type: Method,
url: Endpoint,
dataType: 'json',
data: RequestData
})
.done(function( data, textStatus, jqXHR ) {
// Check if the key or token expired
FV_ConnectionOnline(); // Mark it online
// console.log('Request Done', data, textStatus, jqXHR );
if( typeof SuccessFunction == 'function' ){
SuccessFunction( data, textStatus, jqXHR );
}
})
.fail(function(data, textStatus, errorThrown ){
// console.log( 'ajax failed', data.status );
if( data.status == 0 ){
FV_ConnectionOffline(); // Unable to reach the web
}else{
FV_ConnectionOnline(); // Mark it online
}
if( data.status >= 400 ){
FV_ErrorHandler( data.status ); // Manage big errors
} // Only catching errors here
// Check if the key or token expired
if( typeof ErrorFunction == 'function' ){
ErrorFunction( data.status, textStatus, errorThrown );
}
return;
});
},
FV_ErrorHandler = function( statusCode ){
if( statusCode == '401'){ // 401: Unauthorized - trash the credentials
if( FV_API_AUTH_APPROACH === 'token' ){
FV_removeItem('FV_access_token');
FV_removeItem('FV_refresh_token');
}else if( FV_API_AUTH_APPROACH === 'key' ){
FV_removeItem('FV_api_key');
}
if( typeof FV_DeviceCodeRegister === 'function' ){
FV_DeviceCodeRegister(); // Restart the Device Code approach
}
}
return true;
},
FV_ConnectionStatus = function(){
return FV_connection_api_status; // Simply returns the current status. You may choose to change up the variable name used.
},
FV_Disconnect = function( callbackFunction ){ // Need to drop key/s, and tell Fresh Vine to destroy them - will keep them from showing up in the My Apps section
if( FV_API_AUTH_APPROACH === 'token' ){ var endpoint = 'auth/oauth/destroy';
}else if( FV_API_AUTH_APPROACH === 'key' ){ var endpoint = 'auth/key/destroy'; }
FV_MakeRequest( endpoint, 'GET', {}, function(){
if( FV_API_AUTH_APPROACH === 'token' ){
FV_removeItem('FV_access_token');
FV_removeItem('FV_refresh_token');
}else if( FV_API_AUTH_APPROACH === 'key' ){
FV_removeItem('FV_api_key');
}
if( typeof callbackFunction == 'function'){ callbackFunction(); }
});
};
//
// If you're using a different data storage approach than localstorage you can write your own functions and load them prior to this script.
if( typeof FV_getItem !== 'function'){
/*
* Retreive information from a storage system
*/
var FV_getItem = function( k ){
return window.localStorage.getItem( k );
};
}
if( typeof FV_removeItem !== 'function'){
/*
* Remove information from a storage system
*/
var FV_removeItem = function( k ){
return window.localStorage.removeItem( k );
};
}
if( typeof FV_setItem !== 'function'){
/*
* Save information into a storage system
*/
var FV_setItem = function( k, v ){
return window.localStorage.setItem( k, v );
};
}
if( typeof FV_ConnectionOffline != 'function' ){
/*
* Function called when javascript is unable to reach the server
* Helpful for letting the user know that your application cannot reach the web/fresh vine
*/
FV_ConnectionOffline = function(){
if( FV_ConnectionStatus == false ){ return; } // Already disconnected
FV_connection_api_status = false;
// This is where you'd suspend things that require a web connection
console.log('Your unable to reach the Fresh Vine API')
return;
}
}
if( typeof FV_ConnectionOnline != 'function' ){
/*
* Function called when javascript is able to reach the server
* Helpful for letting the user know that your application can reach the web/fresh vine
*/
FV_ConnectionOnline = function(){
if( FV_ConnectionStatus == true ){ return; } // Already connected
FV_connection_api_status = true;
// This is where you'd place your custom scripts for when the web comes back
return;
};
}