alien4u
11/07/2018, 11:19 PMstalbert
11/07/2018, 11:30 PMstalbert
11/07/2018, 11:30 PM/**
Utility library for making TBA (token based authentication) requests from
server-side scripts to other server side scripts (e.g. Suitelet calling RESTlet via TBA)
This file depends upon library files oauth-1.0a.js and crypto-js.js
**/
declare var OAuth:any
declare var CryptoJS:any
namespace EC {
const log = LogManager.getLogger('TBA')
/**
* Invokes a NetSuite endpoint using TBA, returning the nlobjResponse object
* @param url Netsuite endpoint - e.g. RESTlet or Suitelet endpoint to call
* @param token application token id (key) and token secret as assigned by NS
* @param consumer consumer id (key) and consumer secret as assigned by NS
* @param [postdata] optional js object payload, is JSON.stringified before sending to url.
* If a value is provided, the request is HTTP POSTed, otherwise HTTP GET is used.
*
* @return {nlobjResponse} the response as returned from nlapiRequestURL
* Note: Content-Type is hardcoded to 'application/json'
*/
export function invokeTba(url, token: { key, secret }, consumer: { key, secret }, postdata?: object | null) {
const oauth = OAuth({
consumer: consumer,
signature_method: 'HMAC-SHA1',
hash_function: (b, k) => CryptoJS.HmacSHA1(b, k).toString(CryptoJS.enc.Base64),
realm: nlapiGetContext().getCompany()
})
const authHeader = oauth.toHeader(oauth.authorize({
method: postdata ? 'POST' : "GET",
url: url,
data: {}
}, token)).Authorization
log.debug('OAuth Header', authHeader)
log.debug('calling endpoint', url)
const resp = nlapiRequestURL(url, postdata ? JSON.stringify(postdata) : null, {
Authorization: authHeader,
"Content-Type": "application/json"
})
return resp
}
}
stalbert
11/07/2018, 11:31 PMJohnnyC
11/08/2018, 1:16 AMstalbert
11/08/2018, 4:28 AMalien4u
11/08/2018, 2:18 PMalien4u
11/08/2018, 2:18 PMHubert
11/08/2018, 2:45 PMalien4u
11/08/2018, 2:46 PMHubert
11/08/2018, 2:48 PMvar headers = oauth.getHeaders({
url: config.SEND_DATA_FOR_LABELS_RESTLET,
method: 'POST',
tokenKey: config.TBA_TOKENS['kelly']['tokenid'],
tokenSecret: config.TBA_TOKENS['kelly']['tokensecret']
});
//passing RAW Groupon csv file id
var body = { grouponcsvrawfileid : csvGrouponFileId };
headers['Content-Type'] = 'application/json';
var restResponse = <http://https.post|https.post>({
url: config.SEND_DATA_FOR_LABELS_RESTLET,
headers: headers,
body: JSON.stringify(body)
});
log.debug('response', JSON.stringify(restResponse));
Hubert
11/08/2018, 2:49 PMdefine(['N/https', 'N/ui/serverWidget', '../lib/oauth', '../lib/cryptojs', '../config/GrouponProcessingConfigurationModule'],
Hubert
11/08/2018, 2:50 PMfunction(https, serverWidget, oauth, cryptojs, config) {
Hubert
11/08/2018, 2:51 PMalien4u
11/08/2018, 2:52 PMHubert
11/08/2018, 2:52 PMHubert
11/08/2018, 2:53 PMHubert
11/08/2018, 2:55 PMstalbert
11/08/2018, 2:57 PMHubert
11/08/2018, 2:58 PMHubert
11/08/2018, 3:02 PM/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/https', 'N/ui/serverWidget', '../lib/oauth', '../lib/cryptojs', '../config/GrouponProcessingConfigurationModule'],
/**
* @param {https} https
*/
function(https, serverWidget, oauth, cryptojs, config) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} context
* @param {ServerRequest} context.request - Encapsulation of the incoming request
* @param {ServerResponse} context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
try {
if(context.request.method === 'GET' ){
log.debug(context.request.method + " - START");
var form = serverWidget.createForm({ title: 'Send data for Labels and Pick Ticket(s)' });
var field = form.addField({ id: 'custpage_csvfilefromgroupon', type: serverWidget.FieldType.TEXT, label: 'CSV file id from Groupon US' });
field.layoutType = serverWidget.FieldLayoutType.NORMAL;
form.addSubmitButton({ label: 'Send' });
context.response.writePage(form);
log.debug(context.request.method+" - END");
} else if( context.request.method === 'POST' ){
log.debug(context.request.method+" - START");
var csvGrouponFileId = context.request['parameters']['custpage_csvfilefromgroupon'];
log.debug("request: ","CSV Groupon File id: "+csvGrouponFileId);
//Hubert's
// var headers = oauth.getHeaders({
// url: config.SEND_DATA_FOR_LABELS_RESTLET,
// method: 'POST',
// tokenKey: config.TBA_TOKENS['hubert']['tokenid'],
// tokenSecret: config.TBA_TOKENS['hubert']['tokensecret']
// });
//Kelly's
var headers = oauth.getHeaders({
url: config.SEND_DATA_FOR_LABELS_RESTLET,
method: 'POST',
tokenKey: config.TBA_TOKENS['kelly']['tokenid'],
tokenSecret: config.TBA_TOKENS['kelly']['tokensecret']
});
//passing RAW Groupon csv file id
var body = { grouponcsvrawfileid : csvGrouponFileId };
headers['Content-Type'] = 'application/json';
var restResponse = <http://https.post|https.post>({
url: config.SEND_DATA_FOR_LABELS_RESTLET,
headers: headers,
body: JSON.stringify(body)
});
log.debug('response', JSON.stringify(restResponse));
log.debug(context.request.method+" - END");
}; //POST
} catch (e) {
log.error('error',e.message);
};
}
return {
onRequest: onRequest
};
});
Hubert
11/08/2018, 3:02 PM