Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using Newtonsoft.Json;
  9. using Reservanto.PrivateApiClient.DataObjects;
  10.  
  11. namespace Reservanto.PrivateApiClient
  12. {
  13.     public class Client
  14.     {
  15.         private static class ApiUrls
  16.         {
  17.             // to bys chtěl, co? :DD
  18.         }
  19.  
  20.         private readonly string username;
  21.         private readonly string password;
  22.         private readonly string baseUrl;
  23.  
  24.         /// <summary>
  25.         /// Přijme base url API serveru a na tu se provádí všechna volání API.
  26.         /// </summary>
  27.         public Client(string baseUrl, string username, string password)
  28.         {
  29.             this.baseUrl = baseUrl;
  30.             this.username = username;
  31.             this.password = password;
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Pošle get request na danou url. Vrácí obsah stránky jako string
  36.         /// </summary>
  37.         private string GetData(string url = "")
  38.         {
  39.             try
  40.             {
  41.                 Uri uri = new Uri(this.baseUrl + url);
  42.                 WebRequest webRequest = WebRequest.Create(uri);
  43.  
  44.                 //basic authentication
  45.                 string svcCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
  46.                 webRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
  47.  
  48.                 WebResponse response = webRequest.GetResponse();
  49.                 StreamReader streamReader = new StreamReader(response.GetResponseStream());
  50.                 string json = streamReader.ReadToEnd();
  51.  
  52.                 return json;
  53.             }
  54.             catch (Exception e)
  55.             {
  56.                 //Debug.Fail(e.Message);
  57.                 return null;
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Přijímá url jako string. Převádí obsah stránky z JSONu na object z DataObjects a ten vrací, pokud je to možné.
  63.         /// </summary>
  64.         /// <typeparam name="T">DataObject</typeparam>
  65.         /// <param name="url">Url jako string</param>
  66.         /// <param name="year">Nahradí {year} v url</param>
  67.         /// <param name="month">Nahradí {month} v url</param>
  68.         /// <param name="day">Nahradí {day} v url</param>
  69.         private T CallApi<T>(string url, int? year = null, int? month = null, int? day = null) where T : class
  70.         {
  71.             //nahrazení {year}, {month} a {day} vstupními parametry
  72.             url = url.Replace("{year}", year.ToString());
  73.             url = url.Replace("{month}", month.ToString());
  74.             url = url.Replace("{day}", day.ToString());
  75.  
  76.  
  77.             //maže přebytečné lomítky '/' na konci url
  78.             while (url.Last().Equals('/'))
  79.             {
  80.                 url = url.Remove(url.Length - 1, 1);
  81.             }
  82.  
  83.             //přiřadí do json obsah stránky s danou url
  84.             var json = GetData(url);
  85.  
  86.             //Pokud je to možné, tak převede json na object a ten vrátí. Pokud převedení selže, vrátí null.
  87.             try
  88.             {
  89.                 return JsonConvert.DeserializeObject<T>(json);
  90.             }
  91.             catch
  92.             {
  93.                 return null;
  94.             }
  95.  
  96.         }
  97.  
  98.         /// <summary>
  99.         /// Metoda GetCustomerStats vrací object s celkovým počtem všech merchantů, momentálně aktivních merchantů,
  100.         /// zákazníků a rezervací.
  101.         /// </summary>
  102.         public CustomerStatsApiDto GetCustomerStats()
  103.         {
  104.             var result = CallApi<CustomerStatsApiDto>(ApiUrls.customerStats);
  105.  
  106.             return result;
  107.         }
  108.  
  109.         /// <summary>
  110.         /// Podle parametrů year a month vrací celkový počet zaplacených částek a platících uživatelů po jednotlivých dnech v měsících.
  111.         /// Jednotlivé dny jsou uloženy v Dictionary jako objecty typu <see cref="SubscriptionDayApiDto"/>.
  112.         /// </summary>
  113.         public Dictionary<int, SubscriptionDayApiDto> GetSubscriptionDays(int year, int month)
  114.         {
  115.             var result = CallApi<Dictionary<int, SubscriptionDayApiDto>>(ApiUrls.subscriptionsDays, year, month);
  116.  
  117.             return result;
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Podle parametrů year, month a day vrací celkový počet zaplacených částek a platících uživatelů v určitém dni.
  122.         /// Den je uložen v objectu typu <see cref="SubscriptionDayApiDto"/>.
  123.         /// </summary>
  124.         public SubscriptionDayApiDto GetSubscriptionDay(int year, int month, int day)
  125.         {
  126.             var result = CallApi<SubscriptionDayApiDto>(ApiUrls.subscriptionsDay, year, month, day);
  127.  
  128.             return result;
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Podle parametru year vrací celkový počet zaplacených částek a platících uživatelů po jednotlivých měsících v roce.
  133.         /// Jednotlivé měsíce jsou uloženy v Dictionary jako objecty typu <see cref="SubscriptionMonthApiDto"/>.
  134.         /// </summary>
  135.         public Dictionary<int, SubscriptionMonthApiDto> GetSubscriptionMonths(int year)
  136.         {
  137.             var result = CallApi<Dictionary<int, SubscriptionMonthApiDto>>(ApiUrls.subscriptionMonths, year);
  138.  
  139.             return result;
  140.         }
  141.  
  142.         /// <summary>
  143.         /// Podle parametru year vrací celkový počet registrací, aktivních a uzavřených účtů po jednotlivých měsících v roce.
  144.         /// Jednotlivé měsíce jsou uloženy v Dictionary jako objecty typu <see cref="CountApiDto"/>.
  145.         /// </summary>
  146.         public Dictionary<int, CountApiDto> GetRegistrations(int year)
  147.         {
  148.             var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.registrations, year);
  149.  
  150.             return result;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Podle parametru year a month vrací celkový počet registrací po jednotlivých dnech v měsíci.
  155.         /// Jednotlivé dny jsou uloženy v Dictionary jako objecty typu <see cref="CountApiDto"/>.
  156.         /// </summary>
  157.         public Dictionary<int, CountApiDto> GetRegistrations(int year, int month)
  158.         {
  159.             var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.registrations, year, month);
  160.  
  161.             return result;
  162.         }
  163.  
  164.         /// <summary>
  165.         /// Vrací počet ukončených účtů, podle zadaného roku (parametr year).
  166.         /// </summary>
  167.         /// <returns>Dictionary s klíčem reprezentujícím měsíc a hodnotou typu <see cref="ClosedAccountsModel"/></returns>
  168.         public Dictionary<int, CountApiDto> GetClosedAccounts(int year)
  169.         {
  170.             var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.closedAccounts, year);
  171.  
  172.             return result;
  173.         }
  174.  
  175.         /// <summary>
  176.         /// Vrací počet ukončených účtů, podle zadaného roku (parametr year) a zadaného měsíce (parametr month).
  177.         /// </summary>
  178.         /// <returns>Dictionary s klíčem reprezentujícím den a hodnotou typu <see cref="ClosedAccountsModel"/></returns>
  179.         public Dictionary<int, CountApiDto> GetClosedAccounts(int year, int month)
  180.         {
  181.             var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.closedAccounts, year, month);
  182.  
  183.             return result;
  184.         }
  185.  
  186.         /// <summary>
  187.         /// Vrací list objectů typu <see cref="ActiveMerchantsApiDto"/> ve kterém jsou uloženy počty momentálně aktivních uživatelů podle jednotlivých variant.
  188.         /// </summary>
  189.         public List<ActiveMerchantsApiDto> GetActiveMerchants()
  190.         {
  191.             var result = CallApi<List<ActiveMerchantsApiDto>>(ApiUrls.activeMerchants);
  192.  
  193.             return result;
  194.         }
  195.  
  196.         /// <summary>
  197.         /// Vrací obraty jednotlivých segmentů za celou dobu případně pro zadaný rok.
  198.         /// Obraty jednotlivých segmentů jsou uloženy v Dictionary jako object typu <see cref="IncomesApiDto"/> pod klíčem který určuje rok.
  199.         /// </summary>
  200.         public Dictionary<int, IncomesApiDto> GetIncomes(int? year = null)
  201.         {
  202.             var result = CallApi<Dictionary<int, IncomesApiDto>>(ApiUrls.incomes, year);
  203.  
  204.             return result;
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Vrací List objectů typu <see cref="SegmentApiDto"/>.
  209.         /// V objectu <see cref="SegmentApiDto"/> je uložen název segmentu a jeho Id.
  210.         /// </summary>
  211.         public List<SegmentApiDto> GetSegments()
  212.         {
  213.             var result = CallApi<List<SegmentApiDto>>(ApiUrls.segments);
  214.  
  215.             return result;
  216.         }
  217.  
  218.         /// <summary>
  219.         /// Vrací list objectů typu <see cref="MerchantApiDto"/>, ve kterém jsou uloženi odpadlíci.
  220.         /// </summary>
  221.         public List<MerchantApiDto> GetRenegades()
  222.         {
  223.             var result = CallApi<List<MerchantApiDto>>(ApiUrls.renegades);
  224.  
  225.             return result;
  226.         }
  227.  
  228.         /// <summary>
  229.         /// Vrací list objectů typu <see cref="MerchantApiDto"/>, ve kterém jsou uloženi uživatelé s největším obratem(VIP).
  230.         /// </summary>
  231.         public List<MerchantApiDto> GetVIPs()
  232.         {
  233.             var result = CallApi<List<MerchantApiDto>>(ApiUrls.vips);
  234.  
  235.             return result;
  236.         }
  237.  
  238.         /// <summary>
  239.         /// Vrací object typu <see cref="PendingPaymentsApiDto"/>, ve kterém je uložen součet částek z neuhrazených výzev se splatností v tomto měsíci k platbě od aktivních merchantů.
  240.         /// </summary>
  241.         public PendingPaymentsApiDto GetPendingPayment()
  242.         {
  243.             var now = DateTime.Now;
  244.             var result = CallApi<PendingPaymentsApiDto>(ApiUrls.pendingPayement);
  245.  
  246.             return result;
  247.         }
  248.  
  249.         /// <summary>
  250.         /// Vrací true pokud je spojení s API serverem v pořádku
  251.         /// </summary>
  252.         public bool IsUp()
  253.         {
  254.             bool result = GetData(ApiUrls.echo) != null;
  255.  
  256.             return result;
  257.         }
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement