Guest User

Untitled

a guest
Aug 6th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. public class OAUTH_Client {
  2. Public String login_url {set; get;} //login.salesforce.com || test.salesforce.com
  3. Public Boolean type_z {set; get;}
  4. Public String oauth_url = '/services/oauth2/token';
  5. Public List<Client_Org_Information__c> clientOrgSF {set; get;}
  6.  
  7. public OAUTH_Client() {}
  8.  
  9. @future(callout=true)
  10. public static void getSFOAuth(List<Id> orgIds){
  11. OAUTH_Client newOC = new OAUTH_Client();
  12. newOC.execute_SFOAuth(orgIds);
  13.  
  14. }
  15.  
  16.  
  17. public String execute_SFOAuth(List<Id> orgIds){
  18. List<Client_Org_Information__c> clientSO = getOrgInfo(orgIds);
  19. String end_point;
  20. String client_id;
  21. String client_secret;
  22. String username;
  23. String password;
  24. List<Client_Org_Information__c> toUpdateClientS = new
  25. List<Client_Org_Information__c>();
  26. if(clientSO.size() > 0){
  27.  
  28. for(Client_Org_Information__c org: clientSO){
  29.  
  30. end_point = (org.type__c == 'Sandbox') ? 'https://test.saleforce.com' : 'https://login.salesforce.com';
  31. end_point += oauth_url;
  32. client_id = org.client_id__c;
  33. client_secret = org.client_secret__c;
  34. username = org.username__c;
  35. password = org.password__c;
  36.  
  37. String JSON_BODY = Send_API(end_point, client_id, client_secret, username, password);
  38.  
  39. Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(JSON_BODY);
  40.  
  41. Client_Org_Information__c sfClient = new Client_Org_Information__c();
  42. sfClient.Id = org.Id;
  43. sfClient.instance_url__c = string.valueOf(m.get('instance_url'));
  44. sfClient.access_token__c = string.valueOf(m.get('access_token'));
  45.  
  46. toUpdateClientS.add(sfClient);
  47.  
  48. }
  49. }
  50.  
  51. try {
  52. if(toUpdateClientS.size() > 0) update toUpdateClientS;
  53. }catch(Exception e){
  54. System.debug('ERROR: '+e.getMessage() + ' ' +e.getStackTraceString());
  55. }
  56. return null;
  57. }
  58.  
  59. public static String Send_API(String END_POINT, String CLIENT_ID, String CLIENT_SECRET, String USERNAME, String PASSWORD){
  60.  
  61. HttpRequest req = new HttpRequest();
  62. req.setEndpoint(END_POINT);
  63. req.setMethod('POST');
  64. req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  65. String payLoad = 'grant_type=password'
  66. + '&username=' + USERNAME
  67. + '&password=' + PASSWORD
  68. + '&client_id=' + CLIENT_ID
  69. + '&client_secret=' + CLIENT_SECRET;
  70. //+ '&scope=PRODUCTION';
  71. req.setBody(payLoad);
  72. System.debug('ENDPOINT: '+END_POINT);
  73. System.debug('payLoad: '+payLoad);
  74. System.debug('Request: '+req);
  75. Http http = new Http();
  76. HttpResponse res = http.send(req);
  77. System.debug('REsponse: ' + res.getBody());
  78. return res.getBody();
  79.  
  80. }
  81.  
  82. public List<Client_Org_Information__c> getOrgInfo(List<Id> orgIds){
  83.  
  84. clientOrgSF = [SELECT Id, client_id__c, type__c, client_secret__c, username__c, password__c FROM Client_Org_Information__c WHERE Id IN: orgIds];
  85. return clientOrgSF;
  86. }
  87.  
  88. }
  89.  
  90. public class SF_SystemOverview {
  91. Public String endpoint_url = '/services/apexrest/api/PackageLicense';
  92. Public List<Client_Org_Information__c> clientOrgSF {set; get;}
  93. Public List<Client_Org_System_Overview__c> clientOrgSO {set; get;}
  94.  
  95. public SF_SystemOverview() {}
  96.  
  97. @future(callout=true)
  98. public static void getSFOverview(List<Id> orgIds){
  99. SF_SystemOverview newOC = new SF_SystemOverview();
  100. newOC.execute_SFOverview(orgIds);
  101.  
  102. }
  103.  
  104. public String execute_SFOverview(List<Id> orgIds){
  105. List<Client_Org_Information__c> clientSO = getOrgInfo(orgIds);
  106. String end_point;
  107. String access_token;
  108. List<Client_Org_System_Overview__c> toUpdateClientSO = new
  109. List<Client_Org_System_Overview__c>();
  110. if(clientSO.size() > 0){
  111.  
  112. for(Client_Org_Information__c org: clientSO){
  113.  
  114. end_point = org.Instance_URL__c;
  115. end_point += endpoint_url;
  116. access_token = org.Access_Token__c;
  117.  
  118. String JSON_BODY = Send_API(end_point, access_token);
  119.  
  120. Client_Org_System_Overview__c sfClientSO = new
  121. Client_Org_System_Overview__c();
  122. Map<String, Object> m = (Map<String,
  123. Object>)JSON.deserializeUntyped(JSON_BODY);
  124. Map<String, Object> Storages = (Map<String,Object>)m.get('Storage');
  125. Map<String, Object> APIs = (Map<String,Object>)m.get('Api_Usage');
  126. sfClientSO.client_id__c = org.Id;
  127. sfClientSO.UsedDataStorage__c = string.valueOf(Storages.get('UsedDataStorage'));
  128. sfClientSO.TotalDataStorage__c = string.valueOf(Storages.get('TotalDataStorage'));
  129. sfClientSO.RemainingDataStorage__c = string.valueOf(Storages.get('RemainingDataStorage'));
  130. sfClientSO.usedAPI__c = string.valueOf(APIs.get('usedApi'));
  131. sfClientSO.remApi__c = string.valueOf(APIs.get('remApi'));
  132. sfClientSO.maxApi__c = string.valueOf(APIs.get('maxApi'));
  133.  
  134. toUpdateClientSO.add(sfClientSO);
  135.  
  136. }
  137.  
  138. }
  139.  
  140. try {
  141. if(toUpdateClientSO.size() > 0) update toUpdateClientSO;
  142. }catch(Exception e){
  143. System.debug('ERROR: '+e.getMessage() + ' ' +e.getStackTraceString());
  144. }
  145. return null;
  146. }
  147. public String Send_API(String end_point, String access_token){
  148.  
  149. HttpRequest req = new HttpRequest();
  150. req.setEndpoint(end_point);
  151. req.setMethod('GET');
  152. req.setHeader('Authorization' , 'Bearer'+' '+ access_token)
  153.  
  154. System.debug('ENDPOINT: '+END_POINT);
  155. System.debug('Request: '+req);
  156. Http http = new Http();
  157. HttpResponse res = http.send(req);
  158. System.debug('Response: ' + res.getBody());
  159. return res.getBody();
  160.  
  161. }
  162.  
  163. public List<Client_Org_Information__c> getOrgInfo(List<Id> orgIds){
  164. clientOrgSF = [SELECT Id, Instance_URL__c, Access_Token__c FROM
  165. Client_Org_Information__c WHERE Id IN: orgIds];
  166. return clientOrgSF;
  167. }
  168. }
  169.  
  170. trigger Callout on Client_Org_Information__c (after insert, after update) {
  171.  
  172. if(System.isFuture()) return;
  173.  
  174. List<Id> idS = new List<Id>();
  175. for(Client_Org_Information__c a : trigger.new){
  176. idS.add(a.Id);
  177. }
  178.  
  179. OAUTH_Client.getSFOAuth(idS);
  180. SF_SystemOverview.getSFOverview(idS);
  181. }
Add Comment
Please, Sign In to add comment