Guest User

Untitled

a guest
Aug 7th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. /*
  2. Copyright (C) 2012 by Kyle Roche
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22.  
  23. public class RestClient {
  24. private String endpoint { get; set; }
  25. private String username { get; set; }
  26. private String password { get; set; }
  27.  
  28. public RestClient() {
  29. // This would require that all defaults have already been set above.
  30. }
  31.  
  32. public RestClient(String username, String password) {
  33. this.username = username;
  34. this.password = password;
  35. }
  36.  
  37. public RestClient(String username, String password, String endpoint) {
  38. this.username = username;
  39. this.password = password;
  40. this.endpoint = endpoint;
  41. }
  42.  
  43. public RestResponse request(String path, String method, Map<String, String> params) {
  44. String encoded = '';
  45. String url = this.endpoint + path;
  46.  
  47. // check for params
  48. if (params != null) {
  49. for (String key : params.keySet()) {
  50. try {
  51. encoded += '&' + key + '=' + EncodingUtil.urlEncode(params.get(key), 'UTF-8');
  52. } catch (Exception e) {
  53. system.debug('ERROR: ' + e);
  54. }
  55. }
  56. encoded = encoded.substring(1); // remove extra chars
  57. }
  58.  
  59. // add parameter string prefix for certain methods
  60. if (method.toUpperCase().equals('GET') || method.toUpperCase().equals('DELETE')) {
  61. url += ((path.indexOf('?') == -1)?'?':'&') + encoded;
  62. }
  63.  
  64. try {
  65. HttpRequest request = new HttpRequest();
  66. request.setEndPoint(url);
  67.  
  68. String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(this.username + ':' + this.password));
  69. request.setHeader('Authorization', authorizationHeader);
  70.  
  71. if (method.toUpperCase().equals('GET')) {
  72. request.setMethod('GET');
  73. }
  74.  
  75. Http http = new Http();
  76. HttpResponse response;
  77.  
  78. if (!Test.isRunningTest()) {
  79. response = http.send(request);
  80. } else {
  81. response = new HttpResponse();
  82. }
  83.  
  84. String decodedString = response.getBody();
  85. system.debug(decodedString);
  86. if (decodedString == null) {
  87. system.debug('ERROR: Response not available');
  88. return null;
  89. }
  90. integer httpStatus = response.getStatusCode();
  91.  
  92. return new RestResponse(decodedString, httpStatus);
  93. } catch (Exception e) {
  94. system.debug('ERROR: ' + e);
  95. return null;
  96. }
  97.  
  98. return null;
  99. }
  100. }
Add Comment
Please, Sign In to add comment