mirror of
https://github.com/retailcrm/api-client-dotnet.git
synced 2024-11-22 04:46:02 +03:00
New method
This commit is contained in:
parent
5b0856667d
commit
98114cf816
71
README.md
71
README.md
@ -30,16 +30,71 @@ Constructor arguments are:
|
|||||||
|
|
||||||
``` csharp
|
``` csharp
|
||||||
|
|
||||||
|
ApiClient api;
|
||||||
string url, key;
|
string url, key;
|
||||||
Dictionary<string, object> orderTypes = new Dictionary<string, object>();
|
try
|
||||||
|
{
|
||||||
|
api = new ApiClient(url, key);
|
||||||
|
}
|
||||||
|
catch (WebException e)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine(e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
RestApi api = new RestApi(url, key);
|
Dictionary<string, object> tmpOrder = new Dictionary<string, object>(){
|
||||||
try {
|
{"number", "example"},
|
||||||
orderTypes = api.orderTypesList();
|
{"externalId", "example"},
|
||||||
} catch (ApiException e) {
|
{"createdAt", DateTime.Now.ToString("Y-m-d H:i:s")},
|
||||||
Console.WriteLine(ex.Message);
|
{"discount", 50},
|
||||||
} catch (CurlException e) {
|
{"phone", "89263832233"},
|
||||||
Console.WriteLine(ex.Message);
|
{"email", "vshirokov@gmail.com"},
|
||||||
|
{"customerComment", "example"},
|
||||||
|
{"customFields", new Dictionary<string, object>(){
|
||||||
|
{"reciever_phone", "example"},
|
||||||
|
{"reciever_name", "example"},
|
||||||
|
{"ext_number", "example"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{"contragentType", "individual"},
|
||||||
|
{"orderType", "eshop-individual"},
|
||||||
|
{"orderMethod", "app"},
|
||||||
|
{"customerId", "555"},
|
||||||
|
{"managerId", 8},
|
||||||
|
{"items", new Dictionary<string, object>(){
|
||||||
|
{"0", new Dictionary<string, object>(){
|
||||||
|
{"initialPrice", 100},
|
||||||
|
{"quantity", 1},
|
||||||
|
{"productId", 55},
|
||||||
|
{"productName", "example"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{"delivery", new Dictionary<string, object>(){
|
||||||
|
{"code", "courier"},
|
||||||
|
{"date", DateTime.Now.ToString("Y-m-d")},
|
||||||
|
{"address", new Dictionary<string, object>(){
|
||||||
|
{"text", "exampleing"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ApiResponse response = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
response = api.ordersEdit(order);
|
||||||
|
}
|
||||||
|
catch (WebException e)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine(e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.isSuccessful() && 201 == response["statusCosde"]) {
|
||||||
|
System.Console.WriteLine("Заказ успешно создан. ID заказа в retailCRM = " + response["id"]);
|
||||||
|
} else {
|
||||||
|
System.Console.WriteLine("Ошибка создания заказа: [Статус HTTP-ответа " + response["statusCosde"] + "] " + response["errorMsg"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
@ -1,20 +0,0 @@
|
|||||||
|
|
||||||
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
|
|
755
RetailCrm/ApiClient.cs
Normal file
755
RetailCrm/ApiClient.cs
Normal file
@ -0,0 +1,755 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using RetailCrm.Http;
|
||||||
|
using RetailCrm.Response;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace RetailCrm
|
||||||
|
{
|
||||||
|
public class ApiClient
|
||||||
|
{
|
||||||
|
private const string apiVersion = "v3";
|
||||||
|
protected Client client;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Site code
|
||||||
|
/// </summary>
|
||||||
|
protected string siteCode;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ApiClient creating
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url"></param>
|
||||||
|
/// <param name="apiKey"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
public ApiClient(string url, string apiKey, string site = "")
|
||||||
|
{
|
||||||
|
if ("/" != url.Substring(url.Length - 1, 1))
|
||||||
|
{
|
||||||
|
url += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
url += "api/" + apiVersion;
|
||||||
|
|
||||||
|
client = new Client(url, new Dictionary<string, object>() { { "apiKey", apiKey } });
|
||||||
|
siteCode = site;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a order
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="order"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersCreate(Dictionary<string, object> order, string site = "")
|
||||||
|
{
|
||||||
|
if (order.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `order` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/orders/create",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "order", JsonConvert.SerializeObject(order) }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit a order
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="order"></param>
|
||||||
|
/// <param name="by"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersEdit(Dictionary<string, object> order, string by = "externalId", string site = "")
|
||||||
|
{
|
||||||
|
if (order.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `order` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
checkIdParameter(by);
|
||||||
|
|
||||||
|
if (order.ContainsKey(by) == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Order array must contain the \"" + by + "\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/orders/" + order[by] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "order", JsonConvert.SerializeObject(order) },
|
||||||
|
{ "by", by }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload array of the orders
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="orders"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersUpload(Dictionary<string, object> orders, string site = "")
|
||||||
|
{
|
||||||
|
if (orders.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `order` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/orders/upload",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "orders", JsonConvert.SerializeObject(orders) }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get order by id or externalId
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="by"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersGet(string id, string by = "externalId", string site = "")
|
||||||
|
{
|
||||||
|
checkIdParameter(by);
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/orders/" + id,
|
||||||
|
Client.METHOD_GET,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "by", by }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a orders history
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="startDate"></param>
|
||||||
|
/// <param name="endDate"></param>
|
||||||
|
/// <param name="limit"></param>
|
||||||
|
/// <param name="offset"></param>
|
||||||
|
/// <param name="skipMyChanges"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersHistory(
|
||||||
|
DateTime? startDate = null,
|
||||||
|
DateTime? endDate = null,
|
||||||
|
int limit = 100,
|
||||||
|
int offset = 0,
|
||||||
|
bool skipMyChanges = true
|
||||||
|
)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
if (startDate != null)
|
||||||
|
{
|
||||||
|
parameters.Add("startDate", startDate.Value.ToString("Y-m-d H:i:s"));
|
||||||
|
}
|
||||||
|
if (endDate != null)
|
||||||
|
{
|
||||||
|
parameters.Add("endDate", endDate.Value.ToString("Y-m-d H:i:s"));
|
||||||
|
}
|
||||||
|
if (limit > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("limit", limit);
|
||||||
|
}
|
||||||
|
if (offset > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("offset", offset);
|
||||||
|
}
|
||||||
|
if (skipMyChanges == true)
|
||||||
|
{
|
||||||
|
parameters.Add("skipMyChanges", skipMyChanges);
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest("/orders/history", Client.METHOD_GET, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns filtered orders list
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="page"></param>
|
||||||
|
/// <param name="limit"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersList(Dictionary<string, object> filter = null, int page = 0, int limit = 0)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
if (filter.Count > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("filter", filter);
|
||||||
|
}
|
||||||
|
if (page > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("page", page);
|
||||||
|
}
|
||||||
|
if (limit > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("limit", limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest("/orders", Client.METHOD_GET, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns statuses of the orders
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <param name="externalIds"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersStatuses(Dictionary<string, object> ids = null, Dictionary<string, object> externalIds = null)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
if (ids.Count > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("ids", ids);
|
||||||
|
}
|
||||||
|
if (externalIds.Count > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("externalIds", externalIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest("/orders/statuses", Client.METHOD_GET, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save order IDs' (id and externalId) association in the CRM
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse ordersFixExternalId(Dictionary<string, object> ids)
|
||||||
|
{
|
||||||
|
if (ids.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Method parameter must contains at least one IDs pair");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/orders/fix-external-ids",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "orders", JsonConvert.SerializeObject(ids) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a customer
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="customer"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersCreate(Dictionary<string, object> customer, string site = "")
|
||||||
|
{
|
||||||
|
if (customer.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `customer` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/customers/create",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "customer", JsonConvert.SerializeObject(customer) }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit a customer
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="customer"></param>
|
||||||
|
/// <param name="by"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersEdit(Dictionary<string, object> customer, string by = "externalId", string site = "")
|
||||||
|
{
|
||||||
|
if (customer.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `customer` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
checkIdParameter(by);
|
||||||
|
|
||||||
|
if (customer.ContainsKey(by) == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Customer array must contain the \"" + by + "\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/customers/" + customer[by] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "customer", JsonConvert.SerializeObject(customer) },
|
||||||
|
{ "by", by }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload array of the customers
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="customers"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersUpload(Dictionary<string, object> customers, string site = "")
|
||||||
|
{
|
||||||
|
if (customers.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Parameter `customers` must contains a data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/customers/upload",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "customers", JsonConvert.SerializeObject(customers) }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get customer by id or externalId
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="by"></param>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersGet(string id, string by = "externalId", string site = "")
|
||||||
|
{
|
||||||
|
checkIdParameter(by);
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/customers/" + id,
|
||||||
|
Client.METHOD_GET,
|
||||||
|
this.fillSite(
|
||||||
|
site,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "by", by }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns filtered customers list
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="page"></param>
|
||||||
|
/// <param name="limit"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersList(Dictionary<string, object> filter = null, int page = 0, int limit = 0)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> parameters = new Dictionary<string, object>();
|
||||||
|
if (filter.Count > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("filter", filter);
|
||||||
|
}
|
||||||
|
if (page > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("page", page);
|
||||||
|
}
|
||||||
|
if (limit > 0)
|
||||||
|
{
|
||||||
|
parameters.Add("limit", limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest("/customers", Client.METHOD_GET, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save customer IDs' (id and externalId) association in the CRM
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse customersFixExternalIds(Dictionary<string, object> ids)
|
||||||
|
{
|
||||||
|
if (ids.Count < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Method parameter must contains at least one IDs pair");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/customers/fix-external-ids",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "customers", JsonConvert.SerializeObject(ids) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns deliveryServices list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse deliveryServicesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/delivery-services", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns deliveryTypes list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse deliveryTypesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/delivery-types", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns orderMethods list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse orderMethodsList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/order-methods", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns orderTypes list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse orderTypesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/order-types", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns paymentStatuses list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse paymentStatusesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/payment-statuses", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns paymentTypes list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse paymentTypesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/payment-types", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns productStatuses list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse productStatusesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/product-statuses", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns statusGroups list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse statusGroupsList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/status-groups", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns statuses list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse statusesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/statuses", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns sites list
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse sitesList()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/reference/sites", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit deliveryService
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse deliveryServicesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/delivery-services/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "deliveryService", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit deliveryType
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse deliveryTypesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/delivery-types/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "deliveryType", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit orderMethod
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse orderMethodsEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/order-methods/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "orderMethod", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit orderType
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse orderTypesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/order-types/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "orderType", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit paymentStatus
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse paymentStatusesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/payment-statuses/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "paymentStatus", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit paymentType
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse paymentTypesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/payment-types/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "paymentType", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit productStatus
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse productStatusesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/product-statuses/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "productStatus", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit order status
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse statusesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/statuses/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "status", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edit site
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse sitesEdit(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data.ContainsKey("code") == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Data must contain \"code\" parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.makeRequest(
|
||||||
|
"/reference/sites/" + data["code"] + "/edit",
|
||||||
|
Client.METHOD_POST,
|
||||||
|
new Dictionary<string, object>() {
|
||||||
|
{ "site", JsonConvert.SerializeObject(data) }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update CRM basic statistic
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>ApiResponse</returns>
|
||||||
|
public ApiResponse statisticUpdate()
|
||||||
|
{
|
||||||
|
return client.makeRequest("/statistic/update", Client.METHOD_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return current site
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>string</returns>
|
||||||
|
public string getSite()
|
||||||
|
{
|
||||||
|
return this.siteCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return current site
|
||||||
|
/// </summary>
|
||||||
|
public void setSite(string site)
|
||||||
|
{
|
||||||
|
this.siteCode = site;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check ID parameter
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="by"></param>
|
||||||
|
protected void checkIdParameter(string by)
|
||||||
|
{
|
||||||
|
string[] allowedForBy = new string[]{"externalId", "id"};
|
||||||
|
if (allowedForBy.Contains(by) == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Value \"" + by + "\" for parameter \"by\" is not valid. Allowed values are " + String.Join(", ", allowedForBy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fill params by site value
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="site"></param>
|
||||||
|
/// <param name="param"></param>
|
||||||
|
/// <returns>Dictionary</returns>
|
||||||
|
protected Dictionary<string, object> fillSite(string site, Dictionary<string, object> param)
|
||||||
|
{
|
||||||
|
if (site.Length > 1)
|
||||||
|
{
|
||||||
|
param.Add("site", site);
|
||||||
|
}
|
||||||
|
else if (siteCode.Length > 1)
|
||||||
|
{
|
||||||
|
param.Add("site", siteCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return param;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
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)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
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)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
21
RetailCrm/Exceptions/InvalidJsonException.cs
Normal file
21
RetailCrm/Exceptions/InvalidJsonException.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace RetailCrm.Exceptions
|
||||||
|
{
|
||||||
|
public class InvalidJsonException : Exception
|
||||||
|
{
|
||||||
|
public InvalidJsonException()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidJsonException(string message)
|
||||||
|
: base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidJsonException(string message, Exception inner)
|
||||||
|
: base(message, inner)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
94
RetailCrm/Extra/Tools.cs
Normal file
94
RetailCrm/Extra/Tools.cs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace RetailCrm.Extra
|
||||||
|
{
|
||||||
|
class Tools
|
||||||
|
{
|
||||||
|
public static string httpBuildQuery(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
if (data is Dictionary<string, object> == false)
|
||||||
|
{
|
||||||
|
return String.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parts = new List<string>();
|
||||||
|
HandleItem(data, parts);
|
||||||
|
return String.Join("&", parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void HandleItem(object data, List<string> parts, string prefix = "")
|
||||||
|
{
|
||||||
|
if (data == null) return;
|
||||||
|
|
||||||
|
if (data is Dictionary<string, object>)
|
||||||
|
{
|
||||||
|
parts.Add(FormatDictionary((Dictionary<string, object>)data, prefix));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parts.Add(String.IsNullOrEmpty(data.ToString()) ? String.Empty : String.Format("{0}={1}", prefix, data.ToString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FormatDictionary(Dictionary<string, object> obj, string prefix = "")
|
||||||
|
{
|
||||||
|
var parts = new List<string>();
|
||||||
|
foreach (KeyValuePair<string, object> kvp in obj)
|
||||||
|
{
|
||||||
|
string newPrefix = string.IsNullOrEmpty(prefix) ?
|
||||||
|
String.Format("{0}{1}", prefix, kvp.Key) :
|
||||||
|
String.Format("{0}[{1}]", prefix, kvp.Key);
|
||||||
|
HandleItem(kvp.Value, parts, newPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
return String.Join("&", parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Dictionary<string, object> jsonDecode(string json)
|
||||||
|
{
|
||||||
|
return jsonObjectToDictionary((Dictionary<string, object>)JsonConvert.DeserializeObject<Dictionary<string, object>>(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> jsonObjectToDictionary(Dictionary<string, object> data)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||||
|
foreach (KeyValuePair<string, object> kvp in data)
|
||||||
|
{
|
||||||
|
System.Console.WriteLine(kvp.Key.ToString());
|
||||||
|
System.Console.WriteLine(kvp.Value.ToString());
|
||||||
|
System.Console.ReadLine();
|
||||||
|
object valueObj = kvp.Value;
|
||||||
|
string value = String.Empty;
|
||||||
|
|
||||||
|
if (valueObj.GetType() == typeof(JArray))
|
||||||
|
{
|
||||||
|
string tmpValue = JsonConvert.SerializeObject(((JArray)valueObj).ToArray<dynamic>());
|
||||||
|
value = tmpValue.Replace("[", "{");
|
||||||
|
value = value.Replace("]", "}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value = valueObj.ToString();
|
||||||
|
}
|
||||||
|
System.Console.WriteLine(value);
|
||||||
|
System.Console.ReadLine();
|
||||||
|
if (value != "")
|
||||||
|
{
|
||||||
|
if (valueObj.GetType() == typeof(JObject) || valueObj.GetType() == typeof(JArray))
|
||||||
|
{
|
||||||
|
valueObj = jsonObjectToDictionary((Dictionary<string, object>)JsonConvert.DeserializeObject<Dictionary<string, object>>(value));
|
||||||
|
}
|
||||||
|
result.Add(kvp.Key.ToString(), valueObj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
111
RetailCrm/Http/Client.cs
Normal file
111
RetailCrm/Http/Client.cs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
using RetailCrm.Extra;
|
||||||
|
using RetailCrm.Response;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace RetailCrm.Http
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP client
|
||||||
|
/// </summary>
|
||||||
|
public class Client
|
||||||
|
{
|
||||||
|
public const string METHOD_GET = "GET";
|
||||||
|
public const string METHOD_POST = "POST";
|
||||||
|
private const string USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
|
||||||
|
private const string CONTENT_TYPE = "application/x-www-form-urlencoded";
|
||||||
|
|
||||||
|
protected string url;
|
||||||
|
protected Dictionary<string, object> defaultParameter;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creating HTTP client
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="apiUrl"></param>
|
||||||
|
/// <param name="parameters"></param>
|
||||||
|
public Client(string apiUrl, Dictionary<string, object> parameters = null)
|
||||||
|
{
|
||||||
|
if (apiUrl.IndexOf("https://") == -1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("API schema requires HTTPS protocol");
|
||||||
|
}
|
||||||
|
|
||||||
|
url = apiUrl;
|
||||||
|
defaultParameter = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Make HTTP request
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
/// <param name="method"></param>
|
||||||
|
/// <param name="parameters"></param>
|
||||||
|
/// <param name="timeout"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ApiResponse makeRequest(string path, string method, Dictionary<string, object> parameters = null, int timeout = 30)
|
||||||
|
{
|
||||||
|
string[] allowedMethods = new string[] { METHOD_GET, METHOD_POST };
|
||||||
|
if (allowedMethods.Contains(method) == false)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Method \"" + method + "\" is not valid. Allowed methods are " + String.Join(", ", allowedMethods));
|
||||||
|
}
|
||||||
|
if (parameters == null) {
|
||||||
|
parameters = new Dictionary<string, object>();
|
||||||
|
}
|
||||||
|
parameters = defaultParameter.Union(parameters).ToDictionary(k => k.Key, v => v.Value);
|
||||||
|
path = url + path;
|
||||||
|
string httpQuery = Tools.httpBuildQuery(parameters);
|
||||||
|
|
||||||
|
if (method.Equals(METHOD_GET) && parameters.Count > 0)
|
||||||
|
{
|
||||||
|
path += "?" + httpQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception exception = null;
|
||||||
|
|
||||||
|
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(path);
|
||||||
|
request.Method = method;
|
||||||
|
|
||||||
|
if (method.Equals(METHOD_POST))
|
||||||
|
{
|
||||||
|
UTF8Encoding encoding = new UTF8Encoding();
|
||||||
|
byte[] bytes = encoding.GetBytes(httpQuery);
|
||||||
|
request.ContentLength = bytes.Length;
|
||||||
|
request.ContentType = CONTENT_TYPE;
|
||||||
|
request.UserAgent = USER_AGENT;
|
||||||
|
|
||||||
|
Stream post = request.GetRequestStream();
|
||||||
|
post.Write(bytes, 0, bytes.Length);
|
||||||
|
post.Flush();
|
||||||
|
post.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 WebException(exception.ToString(), exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamReader reader = new StreamReader((Stream) response.GetResponseStream());
|
||||||
|
string responseBody = reader.ReadToEnd();
|
||||||
|
int statusCode = (int) response.StatusCode;
|
||||||
|
|
||||||
|
return new ApiResponse(statusCode, responseBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,36 +0,0 @@
|
|||||||
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")]
|
|
94
RetailCrm/Response/ApiResponse.cs
Normal file
94
RetailCrm/Response/ApiResponse.cs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using RetailCrm.Exceptions;
|
||||||
|
using RetailCrm.Extra;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace RetailCrm.Response
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Response from retailCRM API
|
||||||
|
/// </summary>
|
||||||
|
public class ApiResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP response status code
|
||||||
|
/// </summary>
|
||||||
|
protected int statusCode;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response
|
||||||
|
/// </summary>
|
||||||
|
protected Dictionary<string, object> response;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creating ApiResponse
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="statusCode"></param>
|
||||||
|
/// <param name="responseBody"></param>
|
||||||
|
public ApiResponse(int statusCode, string responseBody = null)
|
||||||
|
{
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
|
||||||
|
if (responseBody != null && responseBody.Length > 0)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> response = new Dictionary<string, object>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
response = Tools.jsonDecode(responseBody);
|
||||||
|
}
|
||||||
|
catch (JsonReaderException e)
|
||||||
|
{
|
||||||
|
throw new InvalidJsonException("Invalid JSON in the API response body. " + e.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return HTTP response status code
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>int</returns>
|
||||||
|
public int getStatusCode()
|
||||||
|
{
|
||||||
|
return this.statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP request was successful
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>boolean</returns>
|
||||||
|
public bool isSuccessful()
|
||||||
|
{
|
||||||
|
return this.statusCode < 400;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return response
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Dictionary</returns>
|
||||||
|
public object this[string code]
|
||||||
|
{
|
||||||
|
get {
|
||||||
|
if (this.response.ContainsKey(code))
|
||||||
|
{
|
||||||
|
return this.response[code];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Property \"" + code + "\" is not writable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,708 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
<?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>
|
|
@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<packages>
|
|
||||||
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net45" />
|
|
||||||
</packages>
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user