I've created a simple API wrapper that I intend a few developers to utilize. I decided to follow an Fluent Interface methodology, similar to this.
public class SalesAPI
{
private HttpClient _client;
private string _url;
private List<string> CompanyNums;
private List<string> CustBillToCodes;
private List<string> CustShipToCodes;
private List<string> CustPONum;
private List<string> Warehouse;
private List<string> ProdNum;
private string StartDeliveryDate;
private string EndDeliveryDate;
private string StartShipDate;
private string EndShipDate;
private string StartPickDate;
private string EndPickDate;
private string Status;
private JObject _request;
public SalesAPI(string url)
{
_request = new JObject();
_client = new HttpClient();
_url = url;
_request["appId"] = "APISALES";
_request["command"] = "getSalesOrderDetails";
_request["username"] = HttpContext.Current.User.Identity.GetShortADName();
}
public SalesAPI SetProdNum(List<string> prodNums)
{
ProdNum = prodNums;
return this;
}
public SalesAPI SetProdNum(string prodNum)
{
ProdNum = new List<string>(new string[] { prodNum });
return this;
}
public SalesAPI SetCompany(List<string> companies)
{
CompanyNums = companies;
return this;
}
public SalesAPI SetCompany(string company)
{
CompanyNums = new List<string>(new string[] { company });
return this;
}
public SalesAPI SetBillToCustCode(List<string> custCodes)
{
CustBillToCodes = custCodes;
return this;
}
public SalesAPI SetBillToCustCode(string custCode)
{
CustBillToCodes = new List<string>(new string[] { custCode });
return this;
}
public SalesAPI SetCustPONum(List<string> custPONums)
{
CustPONum = custPONums;
return this;
}
public SalesAPI SetCustPONum(string custPONum)
{
CustPONum = new List<string>(new string[] { custPONum });
return this;
}
public SalesAPI SetWarehouse(List<string> warehouses)
{
Warehouse = warehouses;
return this;
}
public SalesAPI SetWarehouse(string warehouse)
{
Warehouse = new List<string>(new string[] { warehouse });
return this;
}
public SalesAPI SetShipToCustCode(List<string> custCodes)
{
CustShipToCodes = custCodes;
return this;
}
public SalesAPI SetShipToCustCode(string custCode)
{
CustShipToCodes = new List<string>() { custCode };
return this;
}
public SalesAPI SetDeliveryDateRange(DateTime start, DateTime end)
{
StartDeliveryDate = start.ToPPROFormattedDate();
EndDeliveryDate = end.ToPPROFormattedDate();
return this;
}
public SalesAPI SetShipDateRange(DateTime start, DateTime end)
{
StartShipDate = start.ToPPROFormattedDate();
EndShipDate = end.ToPPROFormattedDate();
return this;
}
public SalesAPI SetPickDateRange(DateTime start, DateTime end)
{
StartPickDate = start.ToPPROFormattedDate();
EndPickDate = end.ToPPROFormattedDate();
return this;
}
public SalesAPI SetStatus(string status)
{
Status = status;
return this;
}
private SalesAPI SetParameters()
{
JArray companyNumArray = new JArray(CompanyNums);
JArray custBillToArray = new JArray(CustBillToCodes);
JArray custShipToArray = new JArray(CustShipToCodes);
JArray custPOArray = new JArray(CustPONum);
JArray warehouseArray = new JArray(Warehouse);
JArray prodNumArray = new JArray(ProdNum);
if (CompanyNums != null) { _request["company"] = companyNumArray; }
if (CustBillToCodes != null) { _request["custPOBillToID"] = custBillToArray; }
if (CustShipToCodes != null) { _request["custShipToID"] = custShipToArray; }
if (CustPONum != null) { _request["custPONum"] = custPOArray; }
if (Warehouse != null) { _request["warehouse"] = warehouseArray; }
if (StartDeliveryDate != null) { _request["startDeliveryDate"] = StartDeliveryDate; }
if (EndDeliveryDate != null) { _request["endDeliveryDate"] = EndDeliveryDate; }
if (StartShipDate != null) { _request["startShipDate"] = StartShipDate; }
if (EndShipDate != null) { _request["endShipDate"] = EndShipDate; }
if (StartPickDate != null) { _request["startPickDate"] = StartPickDate; }
if (EndPickDate != null) { _request["endPickDate"] = EndPickDate; }
if (Status != null) { _request["status"] = Status; }
if (ProdNum != null) { _request["productNum"] = prodNumArray; }
return this;
}
public async Task<RootSalesOrderObject> GetSalesOrderDetailsAsync()
{
SetParameters();
var content = new StringContent(_request.ToString(Formatting.None), Encoding.UTF8, "application/json");
var response = await _client.PostAsync(_url, content);
var contents = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<RootSalesOrderObject>(contents);
}
}
Here is how the api is consumed:
var salesAPI = new SalesAPI("https://xxxxx.xxxxx.com/yyyyy/services");
var companies = new List<string>()
{
"0002",
"0007",
"0009"
};
return await salesAPI
.SetCompany(companies)
.SetDeliveryDateRange(DateTime.Now.AddDays(-3), DateTime.Now.AddDays(3))
.SetProdNum("17876")
.GetSalesOrderDetailsAsync();
Let me know what improvement you think could be made or if you think it is sufficient as is. Do you think this follows best practices and is relatively easy to understand for other developers who may need to use it?