Last active
June 5, 2018 13:14
-
-
Save wake/c93a10a6ba51eeacfce64b926ded2c61 to your computer and use it in GitHub Desktop.
Swapub Mobile API wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* API access | |
* | |
*/ | |
var api = (function () { | |
/** | |
* | |
* Config | |
* | |
*/ | |
var _api = { | |
app: null, | |
authing: false, | |
waiting: [], | |
config: { | |
host: '', | |
email: '', | |
password: '', | |
appid: '', | |
version: '', | |
type: '1', | |
cache: {}, | |
retry: false, | |
token: '' | |
} | |
}; | |
/** | |
* | |
* App 初始化 | |
* | |
*/ | |
_api.init = function (app, config) { | |
config = config || {}; | |
_api.app = app; | |
_api.config.host = config.host || ''; | |
_api.config.email = config.email || ''; | |
_api.config.password = config.password || ''; | |
_api.config.appid = config.appid || ''; | |
_api.config.version = config.version || ''; | |
_api.config.type = config.type || ''; | |
} | |
/** | |
* | |
* Auth (ask token) | |
* | |
*/ | |
_api.auth = function (after) { | |
after && _api.waiting.push (after); | |
// Pending if authing | |
if (_api.authing) | |
return; | |
_api.authing = true; | |
_api.request.post ('/PeformLogon', { | |
Version: _api.config.version, | |
Email: _api.config.email, | |
Password: _api.config.password, | |
Type: _api.config.type | |
}, function (res) { | |
_api.authing = false; | |
if (res.Token && res.Token != '') | |
_api.config.token = res.Token; | |
while (after = _api.waiting.shift ()) { | |
if (typeof after == 'function') | |
after (); | |
else if (typeof after.request == 'function') { | |
// 手動重新配置 arugments (因為 arguments 不是陣列,無法直接改變長度) | |
var pass = []; | |
for (i=0; i<after.arguments.length; i++) | |
pass[i] = after.arguments[i]; | |
pass.push (true); | |
after.request.apply (_api, pass); | |
} | |
} | |
}, function () { | |
_api.authing = false; | |
console.log ('[api:auth] API 驗證失敗 (/PeformLogon)', arguments); | |
}, true) // retry | |
}; | |
/** | |
* | |
* Fair View | |
* | |
*/ | |
_api.fair = { | |
// FairView > GetCategories | |
category: function (handler, error) { | |
_api.request.get ('/Category', handler, error); | |
}, | |
// FairView > GetBannerList | |
banner: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {CategoryID: options}; | |
else | |
options.CategoryID = options.id; | |
query = $.param (options); | |
_api.request.get ('/GetBannerList?' + query, handler, error); | |
}, | |
/** | |
* | |
* FairView > GetProductWithLocationAndCategory_V2 | |
* @doc: http://dev-8085.swapub.com/Help/Api/GET-api-GetProductWithLocationAndCategory_V2_filterDate_category_country_area_city_count | |
* | |
* @param: *filterDate | |
* @param: category | |
* @param: country | |
* @param: area | |
* @param: city | |
* @param: count | |
* | |
*/ | |
product: function (options, handler, error) { | |
options = options || {}; | |
options.filterDate = options.filterDate || moment ().toISOString (); | |
query = $.param (options); | |
_api.request.get ('/GetProductWithLocationAndCategory_V2?' + query, handler, error); | |
} | |
}; | |
/** | |
* | |
* Product View | |
* | |
*/ | |
_api.product = { | |
// FairView > GetCategories | |
get: function (id, handler, error) { | |
_api.request.get ('/Product/' + id, handler, error); | |
}, | |
// ProductView > GetProductTrackCount | |
trackCount: function (id, handler, error) { | |
_api.request.get ('/GetProductTrackCount?productID=' + id, handler, error); | |
}, | |
// ProductView > GetPublicMessageCount | |
publicMessageCount: function (id, handler, error) { | |
_api.request.get ('/PublicMessage?productID=' + id, handler, error); | |
}, | |
// ProductView > GetPublicMessageList | |
publicMessage: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {productID: options, maxtime: moment ().toISOString ()}; | |
else { | |
options.productID = options.id; | |
options.maxtime = options.maxtime || moment ().toISOString (); | |
delete options.id; | |
} | |
query = $.param (options); | |
_api.request.get ('/PublicMessage?' + query, handler, error); | |
}, | |
// ProductView > GetMessageCount | |
messageCount: function (id, handler, error) { | |
_api.request.get ('/Message1?productID=' + id, handler, error); | |
}, | |
// ProductView > GetMessageList | |
message: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {productID: options}; | |
else { | |
options.productID = options.id; | |
delete options.id; | |
} | |
query = $.param (options); | |
_api.request.get ('/Message?' + query, handler, error); | |
}, | |
// ProductView > GetProductDeal | |
dealingInfo: function (options, handler, error) { | |
query = $.param (options); | |
_api.request.get ('/ProductDeal?' + query, handler, error); | |
}, | |
// ProductView > GetProductSearchResult | |
searchResult: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {keyword: options}; | |
options.skip = options.skip || 1; | |
query = $.param (options); | |
_api.request.get ('/search_product_es?' + query, handler, error); | |
} | |
}; | |
/** | |
* | |
* Profile View | |
* | |
*/ | |
_api.person = { | |
// ProfileView > GetAccountInfo | |
profile: function (id, handler, error) { | |
_api.request.get ('/User/' + id, handler, error); | |
}, | |
// ProfileView > GetDealedNum | |
dealtNum: function (id, handler, error) { | |
_api.request.get ('/DealedNum?targetUser=' + id, handler, error); | |
}, | |
// ProfileView > GetRatingCount | |
ratingCount: function (id, handler, error) { | |
_api.request.get ('/Rating?accountID=' + id, handler, error); | |
}, | |
// ProfileView > GetBooth | |
product: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {ownerID: options}; | |
options.filterDate = options.filterDate || moment ().toISOString (); | |
query = $.param (options); | |
_api.request.get ('/Product?' + query, handler, error); | |
}, | |
// ProfileView > GetWishes | |
wish: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {ownerID: options}; | |
options.filterDate = options.filterDate || moment ().toISOString (); | |
query = $.param (options); | |
_api.request.get ('/Wish?' + query, handler, error); | |
} | |
}; | |
/** | |
* | |
* Search View | |
* | |
*/ | |
_api.search = { | |
/** | |
* | |
* SearchView > GetSearchResult_v2 | |
* @doc: http://dev-8085.swapub.com/Help/Api/GET-api-search_v2_keyword_skip_Country_Area_City_categoryID | |
* | |
* @param: *keyword | |
* @param: *skip | |
* @param: Country | |
* @param: Area | |
* @param: City | |
* @param: categoryID | |
* | |
*/ | |
product: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {keyword: options}; | |
options.skip = options.skip || 0; | |
query = $.param (options); | |
_api.request.get ('/search_v2?' + query, handler, error); | |
}, | |
/** | |
* | |
* SearchView > GetSearchUserResult | |
* @doc: http://dev-8085.swapub.com/Help/Api/GET-api-SearchUser_keyword_SkipRecord | |
* | |
* @param: *keyword | |
* @param: *SkipRecord | |
* | |
*/ | |
person: function (options, handler, error) { | |
if (typeof options == 'string' || typeof options == 'number') | |
options = {keyword: options}; | |
options.SkipRecord = options.SkipRecord || 0; | |
query = $.param (options); | |
_api.request.get ('/SearchUser?' + query, handler, error); | |
}, | |
// SearchView > GetPopularSearchKeyword | |
popularKeywords: function (options, handler, error) { | |
query = $.param (options); | |
_api.request.get ('/PopularSearchKeyword?' + query, handler, error); | |
} | |
}; | |
/** | |
* | |
* Request | |
* | |
*/ | |
_api.request = { | |
// GET operator | |
get: function (url, handler, error, retry) { | |
// Cache | |
var cache = {request: _api.request.get, arguments: arguments} | |
, headers = {appid: _api.config.appid} | |
, reqArguments = [] | |
, reqData = null | |
, reqToken = '' | |
, reqLink = '*' | |
; | |
async.whilst ( | |
// Stop condition | |
function () { return reqLink != null; }, | |
// Request | |
function (cb) { | |
var fixedUrl = _api.helper.url (url, reqToken); | |
$.ajax ({ | |
url: fixedUrl, | |
method: 'GET', | |
headers: headers, | |
dataType: 'json', | |
contentType: 'application/json' | |
}).done (function (data, textStatus, xhr) { | |
log.debug ('[api:GET] (done)', url); | |
reqToken = ''; | |
reqArguments = arguments; | |
// 有取到 link header,試著拆解出 pageToken | |
if ((reqLink = xhr.getResponseHeader ('link')) != null) { | |
reqToken = reqLink.match (/pageToken=([._\-\w]+)/); | |
reqToken = reqToken[1] && reqToken[1] != '' ? reqToken[1] : ''; | |
} | |
if (reqData === null) | |
reqData = data; | |
else if (typeof reqData == 'object' && typeof data == 'object') | |
reqData = reqData.concat (data); | |
cb (null, reqLink); | |
}).fail (function (res) { | |
log.debug ('[api:GET] (fail)', url); | |
reqArguments = arguments; | |
cb (res); | |
}); | |
}, | |
function (err) { | |
if (err == null || ! err) { | |
reqArguments[0] = reqData; | |
handler.apply (_api, reqArguments); | |
} | |
else { | |
// Maybe need authing, make authing and retry | |
if (err.status == 401 && ! retry) | |
_api.auth (cache); | |
else if (typeof error == 'function') | |
error.apply (_api, reqArguments); | |
else | |
handler.apply (_api, reqArguments); | |
} | |
} | |
); | |
}, | |
// POST operator | |
post: function (url, data, handler, error, retry) { | |
// Cache | |
var cache = {request: _api.request.post, arguments: arguments} | |
, headers = {appid: _api.config.appid} | |
, fixedUrl = _api.helper.url (url) | |
, formMode = false | |
, fdata | |
; | |
if (data._form) { | |
fdata = new FormData (); | |
formMode = true; | |
for (var i in data) { | |
if (i[0] != '_') | |
fdata.append (i, data[i]); | |
} | |
} | |
$.ajax ({ | |
url: fixedUrl, | |
method: 'POST', | |
headers: headers, | |
data: ! formMode ? JSON.stringify (data) : fdata, | |
dataType: formMode ? 'html' : 'json', | |
contentType: formMode ? false : 'application/json', | |
processData: formMode ? false : true | |
}).done (function () { | |
log.debug ('[api:POST] (done)', url); | |
handler.apply (_api, arguments); | |
}).fail (function (res) { | |
log.debug ('[api:POST] (fail)', url); | |
// Maybe need auth, auth and retry | |
if (res.status == 401 && ! retry) | |
_api.auth (cache); | |
else if (typeof error == 'function') | |
error.apply (_api, arguments); | |
else | |
handler.apply (_api, arguments); | |
}); | |
}, | |
// PUT operator | |
put: function (url, data, handler, error, retry) { | |
// Cache | |
var cache = {request: _api.request.put, arguments: arguments} | |
, headers = {appid: _api.config.appid} | |
, fixedUrl = _api.helper.url (url) | |
, formMode = false | |
, fdata | |
; | |
if (data._form) { | |
fdata = new FormData (); | |
formMode = true; | |
for (var i in data) { | |
if (i[0] != '_') | |
fdata.append (i, data[i]); | |
} | |
} | |
$.ajax ({ | |
url: fixedUrl, | |
method: 'PUT', | |
headers: headers, | |
data: ! formMode ? JSON.stringify (data) : fdata, | |
dataType: formMode ? 'html' : 'json', | |
contentType: formMode ? false : 'application/json', | |
processData: formMode ? false : true | |
}).done (function () { | |
log.debug ('[api:PUT] (done)', url); | |
handler.apply (_api, arguments); | |
}).fail (function (res) { | |
log.debug ('[api:PUT] (fail)', url); | |
// Maybe need auth, auth and retry | |
if (res.status == 401 && ! retry) | |
_api.auth (cache); | |
else if (typeof error == 'function') | |
error.apply (_api, arguments); | |
else | |
handler.apply (_api, arguments); | |
}); | |
}, | |
// DELETE operator | |
delete: function (url, handler, error, retry) { | |
// Cache | |
var cache = {request: _api.request.put, arguments: arguments} | |
, headers = {appid: _api.config.appid} | |
, fixedUrl = _api.helper.url (url) | |
, formMode = false | |
, fdata | |
; | |
$.ajax ({ | |
url: fixedUrl, | |
method: 'PUT', | |
headers: headers, | |
dataType: 'json', | |
contentType: 'application/json' | |
}).done (function () { | |
log.debug ('[api:DELETE] (done)', url); | |
handler.apply (_api, arguments); | |
}).fail (function (res) { | |
log.debug ('[api:DELETE] (fail)', url); | |
// Maybe need auth, auth and retry | |
if (res.status == 401 && ! retry) | |
_api.auth (cache); | |
else if (typeof error == 'function') | |
error.apply (_api, arguments); | |
else | |
handler.apply (_api, arguments); | |
}); | |
} | |
} | |
/** | |
* | |
* Helper | |
* | |
*/ | |
_api.helper = { | |
// URL fixer | |
url: function (url, pageToken) { | |
if (_api.config.token != '') | |
url += (url.indexOf ('?') !== -1 ? '&' : '?') + 'accessToken=' + _api.config.token; | |
return _api.config.host + url; | |
} | |
} | |
/** | |
* | |
* Instance | |
* | |
*/ | |
return _api; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment