Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OAUTH_Client {
- Public String login_url {set; get;} //login.salesforce.com || test.salesforce.com
- Public Boolean type_z {set; get;}
- Public String oauth_url = '/services/oauth2/token';
- Public List<Client_Org_Information__c> clientOrgSF {set; get;}
- public OAUTH_Client() {}
- @future(callout=true)
- public static void getSFOAuth(List<Id> orgIds){
- OAUTH_Client newOC = new OAUTH_Client();
- newOC.execute_SFOAuth(orgIds);
- }
- public String execute_SFOAuth(List<Id> orgIds){
- List<Client_Org_Information__c> clientSO = getOrgInfo(orgIds);
- String end_point;
- String client_id;
- String client_secret;
- String username;
- String password;
- List<Client_Org_Information__c> toUpdateClientS = new
- List<Client_Org_Information__c>();
- if(clientSO.size() > 0){
- for(Client_Org_Information__c org: clientSO){
- end_point = (org.type__c == 'Sandbox') ? 'https://test.saleforce.com' : 'https://login.salesforce.com';
- end_point += oauth_url;
- client_id = org.client_id__c;
- client_secret = org.client_secret__c;
- username = org.username__c;
- password = org.password__c;
- String JSON_BODY = Send_API(end_point, client_id, client_secret, username, password);
- Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(JSON_BODY);
- Client_Org_Information__c sfClient = new Client_Org_Information__c();
- sfClient.Id = org.Id;
- sfClient.instance_url__c = string.valueOf(m.get('instance_url'));
- sfClient.access_token__c = string.valueOf(m.get('access_token'));
- toUpdateClientS.add(sfClient);
- }
- }
- try {
- if(toUpdateClientS.size() > 0) update toUpdateClientS;
- }catch(Exception e){
- System.debug('ERROR: '+e.getMessage() + ' ' +e.getStackTraceString());
- }
- return null;
- }
- public static String Send_API(String END_POINT, String CLIENT_ID, String CLIENT_SECRET, String USERNAME, String PASSWORD){
- HttpRequest req = new HttpRequest();
- req.setEndpoint(END_POINT);
- req.setMethod('POST');
- req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
- String payLoad = 'grant_type=password'
- + '&username=' + USERNAME
- + '&password=' + PASSWORD
- + '&client_id=' + CLIENT_ID
- + '&client_secret=' + CLIENT_SECRET;
- //+ '&scope=PRODUCTION';
- req.setBody(payLoad);
- System.debug('ENDPOINT: '+END_POINT);
- System.debug('payLoad: '+payLoad);
- System.debug('Request: '+req);
- Http http = new Http();
- HttpResponse res = http.send(req);
- System.debug('REsponse: ' + res.getBody());
- return res.getBody();
- }
- public List<Client_Org_Information__c> getOrgInfo(List<Id> orgIds){
- clientOrgSF = [SELECT Id, client_id__c, type__c, client_secret__c, username__c, password__c FROM Client_Org_Information__c WHERE Id IN: orgIds];
- return clientOrgSF;
- }
- }
- public class SF_SystemOverview {
- Public String endpoint_url = '/services/apexrest/api/PackageLicense';
- Public List<Client_Org_Information__c> clientOrgSF {set; get;}
- Public List<Client_Org_System_Overview__c> clientOrgSO {set; get;}
- public SF_SystemOverview() {}
- @future(callout=true)
- public static void getSFOverview(List<Id> orgIds){
- SF_SystemOverview newOC = new SF_SystemOverview();
- newOC.execute_SFOverview(orgIds);
- }
- public String execute_SFOverview(List<Id> orgIds){
- List<Client_Org_Information__c> clientSO = getOrgInfo(orgIds);
- String end_point;
- String access_token;
- List<Client_Org_System_Overview__c> toUpdateClientSO = new
- List<Client_Org_System_Overview__c>();
- if(clientSO.size() > 0){
- for(Client_Org_Information__c org: clientSO){
- end_point = org.Instance_URL__c;
- end_point += endpoint_url;
- access_token = org.Access_Token__c;
- String JSON_BODY = Send_API(end_point, access_token);
- Client_Org_System_Overview__c sfClientSO = new
- Client_Org_System_Overview__c();
- Map<String, Object> m = (Map<String,
- Object>)JSON.deserializeUntyped(JSON_BODY);
- Map<String, Object> Storages = (Map<String,Object>)m.get('Storage');
- Map<String, Object> APIs = (Map<String,Object>)m.get('Api_Usage');
- sfClientSO.client_id__c = org.Id;
- sfClientSO.UsedDataStorage__c = string.valueOf(Storages.get('UsedDataStorage'));
- sfClientSO.TotalDataStorage__c = string.valueOf(Storages.get('TotalDataStorage'));
- sfClientSO.RemainingDataStorage__c = string.valueOf(Storages.get('RemainingDataStorage'));
- sfClientSO.usedAPI__c = string.valueOf(APIs.get('usedApi'));
- sfClientSO.remApi__c = string.valueOf(APIs.get('remApi'));
- sfClientSO.maxApi__c = string.valueOf(APIs.get('maxApi'));
- toUpdateClientSO.add(sfClientSO);
- }
- }
- try {
- if(toUpdateClientSO.size() > 0) update toUpdateClientSO;
- }catch(Exception e){
- System.debug('ERROR: '+e.getMessage() + ' ' +e.getStackTraceString());
- }
- return null;
- }
- public String Send_API(String end_point, String access_token){
- HttpRequest req = new HttpRequest();
- req.setEndpoint(end_point);
- req.setMethod('GET');
- req.setHeader('Authorization' , 'Bearer'+' '+ access_token)
- System.debug('ENDPOINT: '+END_POINT);
- System.debug('Request: '+req);
- Http http = new Http();
- HttpResponse res = http.send(req);
- System.debug('Response: ' + res.getBody());
- return res.getBody();
- }
- public List<Client_Org_Information__c> getOrgInfo(List<Id> orgIds){
- clientOrgSF = [SELECT Id, Instance_URL__c, Access_Token__c FROM
- Client_Org_Information__c WHERE Id IN: orgIds];
- return clientOrgSF;
- }
- }
- trigger Callout on Client_Org_Information__c (after insert, after update) {
- if(System.isFuture()) return;
- List<Id> idS = new List<Id>();
- for(Client_Org_Information__c a : trigger.new){
- idS.add(a.Id);
- }
- OAUTH_Client.getSFOAuth(idS);
- SF_SystemOverview.getSFOverview(idS);
- }
Add Comment
Please, Sign In to add comment