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;
///
/// Site code
///
protected string siteCode;
///
/// ApiClient creating
///
///
///
///
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() { { "apiKey", apiKey } });
siteCode = site;
}
///
/// Create a order
///
///
///
/// ApiResponse
public ApiResponse ordersCreate(Dictionary 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() {
{ "order", JsonConvert.SerializeObject(order) }
}
)
);
}
///
/// Edit a order
///
///
///
///
/// ApiResponse
public ApiResponse ordersEdit(Dictionary 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() {
{ "order", JsonConvert.SerializeObject(order) },
{ "by", by }
}
)
);
}
///
/// Upload array of the orders
///
///
///
/// ApiResponse
public ApiResponse ordersUpload(Dictionary 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() {
{ "orders", JsonConvert.SerializeObject(orders) }
}
)
);
}
///
/// Get order by id or externalId
///
///
///
///
/// ApiResponse
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() {
{ "by", by }
}
)
);
}
///
/// Returns a orders history
///
///
///
///
///
///
/// ApiResponse
public ApiResponse ordersHistory(
DateTime? startDate = null,
DateTime? endDate = null,
int limit = 100,
int offset = 0,
bool skipMyChanges = true
)
{
Dictionary parameters = new Dictionary();
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);
}
///
/// Returns filtered orders list
///
///
///
///
/// ApiResponse
public ApiResponse ordersList(Dictionary filter = null, int page = 0, int limit = 0)
{
Dictionary parameters = new Dictionary();
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);
}
///
/// Returns statuses of the orders
///
///
///
/// ApiResponse
public ApiResponse ordersStatuses(Dictionary ids = null, Dictionary externalIds = null)
{
Dictionary parameters = new Dictionary();
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);
}
///
/// Save order IDs' (id and externalId) association in the CRM
///
///
/// ApiResponse
public ApiResponse ordersFixExternalId(Dictionary 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() {
{ "orders", JsonConvert.SerializeObject(ids) }
}
);
}
///
/// Create a customer
///
///
///
/// ApiResponse
public ApiResponse customersCreate(Dictionary 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() {
{ "customer", JsonConvert.SerializeObject(customer) }
}
)
);
}
///
/// Edit a customer
///
///
///
///
/// ApiResponse
public ApiResponse customersEdit(Dictionary 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() {
{ "customer", JsonConvert.SerializeObject(customer) },
{ "by", by }
}
)
);
}
///
/// Upload array of the customers
///
///
///
/// ApiResponse
public ApiResponse customersUpload(Dictionary 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() {
{ "customers", JsonConvert.SerializeObject(customers) }
}
)
);
}
///
/// Get customer by id or externalId
///
///
///
///
/// ApiResponse
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() {
{ "by", by }
}
)
);
}
///
/// Returns filtered customers list
///
///
///
///
/// ApiResponse
public ApiResponse customersList(Dictionary filter = null, int page = 0, int limit = 0)
{
Dictionary parameters = new Dictionary();
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);
}
///
/// Save customer IDs' (id and externalId) association in the CRM
///
///
/// ApiResponse
public ApiResponse customersFixExternalIds(Dictionary 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() {
{ "customers", JsonConvert.SerializeObject(ids) }
}
);
}
///
/// Returns deliveryServices list
///
/// ApiResponse
public ApiResponse deliveryServicesList()
{
return client.makeRequest("/reference/delivery-services", Client.METHOD_GET);
}
///
/// Returns deliveryTypes list
///
/// ApiResponse
public ApiResponse deliveryTypesList()
{
return client.makeRequest("/reference/delivery-types", Client.METHOD_GET);
}
///
/// Returns orderMethods list
///
/// ApiResponse
public ApiResponse orderMethodsList()
{
return client.makeRequest("/reference/order-methods", Client.METHOD_GET);
}
///
/// Returns orderTypes list
///
/// ApiResponse
public ApiResponse orderTypesList()
{
return client.makeRequest("/reference/order-types", Client.METHOD_GET);
}
///
/// Returns paymentStatuses list
///
/// ApiResponse
public ApiResponse paymentStatusesList()
{
return client.makeRequest("/reference/payment-statuses", Client.METHOD_GET);
}
///
/// Returns paymentTypes list
///
/// ApiResponse
public ApiResponse paymentTypesList()
{
return client.makeRequest("/reference/payment-types", Client.METHOD_GET);
}
///
/// Returns productStatuses list
///
/// ApiResponse
public ApiResponse productStatusesList()
{
return client.makeRequest("/reference/product-statuses", Client.METHOD_GET);
}
///
/// Returns statusGroups list
///
/// ApiResponse
public ApiResponse statusGroupsList()
{
return client.makeRequest("/reference/status-groups", Client.METHOD_GET);
}
///
/// Returns statuses list
///
/// ApiResponse
public ApiResponse statusesList()
{
return client.makeRequest("/reference/statuses", Client.METHOD_GET);
}
///
/// Returns sites list
///
/// ApiResponse
public ApiResponse sitesList()
{
return client.makeRequest("/reference/sites", Client.METHOD_GET);
}
///
/// Edit deliveryService
///
///
/// ApiResponse
public ApiResponse deliveryServicesEdit(Dictionary 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() {
{ "deliveryService", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit deliveryType
///
///
/// ApiResponse
public ApiResponse deliveryTypesEdit(Dictionary 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() {
{ "deliveryType", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit orderMethod
///
///
/// ApiResponse
public ApiResponse orderMethodsEdit(Dictionary 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() {
{ "orderMethod", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit orderType
///
///
/// ApiResponse
public ApiResponse orderTypesEdit(Dictionary 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() {
{ "orderType", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit paymentStatus
///
///
/// ApiResponse
public ApiResponse paymentStatusesEdit(Dictionary 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() {
{ "paymentStatus", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit paymentType
///
///
/// ApiResponse
public ApiResponse paymentTypesEdit(Dictionary 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() {
{ "paymentType", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit productStatus
///
///
/// ApiResponse
public ApiResponse productStatusesEdit(Dictionary 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() {
{ "productStatus", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit order status
///
///
/// ApiResponse
public ApiResponse statusesEdit(Dictionary 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() {
{ "status", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Edit site
///
///
/// ApiResponse
public ApiResponse sitesEdit(Dictionary 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() {
{ "site", JsonConvert.SerializeObject(data) }
}
);
}
///
/// Update CRM basic statistic
///
/// ApiResponse
public ApiResponse statisticUpdate()
{
return client.makeRequest("/statistic/update", Client.METHOD_GET);
}
///
/// Return current site
///
/// string
public string getSite()
{
return this.siteCode;
}
///
/// Return current site
///
public void setSite(string site)
{
this.siteCode = site;
}
///
/// Check ID parameter
///
///
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));
}
}
///
/// Fill params by site value
///
///
///
/// Dictionary
protected Dictionary fillSite(string site, Dictionary param)
{
if (site.Length > 1)
{
param.Add("site", site);
}
else if (siteCode.Length > 1)
{
param.Add("site", siteCode);
}
return param;
}
}
}