modify versioned client

This commit is contained in:
Alex Lushpai 2017-09-27 18:18:42 +03:00
parent 9e61cbe979
commit c10e94053e
2 changed files with 56 additions and 10 deletions

View file

@ -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<string, object> { { "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 @@
}
);
}
/// <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;
}
}
}

View file

@ -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]