From c10e94053edcc778219d0123f2f8145c8f32f22b Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 27 Sep 2017 18:18:42 +0300 Subject: [PATCH] modify versioned client --- Retailcrm/Versions/V3/Client.cs | 59 ++++++++++++++++++++++++++++++--- RetailcrmUnitTest/V3/Orders.cs | 7 +--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/Retailcrm/Versions/V3/Client.cs b/Retailcrm/Versions/V3/Client.cs index 7692a9e..34db69e 100644 --- a/Retailcrm/Versions/V3/Client.cs +++ b/Retailcrm/Versions/V3/Client.cs @@ -2,21 +2,22 @@ { using System; using System.Collections.Generic; + using System.Linq; using System.Web.Script.Serialization; - public class Client : AbstractClient + public class Client { private readonly Request _request; private string _siteCode; - public Client(string url, string key, string version, string site = "") + public Client(string url, string key, string site = "") { if ("/" != url.Substring(url.Length - 1, 1)) { url += "/"; } - url += "api/" + version; + url += "api/v3"; _request = new Request(url, new Dictionary { { "apiKey", key } }); _siteCode = site; @@ -74,7 +75,7 @@ public Response OrdersGet(string id, string by = "externalId", string site = "") { - AbstractClient.CheckIdParameter(by); + CheckIdParameter(by); return _request.MakeRequest( $"/orders/{id}", @@ -119,5 +120,55 @@ } ); } + + /// + /// Return current site + /// + /// string + public string GetSite() + { + return _siteCode; + } + + /// + /// Return current site + /// + public void SetSite(string site) + { + _siteCode = site; + } + + /// + /// Check ID parameter + /// + /// + 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)}"); + } + } + + /// + /// 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; + } } } diff --git a/RetailcrmUnitTest/V3/Orders.cs b/RetailcrmUnitTest/V3/Orders.cs index 8750897..6198c43 100644 --- a/RetailcrmUnitTest/V3/Orders.cs +++ b/RetailcrmUnitTest/V3/Orders.cs @@ -17,12 +17,7 @@ public Orders() { NameValueCollection appSettings = ConfigurationManager.AppSettings; - - _client = new Client( - appSettings["apiUrl"], - appSettings["apiKey"], - "v3" - ); + _client = new Client(appSettings["apiUrl"], appSettings["apiKey"]); } [TestMethod]