You need to sign in to do that
Don't have an account?
Thiago Barbosa 10
Is there a Trailhead Leaderboard with points, badges and trails?Could to share code example?
This code example:
public class RESTGetHttpContentCallout { private static final String htmlBadgesClass = 'tds-tally__count tds-tally__count_success">'; private static final String htmlPointsClass = 'tds-tally__count tds-tally__count_success">'; private static final String htmlTrailsClass = 'tds-tally__count tds-tally__count_success">'; private static final String htmlBadgesJson = '<div data-react-class="BadgesPanel" data-react-props="'; private static final string JsonFinalChar = '">'; private static integer modulos = 0, superbadges = 0, eventos = 0, projetos = 0; public static Map<String, String> getCalloutResponseContents(String url){ HttpResponse res = new HttpResponse(); try{ Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint(url); req.setMethod('GET'); req.setHeader('Content-Type', 'application/json'); System.debug('req' + req); res = h.send(req); System.debug('THBS ---- res' + res); }catch(Exception e){ System.debug('An exception occurred: ' + e.getMessage()); } String lBody = res.getBody(); System.debug('THBS ---- res.getBody();' + res.getBody()); String lContactBadges = getValue( htmlBadgesClass, lBody ); String lContactPoints = getValue( htmlPointsClass, lBody ); String lContactTrails = getValue( htmlTrailsClass, lBody ); String lJsonBadges = getJsonValue( htmlBadgesJson, JsonFinalChar, lBody); system.debug('--->> ' + lContactBadges); system.debug('THBS ---- lContactPoints' + lContactPoints); System.debug('THBS ---- lContactTrails' + lContactTrails); Map<String,String> lMapToReturn = new Map<String,String>(); lMapToReturn.put('Badges', lContactBadges); lMapToReturn.put('Pontos', lContactPoints); lMapToReturn.put('Trails', lContactTrails); lMapToReturn.put('Modulos', string.valueOf(modulos)); lMapToReturn.put('Superbadges', string.valueOf(superbadges)); lMapToReturn.put('Projetos', string.valueOf(projetos)); lMapToReturn.put('Eventos', string.valueOf(eventos)); system.debug('THBS ---- lMapToReturn' + JSON.serialize(lMapToReturn)); return lMapToReturn; } private static String getJsonValue(string aClass, string finalChar, string aBody){ Integer lInit = getInitialLocationByAttributeClass(aClass, aBody); Integer lEnd = getFinalLocationByInitialLocationAndFinalChar(lInit, finalChar, aBody); return getAttributeValues(lInit, lEnd, aBody); } private static String getValue( String aClass, String aBody ) { Integer lInit = getInitialLocationByAttributeClass(aClass, aBody); Integer lEnd = getFinalLocationByInitialLocation(lInit, aBody); return getAttributeValues(lInit, lEnd, aBody); } private static Integer getInitialLocationByAttributeClass(String lClassName, String lBody){ System.debug('THBS ---- lClassName' + lClassName); System.debug('THBS ---- lClassName length' + lClassName.length()); System.debug('THBS ---- lBody.indexOf(lClassName)' + lBody.indexOf(lClassName)); return lBody.indexOf(lClassName) + lClassName.length(); } private static Integer getFinalLocationByInitialLocation(Integer lInitialLocation, String lBody){ return lBody.indexOf('</html>',lInitialLocation); } private static Integer getFinalLocationByInitialLocationAndFinalChar(Integer lInitialLocation, string finalChar ,String lBody){ return lBody.indexOf(finalChar,lInitialLocation); } private static String getAttributeValues(Integer lAttrInitialLocation, Integer lAttrFinalLocation, String lBody){ System.debug('THBS ---- lAttrFinalLocation' + lAttrFinalLocation); System.debug('THBS ---- lBody' + lBody); System.debug('THBS ---- lAttrInitialLocation' + lAttrInitialLocation); return lBody.substring(lAttrInitialLocation,lAttrFinalLocation).replace('\n',''); } }