mirror of
https://github.com/retailcrm/api-client-dotnet.git
synced 2025-04-11 13:00:55 +00:00
init multiversion sdk
This commit is contained in:
parent
c7e7664f30
commit
9e61cbe979
20 changed files with 953 additions and 1409 deletions
63
.gitattributes
vendored
Normal file
63
.gitattributes
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
###############################################################################
|
||||
# Set default behavior to automatically normalize line endings.
|
||||
###############################################################################
|
||||
* text=auto
|
||||
|
||||
###############################################################################
|
||||
# Set default behavior for command prompt diff.
|
||||
#
|
||||
# This is need for earlier builds of msysgit that does not have it on by
|
||||
# default for csharp files.
|
||||
# Note: This is only used by command line
|
||||
###############################################################################
|
||||
#*.cs diff=csharp
|
||||
|
||||
###############################################################################
|
||||
# Set the merge driver for project and solution files
|
||||
#
|
||||
# Merging from the command prompt will add diff markers to the files if there
|
||||
# are conflicts (Merging from VS is not affected by the settings below, in VS
|
||||
# the diff markers are never inserted). Diff markers may cause the following
|
||||
# file extensions to fail to load in VS. An alternative would be to treat
|
||||
# these files as binary and thus will always conflict and require user
|
||||
# intervention with every merge. To do so, just uncomment the entries below
|
||||
###############################################################################
|
||||
#*.sln merge=binary
|
||||
#*.csproj merge=binary
|
||||
#*.vbproj merge=binary
|
||||
#*.vcxproj merge=binary
|
||||
#*.vcproj merge=binary
|
||||
#*.dbproj merge=binary
|
||||
#*.fsproj merge=binary
|
||||
#*.lsproj merge=binary
|
||||
#*.wixproj merge=binary
|
||||
#*.modelproj merge=binary
|
||||
#*.sqlproj merge=binary
|
||||
#*.wwaproj merge=binary
|
||||
|
||||
###############################################################################
|
||||
# behavior for image files
|
||||
#
|
||||
# image files are treated as binary by default.
|
||||
###############################################################################
|
||||
#*.jpg binary
|
||||
#*.png binary
|
||||
#*.gif binary
|
||||
|
||||
###############################################################################
|
||||
# diff behavior for common document formats
|
||||
#
|
||||
# Convert binary document formats to text before diffing them. This feature
|
||||
# is only available from the command line. Turn it on by uncommenting the
|
||||
# entries below.
|
||||
###############################################################################
|
||||
#*.doc diff=astextplain
|
||||
#*.DOC diff=astextplain
|
||||
#*.docx diff=astextplain
|
||||
#*.DOCX diff=astextplain
|
||||
#*.dot diff=astextplain
|
||||
#*.DOT diff=astextplain
|
||||
#*.pdf diff=astextplain
|
||||
#*.PDF diff=astextplain
|
||||
#*.rtf diff=astextplain
|
||||
#*.RTF diff=astextplain
|
106
.gitignore
vendored
106
.gitignore
vendored
|
@ -4,8 +4,14 @@
|
|||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
**/App.config
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
|
@ -13,19 +19,21 @@
|
|||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
build/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
|
||||
# Roslyn cache directories
|
||||
*.ide/
|
||||
# Visual Studio 2015 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
#NUNIT
|
||||
# NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
|
||||
|
@ -34,6 +42,11 @@ TestResult.xml
|
|||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# DNX
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
|
@ -66,14 +79,18 @@ _Chutzpah*
|
|||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
@ -86,7 +103,7 @@ _ReSharper*/
|
|||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# JustCode is a .NET coding addin-in
|
||||
# JustCode is a .NET coding add-in
|
||||
.JustCode
|
||||
|
||||
# TeamCity is a build add-in
|
||||
|
@ -98,6 +115,7 @@ _TeamCity*
|
|||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
|
@ -125,39 +143,63 @@ publish/
|
|||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
#*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/packages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/packages/build/
|
||||
# If using the old MSBuild-Integrated Package Restore, uncomment this:
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/packages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignoreable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Windows Azure Build Output
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
node_modules/
|
||||
orleans.codegen.cs
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
@ -181,3 +223,41 @@ UpgradeLog*.htm
|
|||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# JetBrains Rider
|
||||
.idea/
|
||||
*.sln.iml
|
||||
|
||||
# CodeRush
|
||||
.cr/
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,21 +0,0 @@
|
|||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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)
|
||||
{
|
||||
object valueObj = kvp.Value;
|
||||
string value = String.Empty;
|
||||
|
||||
value = valueObj.ToString();
|
||||
|
||||
if (value != "")
|
||||
{
|
||||
if (valueObj.GetType() == typeof(JObject))
|
||||
{
|
||||
valueObj = jsonObjectToDictionary((Dictionary<string, object>)JsonConvert.DeserializeObject<Dictionary<string, object>>(value));
|
||||
result.Add(kvp.Key.ToString(), valueObj);
|
||||
}
|
||||
else if (valueObj.GetType() == typeof(JArray))
|
||||
{
|
||||
var items = new List<object>();
|
||||
|
||||
dynamic dynamicObject = JsonConvert.DeserializeObject(value);
|
||||
Dictionary<string, object> newObject = new Dictionary<string, object>();
|
||||
|
||||
int j = 0;
|
||||
foreach (var item in dynamicObject)
|
||||
{
|
||||
newObject.Add(j.ToString(), jsonObjectToDictionary(item.ToObject<Dictionary<string, object>>()));
|
||||
j++;
|
||||
}
|
||||
|
||||
result.Add(kvp.Key.ToString(), newObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(kvp.Key.ToString(), valueObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
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,94 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
34
Retailcrm.sln
Normal file
34
Retailcrm.sln
Normal file
|
@ -0,0 +1,34 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26730.16
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Retailcrm", "Retailcrm\Retailcrm.csproj", "{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetailcrmUnitTest", "RetailcrmUnitTest\RetailcrmUnitTest.csproj", "{4069C2BC-C277-48A3-96AE-DA5934A75F4D}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41} = {9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4069C2BC-C277-48A3-96AE-DA5934A75F4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4069C2BC-C277-48A3-96AE-DA5934A75F4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4069C2BC-C277-48A3-96AE-DA5934A75F4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4069C2BC-C277-48A3-96AE-DA5934A75F4D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7EB29423-9A10-4953-B3E8-563908B2D206}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
35
Retailcrm/Properties/AssemblyInfo.cs
Normal file
35
Retailcrm/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Общие сведения об этой сборке предоставляются следующим набором
|
||||
// набора атрибутов. Измените значения этих атрибутов, чтобы изменить сведения,
|
||||
// связанные со сборкой.
|
||||
[assembly: AssemblyTitle("Retailcrm")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Retailcrm")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми
|
||||
// для компонентов COM. Если необходимо обратиться к типу в этой сборке через
|
||||
// COM, задайте атрибуту ComVisible значение TRUE для этого типа.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Следующий GUID служит для идентификации библиотеки типов, если этот проект будет видимым для COM
|
||||
[assembly: Guid("9c378ef4-9f9b-4214-a9aa-1fc3c44edb41")]
|
||||
|
||||
// Сведения о версии сборки состоят из следующих четырех значений:
|
||||
//
|
||||
// Основной номер версии
|
||||
// Дополнительный номер версии
|
||||
// Номер сборки
|
||||
// Редакция
|
||||
//
|
||||
// Можно задать все значения или принять номер сборки и номер редакции по умолчанию.
|
||||
// используя "*", как показано ниже:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
113
Retailcrm/QueryStringBuilder.cs
Normal file
113
Retailcrm/QueryStringBuilder.cs
Normal file
|
@ -0,0 +1,113 @@
|
|||
namespace Retailcrm
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class QueryStringBuilder
|
||||
{
|
||||
private readonly List<KeyValuePair<string, object>> _keyValuePairs
|
||||
= new List<KeyValuePair<string, object>>();
|
||||
|
||||
public static string BuildQueryString(object queryData, string argSeperator = "&")
|
||||
{
|
||||
var encoder = new QueryStringBuilder();
|
||||
encoder.AddEntry(null, queryData, allowObjects: true);
|
||||
|
||||
return encoder.GetUriString(argSeperator);
|
||||
}
|
||||
|
||||
private string GetUriString(string argSeperator)
|
||||
{
|
||||
return String.Join(argSeperator,
|
||||
_keyValuePairs.Select(kvp =>
|
||||
{
|
||||
var key = Uri.EscapeDataString(kvp.Key);
|
||||
var value = Uri.EscapeDataString(kvp.Value.ToString());
|
||||
return $"{key}={value}";
|
||||
}));
|
||||
}
|
||||
|
||||
private void AddEntry(string prefix, object instance, bool allowObjects)
|
||||
{
|
||||
var dictionary = instance as IDictionary;
|
||||
var collection = instance as ICollection;
|
||||
|
||||
if (dictionary != null)
|
||||
{
|
||||
Add(prefix, GetDictionaryAdapter(dictionary));
|
||||
}
|
||||
else if (collection != null)
|
||||
{
|
||||
Add(prefix, GetArrayAdapter(collection));
|
||||
}
|
||||
else if (allowObjects)
|
||||
{
|
||||
Add(prefix, GetObjectAdapter(instance));
|
||||
}
|
||||
else
|
||||
{
|
||||
_keyValuePairs.Add(new KeyValuePair<string, object>(prefix, instance));
|
||||
}
|
||||
}
|
||||
|
||||
private void Add(string prefix, IEnumerable<Entry> datas)
|
||||
{
|
||||
foreach (var item in datas)
|
||||
{
|
||||
var newPrefix = String.IsNullOrEmpty(prefix)
|
||||
? item.Key
|
||||
: $"{prefix}[{item.Key}]";
|
||||
|
||||
AddEntry(newPrefix, item.Value, allowObjects: false);
|
||||
}
|
||||
}
|
||||
|
||||
private struct Entry
|
||||
{
|
||||
public string Key;
|
||||
public object Value;
|
||||
}
|
||||
|
||||
private IEnumerable<Entry> GetObjectAdapter(object data)
|
||||
{
|
||||
var properties = data.GetType().GetProperties();
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
yield return new Entry()
|
||||
{
|
||||
Key = property.Name,
|
||||
Value = property.GetValue(data)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Entry> GetArrayAdapter(ICollection collection)
|
||||
{
|
||||
int i = 0;
|
||||
foreach (var item in collection)
|
||||
{
|
||||
yield return new Entry()
|
||||
{
|
||||
Key = i.ToString(),
|
||||
Value = item,
|
||||
};
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Entry> GetDictionaryAdapter(IDictionary collection)
|
||||
{
|
||||
foreach (DictionaryEntry item in collection)
|
||||
{
|
||||
yield return new Entry()
|
||||
{
|
||||
Key = item.Key.ToString(),
|
||||
Value = item.Value,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
99
Retailcrm/Request.cs
Normal file
99
Retailcrm/Request.cs
Normal file
|
@ -0,0 +1,99 @@
|
|||
namespace Retailcrm
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
public class Request
|
||||
{
|
||||
public const string MethodGet = "GET";
|
||||
public const string MethodPost = "POST";
|
||||
private const 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 const string ContentType = "application/x-www-form-urlencoded";
|
||||
|
||||
private readonly string _url;
|
||||
private readonly Dictionary<string, object> _defaultParameters;
|
||||
|
||||
public Request(string apiUrl, Dictionary<string, object> parameters = null)
|
||||
{
|
||||
if (apiUrl.IndexOf("https://", StringComparison.Ordinal) == -1)
|
||||
{
|
||||
throw new ArgumentException("API schema requires HTTPS protocol");
|
||||
}
|
||||
|
||||
_url = apiUrl;
|
||||
_defaultParameters = parameters;
|
||||
}
|
||||
|
||||
public Response MakeRequest(string path, string method, Dictionary<string, object> parameters = null)
|
||||
{
|
||||
string[] allowedMethods = { MethodGet, MethodPost };
|
||||
|
||||
if (allowedMethods.Contains(method) == false)
|
||||
{
|
||||
throw new ArgumentException($"Method {method} is not valid. Allowed HTTP methods are {string.Join(", ", allowedMethods)}");
|
||||
}
|
||||
|
||||
if (parameters == null)
|
||||
{
|
||||
parameters = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
parameters = _defaultParameters.Union(parameters).ToDictionary(k => k.Key, v => v.Value);
|
||||
path = _url + path;
|
||||
|
||||
string httpQuery = QueryStringBuilder.BuildQueryString(parameters);
|
||||
|
||||
if (method.Equals(MethodGet) && parameters.Count > 0)
|
||||
{
|
||||
path += "?" + httpQuery;
|
||||
}
|
||||
|
||||
Exception exception = null;
|
||||
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
|
||||
request.Method = method;
|
||||
|
||||
if (method.Equals(MethodPost))
|
||||
{
|
||||
UTF8Encoding encoding = new UTF8Encoding();
|
||||
byte[] bytes = encoding.GetBytes(httpQuery);
|
||||
request.ContentLength = bytes.Length;
|
||||
request.ContentType = ContentType;
|
||||
request.UserAgent = UserAgent;
|
||||
|
||||
Stream post = request.GetRequestStream();
|
||||
post.Write(bytes, 0, bytes.Length);
|
||||
post.Flush();
|
||||
post.Close();
|
||||
}
|
||||
|
||||
HttpWebResponse response;
|
||||
|
||||
try
|
||||
{
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
}
|
||||
catch (WebException webException)
|
||||
{
|
||||
response = (HttpWebResponse)webException.Response;
|
||||
exception = webException;
|
||||
}
|
||||
|
||||
if (request == null || response == null)
|
||||
{
|
||||
throw new WebException(exception.ToString(), exception);
|
||||
}
|
||||
|
||||
// ReSharper disable once AssignNullToNotNullAttribute
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
string responseBody = reader.ReadToEnd();
|
||||
int statusCode = (int)response.StatusCode;
|
||||
|
||||
return new Response(statusCode, responseBody);
|
||||
}
|
||||
}
|
||||
}
|
48
Retailcrm/Response.cs
Normal file
48
Retailcrm/Response.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
namespace Retailcrm
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Response
|
||||
{
|
||||
private readonly int _statusCode;
|
||||
private readonly string _rawResponse;
|
||||
private readonly Dictionary<string, object> _responseData;
|
||||
|
||||
public Response(int statusCode, string responseBody = null)
|
||||
{
|
||||
_statusCode = statusCode;
|
||||
|
||||
if (string.IsNullOrEmpty(responseBody))
|
||||
{
|
||||
throw new ArgumentException("Response body is empty");
|
||||
}
|
||||
|
||||
_rawResponse = responseBody;
|
||||
|
||||
var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
|
||||
_responseData = (Dictionary<string, object>)jsSerializer.DeserializeObject(responseBody);
|
||||
}
|
||||
|
||||
public int GetStatusCode()
|
||||
{
|
||||
return _statusCode;
|
||||
}
|
||||
|
||||
public Dictionary<string, object> GetResponse()
|
||||
{
|
||||
return _responseData;
|
||||
}
|
||||
|
||||
public string GetRawResponse()
|
||||
{
|
||||
return _rawResponse;
|
||||
}
|
||||
|
||||
public bool IsSuccessfull()
|
||||
{
|
||||
return _statusCode < 400;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
57
Retailcrm/Retailcrm.csproj
Normal file
57
Retailcrm/Retailcrm.csproj
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" 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>{9C378EF4-9F9B-4214-A9AA-1FC3C44EDB41}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Retailcrm</RootNamespace>
|
||||
<AssemblyName>Retailcrm</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>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="QueryStringBuilder.cs" />
|
||||
<Compile Include="Request.cs" />
|
||||
<Compile Include="Response.cs" />
|
||||
<Compile Include="Versions\AbstractClient.cs" />
|
||||
<Compile Include="Versions\V3\Client.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Versions\V4\" />
|
||||
<Folder Include="Versions\V5\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
61
Retailcrm/Versions/AbstractClient.cs
Normal file
61
Retailcrm/Versions/AbstractClient.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
namespace Retailcrm
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class AbstractClient
|
||||
{
|
||||
private string _siteCode;
|
||||
|
||||
/// <summary>
|
||||
/// Return current site
|
||||
/// </summary>
|
||||
/// <returns>string</returns>
|
||||
public string GetSite()
|
||||
{
|
||||
return _siteCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return current site
|
||||
/// </summary>
|
||||
public void SetSite(string site)
|
||||
{
|
||||
_siteCode = site;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check ID parameter
|
||||
/// </summary>
|
||||
/// <param name="by"></param>
|
||||
protected static void CheckIdParameter(string by)
|
||||
{
|
||||
string[] allowedForBy = { "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;
|
||||
}
|
||||
}
|
||||
}
|
123
Retailcrm/Versions/V3/Client.cs
Normal file
123
Retailcrm/Versions/V3/Client.cs
Normal file
|
@ -0,0 +1,123 @@
|
|||
namespace Retailcrm.Versions.V3
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
public class Client : AbstractClient
|
||||
{
|
||||
private readonly Request _request;
|
||||
private string _siteCode;
|
||||
|
||||
public Client(string url, string key, string version, string site = "")
|
||||
{
|
||||
if ("/" != url.Substring(url.Length - 1, 1))
|
||||
{
|
||||
url += "/";
|
||||
}
|
||||
|
||||
url += "api/" + version;
|
||||
|
||||
_request = new Request(url, new Dictionary<string, object> { { "apiKey", key } });
|
||||
_siteCode = site;
|
||||
}
|
||||
|
||||
public Response OrdersCreate(Dictionary<string, object> order, string site = "")
|
||||
{
|
||||
if (order.Count < 1)
|
||||
{
|
||||
throw new ArgumentException("Parameter `order` must contains a data");
|
||||
}
|
||||
|
||||
return _request.MakeRequest(
|
||||
"/orders/create",
|
||||
Request.MethodPost,
|
||||
FillSite(
|
||||
site,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "order", new JavaScriptSerializer().Serialize(order) }
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Response OrdersUpdate(Dictionary<string, object> order, string by = "externalId", string site = "")
|
||||
{
|
||||
if (order.Count < 1)
|
||||
{
|
||||
throw new ArgumentException("Parameter `order` must contains a data");
|
||||
}
|
||||
|
||||
if (!order.ContainsKey("id") && !order.ContainsKey("externalId"))
|
||||
{
|
||||
throw new ArgumentException("Parameter `order` must contains an id or externalId");
|
||||
}
|
||||
|
||||
CheckIdParameter(by);
|
||||
|
||||
string uid = by == "externalId" ? order["externalId"].ToString() : order["id"].ToString();
|
||||
|
||||
return _request.MakeRequest(
|
||||
$"/orders/{uid}/edit",
|
||||
Request.MethodPost,
|
||||
FillSite(
|
||||
site,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "by", by },
|
||||
{ "order", new JavaScriptSerializer().Serialize(order) }
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Response OrdersGet(string id, string by = "externalId", string site = "")
|
||||
{
|
||||
AbstractClient.CheckIdParameter(by);
|
||||
|
||||
return _request.MakeRequest(
|
||||
$"/orders/{id}",
|
||||
Request.MethodGet,
|
||||
FillSite(
|
||||
site,
|
||||
new Dictionary<string, object>() {
|
||||
{ "by", by }
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public Response OrdersList(Dictionary<string, object> filter = null, int page = 0, int limit = 0)
|
||||
{
|
||||
Dictionary<string, object> parameters = new Dictionary<string, object>();
|
||||
|
||||
if (filter != null && filter.Count > 0)
|
||||
{
|
||||
parameters.Add("filter", filter);
|
||||
}
|
||||
if (page > 0)
|
||||
{
|
||||
parameters.Add("page", page);
|
||||
}
|
||||
if (limit > 0)
|
||||
{
|
||||
parameters.Add("limit", limit);
|
||||
}
|
||||
|
||||
return _request.MakeRequest("/orders", Request.MethodGet, parameters);
|
||||
}
|
||||
|
||||
public Response OrdersFixExternalIds(Dictionary<string, object>[] ids)
|
||||
{
|
||||
return _request.MakeRequest(
|
||||
"/orders/fix-external-ids",
|
||||
Request.MethodPost,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "orders", new JavaScriptSerializer().Serialize(ids) }
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
7
RetailcrmUnitTest/App.config.dist
Normal file
7
RetailcrmUnitTest/App.config.dist
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="apiUrl" value="https://example.retailcrm.ru"/>
|
||||
<add key="apiKey" value="BBBBBBBBBBBBBBBBBBBBBBBBBBBB"/>
|
||||
</appSettings>
|
||||
</configuration>
|
19
RetailcrmUnitTest/Properties/AssemblyInfo.cs
Normal file
19
RetailcrmUnitTest/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("RetailcrmUnitTest")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("RetailcrmUnitTest")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
[assembly: Guid("4069c2bc-c277-48a3-96ae-da5934a75f4d")]
|
||||
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
76
RetailcrmUnitTest/RetailcrmUnitTest.csproj
Normal file
76
RetailcrmUnitTest/RetailcrmUnitTest.csproj
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{4069C2BC-C277-48A3-96AE-DA5934A75F4D}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>RetailcrmUnitTest</RootNamespace>
|
||||
<AssemblyName>RetailcrmUnitTest</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
|
||||
<IsCodedUITest>False</IsCodedUITest>
|
||||
<TestProjectType>UnitTest</TestProjectType>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</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>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Retailcrm">
|
||||
<HintPath>..\Retailcrm\bin\Debug\Retailcrm.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="V3\Orders.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="App.config.dist" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props'))" />
|
||||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets'))" />
|
||||
</Target>
|
||||
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" />
|
||||
</Project>
|
120
RetailcrmUnitTest/V3/Orders.cs
Normal file
120
RetailcrmUnitTest/V3/Orders.cs
Normal file
|
@ -0,0 +1,120 @@
|
|||
namespace RetailcrmUnitTest.V3
|
||||
{
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Configuration;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Retailcrm;
|
||||
using Retailcrm.Versions.V3;
|
||||
|
||||
[TestClass]
|
||||
public class Orders
|
||||
{
|
||||
private readonly Client _client;
|
||||
|
||||
public Orders()
|
||||
{
|
||||
NameValueCollection appSettings = ConfigurationManager.AppSettings;
|
||||
|
||||
_client = new Client(
|
||||
appSettings["apiUrl"],
|
||||
appSettings["apiKey"],
|
||||
"v3"
|
||||
);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrdersCreateReadUpdate()
|
||||
{
|
||||
Dictionary<string, object> order = new Dictionary<string, object>();
|
||||
|
||||
long epochTicks = new DateTime(1970, 1, 1).Ticks;
|
||||
long unixTime = ((DateTime.UtcNow.Ticks - epochTicks) / TimeSpan.TicksPerSecond);
|
||||
|
||||
order.Add("number", unixTime);
|
||||
order.Add("createdAt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
order.Add("lastName", "Doe");
|
||||
order.Add("firstName", "John");
|
||||
order.Add("email", "john@example.com");
|
||||
order.Add("phone", "+79999999999");
|
||||
|
||||
Debug.WriteLine("Running order create, get & update");
|
||||
|
||||
Response createResponse = _client.OrdersCreate(order);
|
||||
Assert.IsTrue(createResponse.IsSuccessfull());
|
||||
Assert.IsInstanceOfType(createResponse, typeof(Response));
|
||||
Assert.IsTrue(createResponse.GetResponse().ContainsKey("id"));
|
||||
|
||||
string id = createResponse.GetResponse()["id"].ToString();
|
||||
|
||||
Response getResponse = _client.OrdersGet(id, "id");
|
||||
Assert.IsTrue(getResponse.IsSuccessfull());
|
||||
Assert.IsInstanceOfType(getResponse, typeof(Response));
|
||||
Assert.IsTrue(getResponse.GetResponse().ContainsKey("order"));
|
||||
|
||||
Dictionary<string, object> update = new Dictionary<string, object>
|
||||
{
|
||||
{"id", id},
|
||||
{"status", "cancel-other"}
|
||||
};
|
||||
|
||||
Response updateResponse = _client.OrdersUpdate(update, "id");
|
||||
Assert.IsTrue(updateResponse.IsSuccessfull());
|
||||
Assert.IsInstanceOfType(updateResponse, typeof(Response));
|
||||
Assert.IsTrue(updateResponse.GetStatusCode() == 200);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrdersFixExternalId()
|
||||
{
|
||||
Dictionary<string, object> order = new Dictionary<string, object>();
|
||||
|
||||
long epochTicks = new DateTime(1970, 1, 1).Ticks;
|
||||
long unixTime = ((DateTime.UtcNow.Ticks - epochTicks) / TimeSpan.TicksPerSecond);
|
||||
|
||||
order.Add("number", unixTime);
|
||||
order.Add("createdAt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
order.Add("lastName", "Doe");
|
||||
order.Add("firstName", "John");
|
||||
order.Add("email", "john@example.com");
|
||||
order.Add("phone", "+79999999999");
|
||||
|
||||
Debug.WriteLine("Running orders fix external ids");
|
||||
Response createResponse = _client.OrdersCreate(order);
|
||||
|
||||
string id = createResponse.GetResponse()["id"].ToString();
|
||||
string externalId = $"{unixTime}ID";
|
||||
|
||||
Dictionary<string, object>[] fix =
|
||||
{
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{ "id", id },
|
||||
{ "externalId", externalId }
|
||||
}
|
||||
};
|
||||
|
||||
Assert.IsTrue(createResponse.IsSuccessfull());
|
||||
Assert.IsInstanceOfType(createResponse, typeof(Response));
|
||||
Assert.IsTrue(createResponse.GetResponse().ContainsKey("id"));
|
||||
|
||||
Response fixResponse = _client.OrdersFixExternalIds(fix);
|
||||
Assert.IsTrue(fixResponse.IsSuccessfull());
|
||||
Assert.IsInstanceOfType(fixResponse, typeof(Response));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OrdersList()
|
||||
{
|
||||
Debug.WriteLine("Running orders list");
|
||||
Response response = _client.OrdersList();
|
||||
|
||||
Assert.IsTrue(response.IsSuccessfull());
|
||||
Assert.IsTrue(response.GetStatusCode() == 200 || response.GetStatusCode() == 201);
|
||||
Assert.IsInstanceOfType(response, typeof(Response));
|
||||
Assert.IsTrue(response.GetResponse().ContainsKey("orders"));
|
||||
}
|
||||
}
|
||||
}
|
5
RetailcrmUnitTest/packages.config
Normal file
5
RetailcrmUnitTest/packages.config
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net45" />
|
||||
<package id="MSTest.TestFramework" version="1.1.11" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Add table
Reference in a new issue