mirror of
https://github.com/retailcrm/api-client-dotnet.git
synced 2024-11-25 06:16:02 +03:00
initial commit
This commit is contained in:
parent
86bebb7471
commit
bddb0b9690
20
IntaroCrm.sln
Normal file
20
IntaroCrm.sln
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetailCrm", "IntaroCrm\RetailCrm.csproj", "{1C407E40-0B79-4593-AC79-03BA8DD76DD1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{1C407E40-0B79-4593-AC79-03BA8DD76DD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1C407E40-0B79-4593-AC79-03BA8DD76DD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1C407E40-0B79-4593-AC79-03BA8DD76DD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1C407E40-0B79-4593-AC79-03BA8DD76DD1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
21
RetailCrm/ApiException.cs
Normal file
21
RetailCrm/ApiException.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace RetailCrm
|
||||
{
|
||||
public class ApiException : Exception
|
||||
{
|
||||
public ApiException()
|
||||
{
|
||||
}
|
||||
|
||||
public ApiException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public ApiException(string message, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
21
RetailCrm/CurlException.cs
Normal file
21
RetailCrm/CurlException.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace RetailCrm
|
||||
{
|
||||
public class CurlException : Exception
|
||||
{
|
||||
public CurlException()
|
||||
{
|
||||
}
|
||||
|
||||
public CurlException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public CurlException(string message, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
36
RetailCrm/Properties/AssemblyInfo.cs
Normal file
36
RetailCrm/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Управление общими сведениями о сборке осуществляется с помощью
|
||||
// набора атрибутов. Измените значения этих атрибутов, чтобы изменить сведения,
|
||||
// связанные со сборкой.
|
||||
[assembly: AssemblyTitle("IntaroCrm")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("IntaroCrm")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Параметр ComVisible со значением FALSE делает типы в сборке невидимыми
|
||||
// для COM-компонентов. Если требуется обратиться к типу в этой сборке через
|
||||
// COM, задайте атрибуту ComVisible значение TRUE для этого типа.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Следующий GUID служит для идентификации библиотеки типов, если этот проект будет видимым для COM
|
||||
[assembly: Guid("9a3f29a5-d878-430c-b7ee-8548d4d03f43")]
|
||||
|
||||
// Сведения о версии сборки состоят из следующих четырех значений:
|
||||
//
|
||||
// Основной номер версии
|
||||
// Дополнительный номер версии
|
||||
// Номер построения
|
||||
// Редакция
|
||||
//
|
||||
// Можно задать все значения или принять номер построения и номер редакции по умолчанию,
|
||||
// используя "*", как показано ниже:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
708
RetailCrm/RestApi.cs
Normal file
708
RetailCrm/RestApi.cs
Normal file
@ -0,0 +1,708 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections;
|
||||
|
||||
namespace RetailCrm
|
||||
{
|
||||
public class RestApi
|
||||
{
|
||||
protected string apiUrl;
|
||||
protected string apiKey;
|
||||
protected string apiVersion = "3";
|
||||
protected DateTime generatedAt;
|
||||
protected Dictionary<string, string> parameters;
|
||||
|
||||
private string userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
|
||||
private string contentType = "application/x-www-form-urlencoded";
|
||||
|
||||
/// <param name="crmUrl">адрес CRM</param>
|
||||
/// <param name="crmKey">ключ для работы с api</param>
|
||||
public RestApi(string crmUrl, string crmKey)
|
||||
{
|
||||
apiUrl = crmUrl + "/api/v" + apiVersion + "/";
|
||||
apiKey = crmKey;
|
||||
parameters = new Dictionary<string, string>();
|
||||
parameters.Add("apiKey", apiKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение заказа по id
|
||||
/// </summary>
|
||||
/// <param name="id">идентификатор заказа</param>
|
||||
/// <param name="by">поиск заказа по id или externalId</param>
|
||||
/// <returns>информация о заказе</returns>
|
||||
public Dictionary<string, object> orderGet(int id, string by)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/" + id.ToString();
|
||||
if (by.Equals("externalId"))
|
||||
{
|
||||
parameters.Add("by", by);
|
||||
}
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание заказа
|
||||
/// </summary>
|
||||
/// <param name="order">информация о заказе</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderCreate(Dictionary<string, object> order)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/create";
|
||||
string dataJson = JsonConvert.SerializeObject(order);
|
||||
parameters.Add("order", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение заказа
|
||||
/// </summary>
|
||||
/// <param name="order">информация о заказе</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderEdit(Dictionary<string, object> order)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/" + order["externalId"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(order);
|
||||
parameters.Add("order", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Пакетная загрузка заказов
|
||||
/// </summary>
|
||||
/// <param name="orders">массив заказов</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderUpload(Dictionary<string, object> orders)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/upload";
|
||||
string dataJson = JsonConvert.SerializeObject(orders);
|
||||
parameters.Add("orders", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
if (result.ContainsKey("uploadedOrders") && result != null)
|
||||
{
|
||||
return getDictionary(result["uploadedOrders"]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновление externalId у заказов с переданными id
|
||||
/// </summary>
|
||||
/// <param name="orders">массив, содержащий id и externalId заказа</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderFixExternalIds(Dictionary<string, object> orders)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/fix-external-ids";
|
||||
string dataJson = JsonConvert.SerializeObject(orders);
|
||||
parameters.Add("orders", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение последних измененных заказов
|
||||
/// </summary>
|
||||
/// <param name="startDate">начальная дата выборки</param>
|
||||
/// <param name="endDate">конечная дата</param>
|
||||
/// <param name="limit">ограничение на размер выборки</param>
|
||||
/// <param name="offset">сдвиг</param>
|
||||
/// <returns>массив заказов</returns>
|
||||
public Dictionary<string, object> orderHistory(DateTime startDate, DateTime endDate, int limit, int offset)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "orders/history";
|
||||
parameters.Add("startDate", startDate.ToString());
|
||||
parameters.Add("startDate", endDate.ToString());
|
||||
parameters.Add("limit", limit.ToString());
|
||||
parameters.Add("offset", offset.ToString());
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение клиента по id
|
||||
/// </summary>
|
||||
/// <param name="id">идентификатор</param>
|
||||
/// <param name="by">поиск заказа по id или externalId</param>
|
||||
/// <returns>информация о клиенте</returns>
|
||||
public Dictionary<string, object> customerGet(string id, string by)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/" + id;
|
||||
if (by.Equals("externalId"))
|
||||
{
|
||||
parameters.Add("by", by);
|
||||
}
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка клиентов в соответсвии с запросом
|
||||
/// </summary>
|
||||
/// <param name="phone">телефон</param>
|
||||
/// <param name="email">почтовый адрес</param>
|
||||
/// <param name="fio">фио пользователя</param>
|
||||
/// <param name="limit">ограничение на размер выборки</param>
|
||||
/// <param name="offset">сдвиг</param>
|
||||
/// <returns>массив клиентов</returns>
|
||||
public Dictionary<string, object> customers(string phone, string email, string fio, int limit, int offset)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers";
|
||||
parameters.Add("phone", phone);
|
||||
parameters.Add("email", email);
|
||||
parameters.Add("fio", fio);
|
||||
parameters.Add("limit", limit.ToString());
|
||||
parameters.Add("offset", offset.ToString());
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание клиента
|
||||
/// </summary>
|
||||
/// <param name="customer">информация о клиенте</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> customerCreate(Dictionary<string, object> customer)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/create";
|
||||
string dataJson = JsonConvert.SerializeObject(customer);
|
||||
parameters.Add("customer", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование клиента
|
||||
/// </summary>
|
||||
/// <param name="customer">информация о клиенте</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> customerEdit(Dictionary<string, object> customer)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/" + customer["externalId"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(customer);
|
||||
parameters.Add("customer", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Пакетная загрузка клиентов
|
||||
/// </summary>
|
||||
/// <param name="customers">массив клиентов</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> customerUpload(Dictionary<string, object> customers)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/upload";
|
||||
string dataJson = JsonConvert.SerializeObject(customers);
|
||||
parameters.Add("customers", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
if (result.ContainsKey("uploaded") && result != null)
|
||||
{
|
||||
return getDictionary(result["uploaded"]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновление externalId у клиентов с переданными id
|
||||
/// </summary>
|
||||
/// <param name="customers">массив, содержащий id и externalId заказа</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> customerFixExternalIds(Dictionary<string, object> customers)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/fix-external-ids";
|
||||
string dataJson = JsonConvert.SerializeObject(customers);
|
||||
parameters.Add("customers", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка заказов клиента
|
||||
/// </summary>
|
||||
/// <param name="id">идентификатор клиента</param>
|
||||
/// <param name="startDate">начальная дата выборки</param>
|
||||
/// <param name="endDate">конечная дата</param>
|
||||
/// <param name="limit">ограничение на размер выборки</param>
|
||||
/// <param name="offset">сдвиг</param>
|
||||
/// <param name="by">поиск заказа по id или externalId</param>
|
||||
/// <returns>массив заказов</returns>
|
||||
public Dictionary<string, object> customerOrdersList(string id, DateTime startDate, DateTime endDate, int limit, int offset, string by)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "customers/" + id + "/orders";
|
||||
if (by.Equals("externalId"))
|
||||
{
|
||||
parameters.Add("by", by);
|
||||
}
|
||||
parameters.Add("startDate", startDate.ToString());
|
||||
parameters.Add("endDate", endDate.ToString());
|
||||
parameters.Add("limit", limit.ToString());
|
||||
parameters.Add("offset", offset.ToString());
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка типов доставки
|
||||
/// </summary>
|
||||
/// <returns>массив типов доставки</returns>
|
||||
public Dictionary<string, object> deliveryTypesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/delivery-types";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование типа доставки
|
||||
/// </summary>
|
||||
/// <param name="deliveryType">информация о типе доставки</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> deliveryTypeEdit(Dictionary<string, object> deliveryType)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/delivery-types/" + deliveryType["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(deliveryType);
|
||||
parameters.Add("deliveryType", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка служб доставки
|
||||
/// </summary>
|
||||
/// <returns>массив служб доставки</returns>
|
||||
public Dictionary<string, object> deliveryServicesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/delivery-services";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование службы доставки
|
||||
/// </summary>
|
||||
/// <param name="deliveryService">информация о службе доставки</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> deliveryServiceEdit(Dictionary<string, object> deliveryService)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/delivery-services/" + deliveryService["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(deliveryService);
|
||||
parameters.Add("deliveryService", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка типов оплаты
|
||||
/// </summary>
|
||||
/// <returns>массив типов оплаты</returns>
|
||||
public Dictionary<string, object> paymentTypesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/payment-types";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование типа оплаты
|
||||
/// </summary>
|
||||
/// <param name="paymentType">информация о типе оплаты</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> paymentTypesEdit(Dictionary<string, object> paymentType)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/payment-types/" + paymentType["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(paymentType);
|
||||
parameters.Add("paymentType", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка статусов оплаты
|
||||
/// </summary>
|
||||
/// <returns>массив статусов оплаты</returns>
|
||||
public Dictionary<string, object> paymentStatusesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/payment-statuses";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование статуса оплаты
|
||||
/// </summary>
|
||||
/// <param name="paymentStatus">информация о статусе оплаты</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> paymentStatusesEdit(Dictionary<string, object> paymentStatus)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/payment-statuses/" + paymentStatus["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(paymentStatus);
|
||||
parameters.Add("paymentStatus", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка типов заказа
|
||||
/// </summary>
|
||||
/// <returns>массив типов заказа</returns>
|
||||
public Dictionary<string, object> orderTypesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/order-types";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование типа заказа
|
||||
/// </summary>
|
||||
/// <param name="orderType">информация о типе заказа</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderTypesEdit(Dictionary<string, object> orderType)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/order-types/" + orderType["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(orderType);
|
||||
parameters.Add("orderType", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка способов оформления заказа
|
||||
/// </summary>
|
||||
/// <returns>массив способов оформления заказа</returns>
|
||||
public Dictionary<string, object> orderMethodsList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/order-methods";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование способа оформления заказа
|
||||
/// </summary>
|
||||
/// <param name="orderMethod">информация о способе оформления заказа</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderMethodsEdit(Dictionary<string, object> orderMethod)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/order-methods/" + orderMethod["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(orderMethod);
|
||||
parameters.Add("orderMethod", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка статусов заказа
|
||||
/// </summary>
|
||||
/// <returns>массив статусов заказа</returns>
|
||||
public Dictionary<string, object> orderStatusesList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/statuses";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование статуса заказа
|
||||
/// </summary>
|
||||
/// <param name="status">информация о статусе заказа</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> orderStatusEdit(Dictionary<string, object> status)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/statuses/" + status["code"].ToString() + "/edit";
|
||||
string dataJson = JsonConvert.SerializeObject(status);
|
||||
parameters.Add("status", dataJson);
|
||||
result = request(url, "POST");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка групп статусов заказа
|
||||
/// </summary>
|
||||
/// <returns>массив групп статусов заказа</returns>
|
||||
public Dictionary<string, object> orderStatusGroupsList()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "reference/status-groups";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновление статистики
|
||||
/// </summary>
|
||||
/// <returns>статус выполненного обновления</returns>
|
||||
public Dictionary<string, object> statisticUpdate()
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
string url = apiUrl + "statistic/update";
|
||||
result = request(url, "GET");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <returns>дата генерации</returns>
|
||||
public DateTime getGeneratedAt()
|
||||
{
|
||||
return generatedAt;
|
||||
}
|
||||
|
||||
/// <param name="url"></param>
|
||||
/// <param name="method"></param>
|
||||
/// <returns></returns>
|
||||
protected Dictionary<string, object> request(string url, string method)
|
||||
{
|
||||
Dictionary<string, object> data = new Dictionary<string, object>();
|
||||
string urlParameters = httpBuildQuery(parameters);
|
||||
|
||||
if (method.Equals("GET") && urlParameters.Length > 0)
|
||||
{
|
||||
url += "?" + urlParameters;
|
||||
}
|
||||
|
||||
Exception exception = null;
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
|
||||
request.Method = method;
|
||||
|
||||
if (method.Equals("POST"))
|
||||
{
|
||||
UTF8Encoding encoding = new UTF8Encoding();
|
||||
byte[] postBytes = encoding.GetBytes(urlParameters);
|
||||
|
||||
request.ContentType = contentType;
|
||||
request.ContentLength = postBytes.Length;
|
||||
request.UserAgent = userAgent;
|
||||
|
||||
Stream postStream = request.GetRequestStream();
|
||||
postStream.Write(postBytes, 0, postBytes.Length);
|
||||
postStream.Flush();
|
||||
postStream.Close();
|
||||
}
|
||||
|
||||
HttpWebResponse response = null;
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
response = (HttpWebResponse)ex.Response;
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
if (request == null || response == null)
|
||||
{
|
||||
throw new CurlException(exception.ToString(), exception);
|
||||
}
|
||||
|
||||
int statusCode = (int)response.StatusCode;
|
||||
|
||||
Stream dataStream = response.GetResponseStream();
|
||||
|
||||
StreamReader reader = new StreamReader(dataStream);
|
||||
string serverResponse = reader.ReadToEnd();
|
||||
|
||||
parameters.Clear();
|
||||
parameters.Add("apiKey", apiKey);
|
||||
|
||||
data = jsonDecode(serverResponse);
|
||||
|
||||
if (data.ContainsKey("generatedAt"))
|
||||
{
|
||||
generatedAt = DateTime.ParseExact(data["generatedAt"].ToString(), "Y-m-d H:i:s",
|
||||
System.Globalization.CultureInfo.InvariantCulture);
|
||||
data.Remove("generatedAt");
|
||||
}
|
||||
|
||||
if (statusCode >= 400 || (data.ContainsKey("success") && !(bool)data["success"]))
|
||||
{
|
||||
throw new ApiException(getErrorMessage(data));
|
||||
}
|
||||
|
||||
data.Remove("success");
|
||||
|
||||
if (data.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
protected string getErrorMessage(Dictionary<string, object> data)
|
||||
{
|
||||
string error = "";
|
||||
|
||||
if (data.ContainsKey("message"))
|
||||
{
|
||||
error = data["message"].ToString();
|
||||
}
|
||||
else if (data.ContainsKey("0"))
|
||||
{
|
||||
Dictionary<string, object> sub = getDictionary(data["0"]);
|
||||
if (sub.ContainsKey("message"))
|
||||
{
|
||||
error = sub["message"].ToString();
|
||||
}
|
||||
|
||||
}
|
||||
else if (data.ContainsKey("errorMsg"))
|
||||
{
|
||||
error = data["errorMsg"].ToString();
|
||||
}
|
||||
else if (data.ContainsKey("error"))
|
||||
{
|
||||
Dictionary<string, object> sub = getDictionary(data["error"]);
|
||||
if (sub.ContainsKey("message"))
|
||||
{
|
||||
error = sub["message"].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
if (data.ContainsKey("errors"))
|
||||
{
|
||||
Dictionary<string, object> sub = getDictionary(data["errors"]);
|
||||
foreach (KeyValuePair<string, object> kvp in data)
|
||||
{
|
||||
error += ". " + kvp.Value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> getDictionary(object data)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
IDictionary idict = (IDictionary)data;
|
||||
|
||||
foreach (object key in idict.Keys)
|
||||
{
|
||||
result.Add(key.ToString(), idict[key]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <param name="json"></param>
|
||||
/// <returns></returns>
|
||||
protected Dictionary<string, object> jsonDecode(string json)
|
||||
{
|
||||
Dictionary<string, object> data = new Dictionary<string, object>();
|
||||
data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
|
||||
data = jsonToDictionary(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
protected static Dictionary<string, object> jsonToDictionary(Dictionary<string, object> data)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
foreach (KeyValuePair<string, object> kvp in data)
|
||||
{
|
||||
string key = kvp.Key;
|
||||
object value = kvp.Value;
|
||||
|
||||
if (value.GetType() == typeof(JObject))
|
||||
{
|
||||
Dictionary<string, object> valueJson = JsonConvert.DeserializeObject<Dictionary<string, object>>(value.ToString());
|
||||
value = jsonToDictionary(valueJson);
|
||||
}
|
||||
result.Add(key, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
protected string httpBuildQuery(Dictionary<string, string> data)
|
||||
{
|
||||
string queryString = null;
|
||||
foreach (KeyValuePair<string, string> kvp in data)
|
||||
{
|
||||
queryString += kvp.Key + "=" + kvp.Value + "&";
|
||||
}
|
||||
|
||||
if (queryString.Length > 0)
|
||||
{
|
||||
queryString = queryString.Substring(0, queryString.Length - 1);
|
||||
}
|
||||
|
||||
return queryString;
|
||||
}
|
||||
}
|
||||
}
|
68
RetailCrm/RetailCrm.csproj
Normal file
68
RetailCrm/RetailCrm.csproj
Normal file
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{1C407E40-0B79-4593-AC79-03BA8DD76DD1}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>RetailCrm</RootNamespace>
|
||||
<AssemblyName>IntaroCrm</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<AssemblyOriginatorKeyFile>intaro.pfx</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ApiException.cs" />
|
||||
<Compile Include="RestApi.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="CurlException.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="intaro.pfx" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
4
RetailCrm/packages.config
Normal file
4
RetailCrm/packages.config
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Reference in New Issue
Block a user