Commit 9bb6cf19 authored by Kirill's avatar Kirill

Отрабатываются ошибки при получении estates

parent fbf2e42b
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace TourDataManager.Entities { namespace TourDataManager.Entities {
public class Estate { public class Estate {
...@@ -7,7 +10,8 @@ namespace TourDataManager.Entities { ...@@ -7,7 +10,8 @@ namespace TourDataManager.Entities {
public string Title{ get; set; } public string Title{ get; set; }
public int TourCount{ get; set; } //cnt_tours [JsonProperty("cnt_tours")]
public int TourCount{ get; set; }
public string Preview{ get; set; } public string Preview{ get; set; }
......
using System;
using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Ninject; using Ninject;
using TourDataManager.Entities;
namespace TourDataManager { namespace TourDataManager {
public class GetEstatesUseCase { public class GetEstatesUseCase {
...@@ -7,10 +14,55 @@ namespace TourDataManager { ...@@ -7,10 +14,55 @@ namespace TourDataManager {
[Inject] [Inject]
public HttpClient httpClient{ get; set; } public HttpClient httpClient{ get; set; }
public async void GetEstates(){
public struct BigantoErrorList {
public List<BigantoError> Errors;
}
public struct BigantoError {
public int Code;
public string Message;
public override string ToString(){ return $"code={Code};message={Message}"; }
}
public class BigantoErrorException : Exception {
public BigantoErrorList ErrorsList;
public BigantoErrorException(BigantoErrorList errors){
ErrorsList = errors;
}
}
/// <summary>
/// <exception cref="HttpRequestException">Если сервер даст дубу</exception>
/// <exception cref="BigantoErrorException">Если вместо массива эстейтов с сервера получены ошибки</exception>
/// <exception cref="JsonException">Если вместо массива эстейтов или ошибки пришло нечто иное</exception>
/// </summary>
public async Task<Estate[]> GetEstates(){
var response = await httpClient.GetAsync("https://biganto.com/api-novus/estates.getList?client=desktopplayer&client_version=3.0&v=2.0"); var response = await httpClient.GetAsync("https://biganto.com/api-novus/estates.getList?client=desktopplayer&client_version=3.0&v=2.0");
// throw HttpRequestException
var content = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsStringAsync();
Debug.Log(content);
ErrorContext error = null;
var errorHandlerSettings = new JsonSerializerSettings{
Error = (sender,args) => {
error = args.ErrorContext;
args.ErrorContext.Handled = true;
}
};
var estates = JsonConvert.DeserializeObject<Estate[]>(content,errorHandlerSettings);
if (error == null) return estates;
error = null;
// Вместо массиво эстейтов в ответе может содержаться json ошибки
// Предпринимается попытка десериализовать ошибки и выкинуть BigantoErrorException
var exceptionList = JsonConvert.DeserializeObject<BigantoErrorList>(content,errorHandlerSettings);
if (error == null) throw new BigantoErrorException(exceptionList);
throw new JsonException("Something goes wrong",error.Error);
} }
} }
} }
\ No newline at end of file
using System;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Ninject; using Ninject;
using Ninject.Modules; using Ninject.Modules;
using TourDataManager.Entities;
namespace TourDataManager { namespace TourDataManager {
...@@ -23,8 +26,8 @@ namespace TourDataManager { ...@@ -23,8 +26,8 @@ namespace TourDataManager {
}); });
} }
public void GetEstates(){ public Task<Estate[]> GetEstates(){
Container.Get<GetEstatesUseCase>().GetEstates(); return Container.Get<GetEstatesUseCase>().GetEstates();
} }
} }
......
...@@ -38,6 +38,10 @@ ...@@ -38,6 +38,10 @@
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="mscorlib" /> <Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7"> <Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7">
<HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath> <HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath>
<Private>True</Private> <Private>True</Private>
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net45" userInstalled="true" /> <package id="EntityFramework" version="6.2.0" targetFramework="net45" userInstalled="true" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net472" />
<package id="Ninject" version="3.3.4" targetFramework="net472" /> <package id="Ninject" version="3.3.4" targetFramework="net472" />
<package id="SQLite.CodeFirst" version="1.5.1.25" targetFramework="net472" /> <package id="SQLite.CodeFirst" version="1.5.1.25" targetFramework="net472" />
<package id="System.Data.SqlClient" version="4.5.1" targetFramework="net472" /> <package id="System.Data.SqlClient" version="4.5.1" targetFramework="net472" />
......
...@@ -12,13 +12,17 @@ namespace TourDataManagerConsoleApplication { ...@@ -12,13 +12,17 @@ namespace TourDataManagerConsoleApplication {
public static void Main(string[] args){ public static void Main(string[] args){
var tourDataManager = new TourDataManager.TourDataManager(PersistentPath); var tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
//tourDataManager.Login(defaultLogin, defaultPassword); tourDataManager.Login(defaultLogin, defaultPassword);
//new Db(); //new Db();
tourDataManager.GetEstates(); tourDataManager.GetEstates().ContinueWith(task => {
var est = task.Result;
});
Console.Read(); Console.Read();
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment