Skip to content

Commit

Permalink
Merge pull request #22 from jvoliveiraGN/master
Browse files Browse the repository at this point in the history
Correção do tratamento de responses
  • Loading branch information
jvoliveiraGN authored May 19, 2021
2 parents cecebfb + 5271f7e commit 94ea444
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 72 deletions.
4 changes: 2 additions & 2 deletions lib/gn-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ GnAuth.prototype.getAccessToken = function () {
return response.data;
})
.catch((error) => {
console.error(error);
return error;
});

return response;
};

module.exports = GnAuth;
module.exports = GnAuth;
109 changes: 39 additions & 70 deletions lib/gn-endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,78 @@ var https = require('https');
var q = require('q');
var GnAuth = require('./gn-auth');
var sdkPackage = require('../package.json');
const axios = require('axios');
const axios = require('axios');

function GnEndpoints(options, constants) {
// options.baseUrl = options.sandbox ? constants.URL.DEFAULT.sandbox : constants.URL.DEFAULT.production;
this.options = options;
this.accessToken = null;
this.constants = constants;
}

GnEndpoints.prototype.run = function (name, params, body) {
var self = this;
self.defer = q.defer();

if (self.constants.ENDPOINTS.PIX.hasOwnProperty(name)) {
try {
this.certificate = fs.readFileSync(this.options.pathCert);
this.agent = new https.Agent({
pfx: this.certificate,
passphrase: "",
});
} catch (error) {
throw 'FALHA AO LER O CERTIFICADO'
}

self.endpoint = self.constants.ENDPOINTS.PIX[name];
this.defer = q.defer();
if (this.constants.ENDPOINTS.PIX.hasOwnProperty(name)) {
try {
this.certificate = fs.readFileSync(this.options.pathCert);
this.agent = new https.Agent({
pfx: this.certificate,
passphrase: "",
});
} catch (error) {
throw 'FALHA AO LER O CERTIFICADO'
}

this.endpoint = this.constants.ENDPOINTS.PIX[name];
this.options.baseUrl = this.options.sandbox ? this.constants.URL.PIX.sandbox : this.constants.URL.PIX.production;
}else{
self.endpoint = self.constants.ENDPOINTS.DEFAULT[name];
} else {

this.endpoint = this.constants.ENDPOINTS.DEFAULT[name];
this.options.baseUrl = this.options.sandbox ? this.constants.URL.DEFAULT.sandbox : this.constants.URL.DEFAULT.production;
}
self.body = body;
self.params = params;
this.body = body;
this.params = params;

if (!self.accessToken) {
self.getAccessToken()
.then(self.directReq.bind(self));
} else {
self.withTokenReq.call(self);
}
this.getAccessToken().then(this.directReq.bind(this));

return self.defer.promise;
return this.defer.promise;
};

GnEndpoints.prototype.getAccessToken = function () {
var self = this;
let self = this;
var gnAuth = new GnAuth(this.options, this.constants);
return gnAuth.getAccessToken()
.then(function (response) {
self.accessToken = response.access_token;
return self.accessToken;
return response.access_token;
}).catch(function (err) {
return err;
});
};

GnEndpoints.prototype.getResponse = function (response, body) {
return this.options
.raw_response ? response : body;
};

GnEndpoints.prototype.req = async function (callback) {
let req = this.getParams.call(this, this.endpoint.route);
req['method'] = this.endpoint.method;
const response = axios(req)
.then((res) => {
callback(res);
console.log(res.data)
})
.catch((error) => {
callback(error);
console.log(error.response.data);
});
};

GnEndpoints.prototype.directReq = function () {
this.req(this.directReqCallback.bind(this));
};

GnEndpoints.prototype.withTokenReq = function () {
this.req(this.withTokenReqCallback.bind(this));
};

GnEndpoints.prototype.getParams = function (route) {
var self = this;
var regex = /\:(\w+)/g;
var query = '';
var placeholders = route.match(regex) || [];
var params = {};

for (var prop in self.params) {
params[prop] = self.params[prop];
for (var prop in this.params) {
params[prop] = this.params[prop];
}

var getVariables = function () {
Expand Down Expand Up @@ -156,32 +136,21 @@ GnEndpoints.prototype.getParams = function (route) {
return req;
};

GnEndpoints.prototype.withTokenReqCallback = function (err, httpResponse, httpResponseBody) {
var self = this;
var response = self.getResponse(httpResponse, httpResponseBody);

if (err) {
self.defer.reject(err);
} else if (httpResponse.statusCode === 401) {
self.getAccessToken().then(self.directReq.bind(self));
} else if (httpResponse.statusCode !== 200) {
self.defer.reject(response);
} else {
self.defer.resolve(response);
}
};
GnEndpoints.prototype.directReqCallback = function (raw_response) {

GnEndpoints.prototype.directReqCallback = function (err, httpResponse, bodyResponse) {
var response = this.getResponse(httpResponse, bodyResponse);

if (err) {
this.defer.reject(err);
} else if (httpResponse.statusCode !== 200) {
this.defer.reject(response);
} else {
this.defer.resolve(response);
if(raw_response.data){
if(raw_response.status < 300){
if (raw_response.data.data){
this.defer.resolve(raw_response.data.data);
} else {
this.defer.resolve(raw_response.data);
}
} else {
this.defer.reject(raw_response.data);
}
} else if (raw_response.response && raw_response.response.data) {
this.defer.reject(raw_response.response.data);
}
};


module.exports = GnEndpoints;
module.exports = GnEndpoints;

0 comments on commit 94ea444

Please sign in to comment.