From 5bb667c1297ef2d6f02353971dc0f3ddf8a5daaf Mon Sep 17 00:00:00 2001 From: Daniel WOAGOU Date: Mon, 20 Apr 2020 22:21:59 +0000 Subject: [PATCH] IMplementation du Service Azure --- COVID.Android/Resources/Resource.designer.cs | 8 +-- COVID/COVID.csproj | 1 + COVID/Models/Details.cs | 3 +- COVID/Models/InfosCovid.cs | 8 +-- COVID/Services/AzureClient.cs | 31 +++++++++++ COVID/Services/Covid19TgService.cs | 58 +------------------- COVID/ViewModels/DetailViewModel.cs | 19 +++---- COVID/ViewModels/HomeViewModel.cs | 18 +++--- COVID/Views/DetailsTemplate.xaml | 2 +- 9 files changed, 57 insertions(+), 91 deletions(-) create mode 100644 COVID/Services/AzureClient.cs diff --git a/COVID.Android/Resources/Resource.designer.cs b/COVID.Android/Resources/Resource.designer.cs index 268e7ab..635153f 100644 --- a/COVID.Android/Resources/Resource.designer.cs +++ b/COVID.Android/Resources/Resource.designer.cs @@ -1,11 +1,11 @@ #pragma warning disable 1591 //------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// Ce code a été généré par un outil. +// Version du runtime :4.0.30319.42000 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si +// le code est régénéré. // //------------------------------------------------------------------------------ diff --git a/COVID/COVID.csproj b/COVID/COVID.csproj index 1d61020..86048df 100644 --- a/COVID/COVID.csproj +++ b/COVID/COVID.csproj @@ -33,6 +33,7 @@ + diff --git a/COVID/Models/Details.cs b/COVID/Models/Details.cs index 2016c6e..beffb12 100644 --- a/COVID/Models/Details.cs +++ b/COVID/Models/Details.cs @@ -7,9 +7,10 @@ namespace COVID.Models { public class Details { + public Stats Stat { get; set; } public string Date { get; set; } - public string Histoire { get; set; } + public string History { get; set; } public string ActiveCases { get{ return Stat.ActiveCases.ToString();} } public string Cured { get{ return Stat.Cured.ToString();} } diff --git a/COVID/Models/InfosCovid.cs b/COVID/Models/InfosCovid.cs index 0121823..5cd0a1b 100644 --- a/COVID/Models/InfosCovid.cs +++ b/COVID/Models/InfosCovid.cs @@ -6,12 +6,8 @@ namespace COVID.Models { public class InfosCovid - { - public Stats InfosduJour { get { return Details.FirstOrDefault().Stat; } } + { public List
Details { get; set; } - public InfosCovid(List
infos) - { - Details = infos; - } + } } diff --git a/COVID/Services/AzureClient.cs b/COVID/Services/AzureClient.cs new file mode 100644 index 0000000..ae61b28 --- /dev/null +++ b/COVID/Services/AzureClient.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using COVID.Models; +using Newtonsoft.Json; + +namespace COVID.Services +{ + public static class AzureClient + { + private static string Url { get; } = "https://covidapi20200420180335.azurewebsites.net/covid19tg"; + public static async Task RefreshDataAsync() + { + var uri = new Uri(Url); + HttpClient myClient = new HttpClient(); + + var response = await myClient.GetAsync(uri); + if (response.IsSuccessStatusCode) + { + var content = await response.Content.ReadAsStringAsync(); + + var returnValue = new InfosCovid(); + returnValue.Details=JsonConvert.DeserializeObject>(content); + return returnValue; + } + return null; + } + } +} diff --git a/COVID/Services/Covid19TgService.cs b/COVID/Services/Covid19TgService.cs index 018d555..ea0a6d5 100644 --- a/COVID/Services/Covid19TgService.cs +++ b/COVID/Services/Covid19TgService.cs @@ -12,68 +12,14 @@ namespace COVID.Services { public static class Covid19TgService { - public static async Task GetAsync() - { - // Load default configuration - var config = Configuration.Default.WithDefaultLoader(); - // Create a new browsing context - var context = BrowsingContext.New(config); - // This is where the HTTP request happens, returns that // we can query later - try - { - var document = await context.OpenAsync("https://covid19.gouv.tg/"); - string tmp = document.QuerySelector("#active-cases>div>h2").InnerHtml; - var stat = new Stats(); - stat.ActiveCases = document.ReadInteger("#active-cases>div>h2"); - stat.Cured = document.ReadInteger("#cured>div>h2"); - stat.Deaths = document.ReadInteger("#deceased>div>h2"); - return stat; - }catch(Exception e) - { - var t = e.Message; - return new Stats(); - } - } + public static InfosCovid InfosCovid{get;set;} + public static Details InfoduJour {get{ return InfosCovid.Details.FirstOrDefault();}} public static void AppelNumeroVert() { PhoneDialer.Open(Covid19TgService.NumeroVert); } public static string NumeroVert{get;set;}="111"; - public static InfosCovid InfosCovid{get;set;} - public static async Task GetDetailsAsync() - { - var config = Configuration.Default.WithDefaultLoader(); - var context = BrowsingContext.New(config); - var document = await context.OpenAsync("http://covid19.gouv.tg/situation-au-togo/"); - var details = new List
(); - var sections = document.QuerySelectorAll(".ee-loop__item>article>div>div>div"); - string xt = string.Empty; - - foreach (var item in sections.Skip(1)) - { - var itemDetails = new Details(); - xt += "\n\n"; - var itemsections = item.QuerySelectorAll("section"); - var itemHtmlDetails = itemsections.FirstOrDefault().QuerySelectorAll("h2"); - itemDetails.Date = $"{itemHtmlDetails[0].InnerHtml} à { itemHtmlDetails[1].InnerHtml}"; - - int i = 0; - Stats itemStats = new Stats(); - itemStats.ActiveCases = itemHtmlDetails[3].InnerHtml.GetInt(); - itemStats.Cured = itemHtmlDetails[4].InnerHtml.GetInt(); - itemStats.Deaths = itemHtmlDetails[5].InnerHtml.GetInt(); - itemDetails.Stat = itemStats; - foreach (var history in itemsections[1].QuerySelectorAll("p")) - { - itemDetails.Histoire += $"\n{history.InnerHtml}"; - } - details.Add(itemDetails); - - } - - return new InfosCovid(details); - } } } diff --git a/COVID/ViewModels/DetailViewModel.cs b/COVID/ViewModels/DetailViewModel.cs index 59d0b8c..ee36f1f 100644 --- a/COVID/ViewModels/DetailViewModel.cs +++ b/COVID/ViewModels/DetailViewModel.cs @@ -23,7 +23,6 @@ public List
LeDetails set { SetProperty(ref _details, value); } } - public Stats Stat { get { return _stat; } @@ -32,8 +31,6 @@ public Stats Stat } private Stats _stat; - - private string _date; public string Date { @@ -42,15 +39,13 @@ public string Date set { SetProperty(ref _date, value); } } - - private string _histoire; - public string Histoire + private string _history; + public string History { - get { return _histoire; } + get { return _history; } - set { SetProperty(ref _histoire, value); } + set { SetProperty(ref _history, value); } } - public string ActiveCases { get { return Stat.ActiveCases.ToString(); } } public string Cured { get { return Stat.Cured.ToString(); } } public string Deaths { get { return Stat.Deaths.ToString(); } } @@ -58,17 +53,17 @@ public string Histoire public DetailViewModel() { - _ = GetDetailsAsync(); + _= GetDetailsAsync(); Appel = new Command(() => Appeler()); } private async Task GetDetailsAsync() { - Covid19TgService.InfosCovid= await Covid19TgService.GetDetailsAsync(); + Covid19TgService.InfosCovid= await AzureClient.RefreshDataAsync(); LeDetails = Covid19TgService.InfosCovid.Details; } - + private void Appeler() { Covid19TgService.AppelNumeroVert(); diff --git a/COVID/ViewModels/HomeViewModel.cs b/COVID/ViewModels/HomeViewModel.cs index 26dc5b6..b7e79df 100644 --- a/COVID/ViewModels/HomeViewModel.cs +++ b/COVID/ViewModels/HomeViewModel.cs @@ -71,17 +71,13 @@ public HomeViewModel() async Task GetDetails() { - - //var stat = await Covid19TgService.GetAsync(); - - Covid19TgService.InfosCovid= await Covid19TgService.GetDetailsAsync(); - - CasActifs = Covid19TgService.InfosCovid.InfosduJour.ActiveCases.ToString(); - CasGueris = Covid19TgService.InfosCovid.InfosduJour.Cured.ToString(); - Deces = Covid19TgService.InfosCovid.InfosduJour.Deaths.ToString(); - CasConfirmes = Covid19TgService.InfosCovid.InfosduJour.Total.ToString(); - DateUpdate = Covid19TgService.InfosCovid.Details.FirstOrDefault().Date; - + Covid19TgService.InfosCovid= await AzureClient.RefreshDataAsync(); + var info = Covid19TgService.InfoduJour; + CasActifs = info.ActiveCases.ToString(); + CasGueris = info.Cured.ToString(); + Deces = info.Deaths.ToString(); + CasConfirmes = info.Total.ToString(); + DateUpdate = info.Date; } private void Appeler() diff --git a/COVID/Views/DetailsTemplate.xaml b/COVID/Views/DetailsTemplate.xaml index df23217..a57dd1c 100644 --- a/COVID/Views/DetailsTemplate.xaml +++ b/COVID/Views/DetailsTemplate.xaml @@ -140,7 +140,7 @@ HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.ColumnSpan="4" - Text="{Binding Histoire }" + Text="{Binding History }" FontFamily="{StaticResource MontserratMediumFont}" FontSize="16" Padding="10,30,10,10"