Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using RestSharp;
- public class Magento
- {
- private readonly string _host;
- private readonly string _username;
- private readonly string _password;
- public Magento(string host, string username, string password)
- {
- _host = host;
- _username = username;
- _password = password;
- }
- /// <summary>
- /// Gets the bearer token by user name and password
- /// </summary>
- /// <returns>Magento bearer token</returns>
- public string GetBearerToken()
- {
- var client = new RestClient { BaseUrl = new Uri(_host) };
- var request = new RestRequest(Method.POST);
- request.AddHeader("Content-Type", "application/json");
- request.Resource = "rest/V1/integration/admin/token";
- request.AddJsonBody(new { username = _username, password = _password });
- var response = client.Execute(request);
- return response.Content;
- }
- /// <summary>
- /// Gets all customers
- /// </summary>
- /// <returns>Magento response containing customers info</returns>
- public MagentoResponse GetAllCustomers()
- {
- var token = GetBearerToken().Trim('"');
- var client = new RestClient { BaseUrl = new Uri(_host) };
- var request = new RestRequest(Method.GET);
- request.AddHeader("Content-Type", "application/json");
- request.AddHeader("Authorization", "Bearer " + token);
- request.Resource = "rest/V1/customers/search?searchCriteria";
- var response = client.Execute<MagentoResponse>(request);
- return response.Data;
- }
- /// <summary>
- /// Class representing Magento response after customers request
- /// </summary>
- public class MagentoResponse
- {
- public List<Customer> Items { get; set; }
- public Search_Criteria search_criteria { get; set; }
- public int total_count { get; set; }
- }
- public class Search_Criteria
- {
- public List<string> filter_groups { get; set; }
- public int page_size { get; set; }
- }
- public class Customer
- {
- public int id { get; set; }
- public int group_id { get; set; }
- public string default_billing { get; set; }
- public string default_shipping { get; set; }
- public string created_at { get; set; }
- public string updated_at { get; set; }
- public string created_in { get; set; }
- public string email { get; set; }
- public string firstname { get; set; }
- public string lastname { get; set; }
- public int store_id { get; set; }
- public int website_id { get; set; }
- public List<string> addresses { get; set; }
- public int disable_auto_group_change { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement