0% found this document useful (0 votes)
24 views

Comex Net Api

The document defines a ComexNetAPI class that handles authentication and authorization with external APIs. It contains methods to login, verify tokens, and retrieve account information by making REST API calls and handling the responses.

Uploaded by

Scotty Setroc
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Comex Net Api

The document defines a ComexNetAPI class that handles authentication and authorization with external APIs. It contains methods to login, verify tokens, and retrieve account information by making REST API calls and handling the responses.

Uploaded by

Scotty Setroc
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

public class ComexNetAPI {

public Integer idUsuario {get; set;}


public String nombreConcesionario {get; set;}
public String correoConcesrio {get; set;}
public List<JSON2Apex.Cuentas> numeroCuentas {get; set;}

//Credenciales
public String userHead = 'Comex';
public String passwordHead = 'Comex2017';
public String AXXESSAPPSECRET = 'KWES';
public String AXXESSAPPID = 'SLF';
public String method = 'GET';
public String endPoint = 'https://servicios.uataxxess.com.mx/';

public String loginSuccess(){


String outcomeMsg;

String resource = 'Comex.TI.WebApi.SSO/api/Authentic';


String method = 'GET';

if(Limits.getCallouts() >= Limits.getLimitCallouts()){


outcomeMsg = 'Se ha superdado el numero de llamadas';
}
else if(endPoint == null || userHead == null || passwordHead == null){
outcomeMsg = 'Verifique sus credenciales';
}

else {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new Http();

req.setMethod(method);
req.setTimeout(120000);
req.setHeader('Accept', '*/*');
req.setHeader( 'Content-Type', 'application/json' );
Blob headerValue = Blob.valueOf(userHead + ':' + passwordHead);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', + authorizationHeader);

String encoded = EncodingUtil.urlEncode(passwordHead, 'UTF-8');


String reqBody = '?appid=' +AXXESSAPPID+
'&redirectUri=COMEX&User=C005233&Pass=Agosto2019&dominio=Comex&scope=scope';

req.setEndpoint(endpoint+resource+reqBody);
try {
res = h.send(req);

if((res.getStatusCode() == 200 || res.getStatusCode() == 201) &&


res.getBody() != null && res.getBody() != null){
JSON2Apex.RequestWrapperToken rw =
(JSON2Apex.RequestWrapperToken)JSON.deserialize(res.getBody(),JSON2Apex.RequestWrap
perToken.class);
verifyToken(rw.Token);
}
else {
outcomeMsg = 'Error en la respuesta';
}
}catch(exception e){
outcomeMsg = 'Error: error en la integración de API REST: ' +
e.getMessage();
}
}
return outcomeMsg;
}

//Validación de Token Valido


public void verifyToken(String token){

//Mensaje de salida en caso de existir error.


String outcomeMsg;
//End point para validación de Token
String resource = 'Comex.TI.WebApi.SSO/api/Authorize';

if(Limits.getCallouts() >= Limits.getLimitCallouts()){


outcomeMsg = 'Se ha superdado el numero de llamadas';
}
else if(token == null){
outcomeMsg = 'Token vacío';
}
else{
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new Http();

req.setMethod(method);
req.setTimeout(120000);
req.setHeader('Accept', '*/*');
req.setHeader( 'Content-Type', 'application/json' );
Blob headerValue = Blob.valueOf(userHead + ':' + passwordHead);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', + authorizationHeader);

String reqBody = '?appid=' +AXXESSAPPID+


'&redirectUrl=&scope=scope&code=' +token+ '&appsecrect=' + AXXESSAPPSECRET;

req.setEndpoint(endpoint+resource+reqBody);

try {
res = h.send(req);

if((res.getStatusCode() == 200 || res.getStatusCode() == 201) &&


res.getBody() != null && res.getBody() != null){
JSON2Apex.RequesWrapperInfoToken rw =
(JSON2Apex.RequesWrapperInfoToken)JSON.deserialize(res.getBody(),JSON2Apex.RequesWr
apperInfoToken.class);
infoAccount(userHead, passwordHead, rw.User);
}
else {
outcomeMsg = 'Error en la respuesta';

}
}catch(exception e){
outcomeMsg = 'Error: error en la integración de API REST: ' +
e.getMessage();
}
}
}

//Obtiene la información del concesionario


public void infoAccount(String user, String password, String userAPI){
System.debug(LoggingLevel.INFO,'USER API: '+userAPI);
//Mensaje de salida en caso de existir error.
String outcomeMsg;
//End point para obtener información del concesionario
String resource =
'Comex.TI.Web.Service.AttributesUser.WebApi/api/Attributes';

if(Limits.getCallouts() >= Limits.getLimitCallouts()){


outcomeMsg = 'Se ha superdado el numero de llamadas';
}
else if(userAPI == null){
outcomeMsg = 'Token vacío';
}
else {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new Http();

req.setMethod(method);
req.setTimeout(120000);
req.setHeader('Accept', '*/*');
req.setHeader( 'Content-Type', 'application/json' );
Blob headerValue = Blob.valueOf(user + ':' + password);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', + authorizationHeader);

String reqBody = '?IdUser='+ userAPI +'&KeyAplication='+


AXXESSAPPSECRET
+'&attributosUsr=TipoUsuarioVocacionPlaza,Mail,Cuentas,NombreCompleto,Zonas';

req.setEndpoint(endpoint+resource+reqBody);
System.debug(LoggingLevel.INFO,'Link: '+endpoint+resource+reqBody);
try {
res = h.send(req);

if((res.getStatusCode() == 200 || res.getStatusCode() == 201) &&


res.getBody() != null && res.getBody() != null){
JSON2Apex rw =
(JSON2Apex)JSON.deserialize(res.getBody(),JSON2Apex.class);
idUsuario = rw.AttributesUser.IdUsuario;
nombreConcesionario = rw.AttributesUser.NombreCompleto;
correoConcesrio = rw.AttributesUser.Mail;
numeroCuentas = rw.AttributesUser.Cuentas;
}
else {
outcomeMsg = 'Error en la respuesta';

}
}catch(exception e){
outcomeMsg = 'Error: error en la integración de API REST: ' +
e.getMessage();
}
}
}
}

You might also like