Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using Newtonsoft.Json;
- using Reservanto.PrivateApiClient.DataObjects;
- namespace Reservanto.PrivateApiClient
- {
- public class Client
- {
- private static class ApiUrls
- {
- // to bys chtěl, co? :DD
- }
- private readonly string username;
- private readonly string password;
- private readonly string baseUrl;
- /// <summary>
- /// Přijme base url API serveru a na tu se provádí všechna volání API.
- /// </summary>
- public Client(string baseUrl, string username, string password)
- {
- this.baseUrl = baseUrl;
- this.username = username;
- this.password = password;
- }
- /// <summary>
- /// Pošle get request na danou url. Vrácí obsah stránky jako string
- /// </summary>
- private string GetData(string url = "")
- {
- try
- {
- Uri uri = new Uri(this.baseUrl + url);
- WebRequest webRequest = WebRequest.Create(uri);
- //basic authentication
- string svcCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
- webRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
- WebResponse response = webRequest.GetResponse();
- StreamReader streamReader = new StreamReader(response.GetResponseStream());
- string json = streamReader.ReadToEnd();
- return json;
- }
- catch (Exception e)
- {
- //Debug.Fail(e.Message);
- return null;
- }
- }
- /// <summary>
- /// 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é.
- /// </summary>
- /// <typeparam name="T">DataObject</typeparam>
- /// <param name="url">Url jako string</param>
- /// <param name="year">Nahradí {year} v url</param>
- /// <param name="month">Nahradí {month} v url</param>
- /// <param name="day">Nahradí {day} v url</param>
- private T CallApi<T>(string url, int? year = null, int? month = null, int? day = null) where T : class
- {
- //nahrazení {year}, {month} a {day} vstupními parametry
- url = url.Replace("{year}", year.ToString());
- url = url.Replace("{month}", month.ToString());
- url = url.Replace("{day}", day.ToString());
- //maže přebytečné lomítky '/' na konci url
- while (url.Last().Equals('/'))
- {
- url = url.Remove(url.Length - 1, 1);
- }
- //přiřadí do json obsah stránky s danou url
- var json = GetData(url);
- //Pokud je to možné, tak převede json na object a ten vrátí. Pokud převedení selže, vrátí null.
- try
- {
- return JsonConvert.DeserializeObject<T>(json);
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// Metoda GetCustomerStats vrací object s celkovým počtem všech merchantů, momentálně aktivních merchantů,
- /// zákazníků a rezervací.
- /// </summary>
- public CustomerStatsApiDto GetCustomerStats()
- {
- var result = CallApi<CustomerStatsApiDto>(ApiUrls.customerStats);
- return result;
- }
- /// <summary>
- /// 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.
- /// Jednotlivé dny jsou uloženy v Dictionary jako objecty typu <see cref="SubscriptionDayApiDto"/>.
- /// </summary>
- public Dictionary<int, SubscriptionDayApiDto> GetSubscriptionDays(int year, int month)
- {
- var result = CallApi<Dictionary<int, SubscriptionDayApiDto>>(ApiUrls.subscriptionsDays, year, month);
- return result;
- }
- /// <summary>
- /// Podle parametrů year, month a day vrací celkový počet zaplacených částek a platících uživatelů v určitém dni.
- /// Den je uložen v objectu typu <see cref="SubscriptionDayApiDto"/>.
- /// </summary>
- public SubscriptionDayApiDto GetSubscriptionDay(int year, int month, int day)
- {
- var result = CallApi<SubscriptionDayApiDto>(ApiUrls.subscriptionsDay, year, month, day);
- return result;
- }
- /// <summary>
- /// 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.
- /// Jednotlivé měsíce jsou uloženy v Dictionary jako objecty typu <see cref="SubscriptionMonthApiDto"/>.
- /// </summary>
- public Dictionary<int, SubscriptionMonthApiDto> GetSubscriptionMonths(int year)
- {
- var result = CallApi<Dictionary<int, SubscriptionMonthApiDto>>(ApiUrls.subscriptionMonths, year);
- return result;
- }
- /// <summary>
- /// Podle parametru year vrací celkový počet registrací, aktivních a uzavřených účtů po jednotlivých měsících v roce.
- /// Jednotlivé měsíce jsou uloženy v Dictionary jako objecty typu <see cref="CountApiDto"/>.
- /// </summary>
- public Dictionary<int, CountApiDto> GetRegistrations(int year)
- {
- var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.registrations, year);
- return result;
- }
- /// <summary>
- /// Podle parametru year a month vrací celkový počet registrací po jednotlivých dnech v měsíci.
- /// Jednotlivé dny jsou uloženy v Dictionary jako objecty typu <see cref="CountApiDto"/>.
- /// </summary>
- public Dictionary<int, CountApiDto> GetRegistrations(int year, int month)
- {
- var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.registrations, year, month);
- return result;
- }
- /// <summary>
- /// Vrací počet ukončených účtů, podle zadaného roku (parametr year).
- /// </summary>
- /// <returns>Dictionary s klíčem reprezentujícím měsíc a hodnotou typu <see cref="ClosedAccountsModel"/></returns>
- public Dictionary<int, CountApiDto> GetClosedAccounts(int year)
- {
- var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.closedAccounts, year);
- return result;
- }
- /// <summary>
- /// Vrací počet ukončených účtů, podle zadaného roku (parametr year) a zadaného měsíce (parametr month).
- /// </summary>
- /// <returns>Dictionary s klíčem reprezentujícím den a hodnotou typu <see cref="ClosedAccountsModel"/></returns>
- public Dictionary<int, CountApiDto> GetClosedAccounts(int year, int month)
- {
- var result = CallApi<Dictionary<int, CountApiDto>>(ApiUrls.closedAccounts, year, month);
- return result;
- }
- /// <summary>
- /// 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.
- /// </summary>
- public List<ActiveMerchantsApiDto> GetActiveMerchants()
- {
- var result = CallApi<List<ActiveMerchantsApiDto>>(ApiUrls.activeMerchants);
- return result;
- }
- /// <summary>
- /// Vrací obraty jednotlivých segmentů za celou dobu případně pro zadaný rok.
- /// Obraty jednotlivých segmentů jsou uloženy v Dictionary jako object typu <see cref="IncomesApiDto"/> pod klíčem který určuje rok.
- /// </summary>
- public Dictionary<int, IncomesApiDto> GetIncomes(int? year = null)
- {
- var result = CallApi<Dictionary<int, IncomesApiDto>>(ApiUrls.incomes, year);
- return result;
- }
- /// <summary>
- /// Vrací List objectů typu <see cref="SegmentApiDto"/>.
- /// V objectu <see cref="SegmentApiDto"/> je uložen název segmentu a jeho Id.
- /// </summary>
- public List<SegmentApiDto> GetSegments()
- {
- var result = CallApi<List<SegmentApiDto>>(ApiUrls.segments);
- return result;
- }
- /// <summary>
- /// Vrací list objectů typu <see cref="MerchantApiDto"/>, ve kterém jsou uloženi odpadlíci.
- /// </summary>
- public List<MerchantApiDto> GetRenegades()
- {
- var result = CallApi<List<MerchantApiDto>>(ApiUrls.renegades);
- return result;
- }
- /// <summary>
- /// Vrací list objectů typu <see cref="MerchantApiDto"/>, ve kterém jsou uloženi uživatelé s největším obratem(VIP).
- /// </summary>
- public List<MerchantApiDto> GetVIPs()
- {
- var result = CallApi<List<MerchantApiDto>>(ApiUrls.vips);
- return result;
- }
- /// <summary>
- /// 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ů.
- /// </summary>
- public PendingPaymentsApiDto GetPendingPayment()
- {
- var now = DateTime.Now;
- var result = CallApi<PendingPaymentsApiDto>(ApiUrls.pendingPayement);
- return result;
- }
- /// <summary>
- /// Vrací true pokud je spojení s API serverem v pořádku
- /// </summary>
- public bool IsUp()
- {
- bool result = GetData(ApiUrls.echo) != null;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement