Given a Tax type and city, access a REST API to get the total amount of taxes with the given type and at the given city. This information is to be grouped by users and ordered ascending by numerical id.
To access the log of taxes, I perform an HTTP GET request to https://api.doit.com/taxes/search?taxType={0}&page={1}
where the taxType
is either 'd' or 'c'and the page
is the page of the results, as an integer.
The response to a request is a JSON ith the following 5 fields:
public class PageResult
{
public string page { get; set; }
public int per_page { get; set; }
public int total { get; set; }
public int total_pages { get; set; }
public TaxData[] data { get; set; }
}
Each Tax Record has the following schema:
public class TaxData
{
public int id { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public long timestamp { get; set; }
public string txnType { get; set; }
public string amount { get; set; }
public City city { get; set; }
public string ip { get; set; }
}
The city object has the following scheme:
public class City
{
public int id { get; set; }
public string address { get; set; }
public string city { get; set; }
public int zipCode { get; set; }
}
I need to return a 2d array of integers with two columns, where the first column contains user ids of all users those pay taxes in the given city using the given tax type. The second column for a row corresponding to some user must contain the total amount that this user pays in dollars.
the following code that I want to review, especially the API consuming way.
class Program
{
private static readonly string apiUrl = "https://api.doit.com/taxes/search?taxType={0}&page={1}";
static void Main(string[] args)
{
var c = GetTaxesTotals(1, "d");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(c));
Console.ReadLine();
}
public static List<List<int>> GetTaxesTotals(int cityId, string taxType)
{
List<TaxData> taxes = GetTaxes(cityId, taxType);
var transactionGroupsPreUser = taxes.OrderBy(x => x.userId).GroupBy(x => x.userId);
var result = new List<List<int>>();
result = transactionGroupsPreUser.Select(g => new List<int> { g.Key,
(int)g.Sum(x => decimal.Parse(x.amount, NumberStyles.Currency)) }).ToList();
if (result.Count == 0)
{
result = new List<List<int>> { new List<int> { -1, -1 } };
}
return result;
}
private static PageResult GetDataPage(string taxType, int page)
{
if (!string.Equals(taxType, "c", StringComparison.OrdinalIgnoreCase) && !string.Equals(taxType, "d", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Unexpected tax type the allowed values: 'c', 'd'");
}
var uri = string.Format(apiUrl, taxType, page);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate;
var jsonResult = string.Empty;
using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
jsonResult = reader.ReadToEnd();
}
return Newtonsoft.Json.JsonConvert.DeserializeObject<PageResult>(jsonResult);
}
private static List<TaxData> GetTaxes(int city, string taxType)
{
PageResult page = null;
var taxes = new List<TaxData>();
var currentPageIndex = 0;
do
{
currentPageIndex++;
page = GetDataPage(taxType, currentPageIndex);
if (page?.data != null)
{
taxes.AddRange(page.data.Where(x => x.city.id == city));
}
} while (currentPageIndex < page.total_pages);
return taxes;
}
}