Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using RestSharp;
  4.  
  5. public class Magento
  6. {
  7. private readonly string _host;
  8. private readonly string _username;
  9. private readonly string _password;
  10.  
  11. public Magento(string host, string username, string password)
  12. {
  13. _host = host;
  14. _username = username;
  15. _password = password;
  16. }
  17.  
  18. /// <summary>
  19. /// Gets the bearer token by user name and password
  20. /// </summary>
  21. /// <returns>Magento bearer token</returns>
  22. public string GetBearerToken()
  23. {
  24. var client = new RestClient { BaseUrl = new Uri(_host) };
  25. var request = new RestRequest(Method.POST);
  26. request.AddHeader("Content-Type", "application/json");
  27. request.Resource = "rest/V1/integration/admin/token";
  28. request.AddJsonBody(new { username = _username, password = _password });
  29. var response = client.Execute(request);
  30. return response.Content;
  31. }
  32.  
  33. /// <summary>
  34. /// Gets all customers
  35. /// </summary>
  36. /// <returns>Magento response containing customers info</returns>
  37. public MagentoResponse GetAllCustomers()
  38. {
  39. var token = GetBearerToken().Trim('"');
  40. var client = new RestClient { BaseUrl = new Uri(_host) };
  41. var request = new RestRequest(Method.GET);
  42. request.AddHeader("Content-Type", "application/json");
  43. request.AddHeader("Authorization", "Bearer " + token);
  44. request.Resource = "rest/V1/customers/search?searchCriteria";
  45. var response = client.Execute<MagentoResponse>(request);
  46. return response.Data;
  47. }
  48.  
  49. /// <summary>
  50. /// Class representing Magento response after customers request
  51. /// </summary>
  52. public class MagentoResponse
  53. {
  54. public List<Customer> Items { get; set; }
  55. public Search_Criteria search_criteria { get; set; }
  56. public int total_count { get; set; }
  57. }
  58.  
  59. public class Search_Criteria
  60. {
  61. public List<string> filter_groups { get; set; }
  62. public int page_size { get; set; }
  63. }
  64.  
  65. public class Customer
  66. {
  67. public int id { get; set; }
  68. public int group_id { get; set; }
  69. public string default_billing { get; set; }
  70. public string default_shipping { get; set; }
  71. public string created_at { get; set; }
  72. public string updated_at { get; set; }
  73. public string created_in { get; set; }
  74. public string email { get; set; }
  75. public string firstname { get; set; }
  76. public string lastname { get; set; }
  77. public int store_id { get; set; }
  78. public int website_id { get; set; }
  79. public List<string> addresses { get; set; }
  80. public int disable_auto_group_change { get; set; }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement