{"action":"opened","issue":{"url":"https://api.github.com/repos/jottley/spring-social-salesforce/issues/37","repository_url":"https://api.github.com/repos/jottley/spring-social-salesforce","labels_url":"https://api.github.com/repos/jottley/spring-social-salesforce/issues/37/labels{/name}","comments_url":"https://api.github.com/repos/jottley/spring-social-salesforce/issues/37/comments","events_url":"https://api.github.com/repos/jottley/spring-social-salesforce/issues/37/events","html_url":"https://github.com/jottley/spring-social-salesforce/issues/37","id":374773965,"node_id":"MDU6SXNzdWUzNzQ3NzM5NjU=","number":37,"title":"Connectivity & Error Handling","user":{"login":"kemuning","id":23009699,"node_id":"MDQ6VXNlcjIzMDA5Njk5","avatar_url":"https://avatars2.githubusercontent.com/u/23009699?v=4","gravatar_id":"","url":"https://api.github.com/users/kemuning","html_url":"https://github.com/kemuning","followers_url":"https://api.github.com/users/kemuning/followers","following_url":"https://api.github.com/users/kemuning/following{/other_user}","gists_url":"https://api.github.com/users/kemuning/gists{/gist_id}","starred_url":"https://api.github.com/users/kemuning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kemuning/subscriptions","organizations_url":"https://api.github.com/users/kemuning/orgs","repos_url":"https://api.github.com/users/kemuning/repos","events_url":"https://api.github.com/users/kemuning/events{/privacy}","received_events_url":"https://api.github.com/users/kemuning/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-28T16:50:58Z","updated_at":"2018-10-28T16:50:58Z","closed_at":null,"author_association":"NONE","body":"I went through the wiki page, but I still need your help on the best practice.\r\n\r\nAt present, here is the high level structure of my Spring projet to invoke Apex Rest calls :\r\n\r\n* MyController.java\r\n```\r\n...\r\n@GetMapping(\"/opportunities\")\r\npublic ResponseEntity<String> getOpportunities() {\r\n   return myService.getOpportunities();\r\n}\r\n\r\n@PostMapping(\"/leads\")\r\npublic ResponseEntity<String> addLeads(@RequestBody Leads l) {\r\n   return myService.addLeads(l);\r\n}\r\n\r\n@PostMapping(\"/accounts\")\r\npublic ResponseEntity<String> addAccounts(@RequestBody Accounts acc) {\r\n   return myService.addAccounts(acc);\r\n}\r\n...\r\n```\r\n\r\n* MyService.java\r\n```\r\n...\r\n@Override\r\npublic ResponseEntity<String> getOpportunities() {\r\n\r\n    // Initiate Salesforce connectivity & get the OAuth2 token\r\n    // ClientID & Client  Secret are defined in the Salesforce's Connected App\r\n    // Username is a user ( kind of an admin) who is granted permission to execute the Apex Rest class\r\n    SalesforceConnectionFactory salesforceConnectionFactory = new SalesforceConnectionFactory(getClientId(), getClientSecret(), true);\r\n    AccessGrant accessGrant = salesforceConnectionFactory.getOAuthOperations().exchangeCredentialsForAccess(getUsername(), getPassword(), null);\r\n\t\t\r\n    // Add the token in the header\r\n    HttpHeaders headers = new HttpHeaders();\r\n    headers.add(\"Authorization\", \"Bearer \" + accessGrant.getAccessToken());\r\n    headers.add(\"Accept\", \"application/json\");\r\n\r\n    HttpEntity<Piste> request = new HttpEntity<>(headers);\r\n    RestTemplate restTemplate = new RestTemplate();\r\n    restTemplate.setErrorHandler(new ApexRestErrorHandler());\r\n        \r\n    // Rest URL for Opportunities : https://<instance-of-salesforce>/services/apexrest/v1/opportunities\r\n    ResponseEntity<String> response = restTemplate.exchange(getApexrestUrlOpportunities(), HttpMethod.GET, request, String.class);\r\n\t    \t    \t    \r\n    return response;\r\n}\r\n\r\n@Override\r\npublic ResponseEntity<String> addLeads(Leads leads) {\r\n\r\n    // Initiate Salesforce connectivity & get the OAuth2 token\r\n    // ClientID & Client  Secret are defined in the Salesforce's Connected App\r\n    // Username is a user ( kind of an admin) who is granted permission to execute the Apex Rest class\r\n    SalesforceConnectionFactory salesforceConnectionFactory = new SalesforceConnectionFactory(getClientId(), getClientSecret(), true);\r\n    AccessGrant accessGrant = salesforceConnectionFactory.getOAuthOperations().exchangeCredentialsForAccess(getUsername(), getPassword(), null);\r\n\t\t\r\n    // Add the token in the header\r\n    HttpHeaders headers = new HttpHeaders();\r\n    headers.add(\"Authorization\", \"Bearer \" + accessGrant.getAccessToken());\r\n    headers.add(\"Accept\", \"application/json\");\r\n\r\n    HttpEntity<Leads> request = new HttpEntity<>(leads, headers);\r\n    RestTemplate restTemplate = new RestTemplate();\r\n    restTemplate.setErrorHandler(new ApexRestErrorHandler());\r\n        \r\n    // Rest URL for Opportunities : https://<instance-of-salesforce>/services/apexrest/v1/leads\r\n    ResponseEntity<String> response = restTemplate.exchange(getApexrestUrlLeads(), HttpMethod.POST, request, String.class);\r\n\t    \t    \t    \r\n    return response;\r\n}\r\n\r\n@Override\r\npublic ResponseEntity<String> addAccounts(Accounts acc) {\r\n\r\n    // Initiate Salesforce connectivity & get the OAuth2 token\r\n    // ClientID & Client  Secret are defined in the Salesforce's Connected App\r\n    // Username is a user ( kind of an admin) who is granted permission to execute the Apex Rest class\r\n    SalesforceConnectionFactory salesforceConnectionFactory = new SalesforceConnectionFactory(getClientId(), getClientSecret(), true);\r\n    AccessGrant accessGrant = salesforceConnectionFactory.getOAuthOperations().exchangeCredentialsForAccess(getUsername(), getPassword(), null);\r\n\t\t\r\n    // Add the token in the header\r\n    HttpHeaders headers = new HttpHeaders();\r\n    headers.add(\"Authorization\", \"Bearer \" + accessGrant.getAccessToken());\r\n    headers.add(\"Accept\", \"application/json\");\r\n\r\n    HttpEntity<Accounts> request = new HttpEntity<>(acc, headers);\r\n    RestTemplate restTemplate = new RestTemplate();\r\n    restTemplate.setErrorHandler(new ApexRestErrorHandler());\r\n        \r\n    // Rest URL for Opportunities : https://<instance-of-salesforce>/services/apexrest/v1/accounts\r\n    ResponseEntity<String> response = restTemplate.exchange(getApexrestUrlAccounts(), HttpMethod.POST, request, String.class);\r\n\t    \t    \t    \r\n    return response;\r\n}\r\n...\r\n```\r\n\r\nHere are my questions :\r\n\r\n1. The code to initiate the Salesforce connectivity as well as the injection of the token are repeated. This is probably a bad design. What is the recommeded practice to do those two ? Any example ?\r\n\r\n2. Should we use SalesforceErrorHandler or just ErrorHandler ? What are the differences ?\r\n\r\nThanks\r\n\r\n"}}
{"actor":{"display_login":"kemuning"}}