Commit 8add35b7 authored by Kirill's avatar Kirill

Подключен декодер Json

parents 6c89ad6c 9bb6cf19
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.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Ninject;
using TourDataManager.Entities;
namespace TourDataManager {
public class GetEstatesUseCase {
[Inject]
public HttpClient httpClient{ get; set; }
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");
// throw HttpRequestException
var content = await response.Content.ReadAsStringAsync();
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 {
...@@ -22,6 +25,10 @@ namespace TourDataManager { ...@@ -22,6 +25,10 @@ namespace TourDataManager {
cookiestor.Save(); cookiestor.Save();
}); });
} }
public Task<Estate[]> GetEstates(){
return Container.Get<GetEstatesUseCase>().GetEstates();
}
} }
public class MyModule : NinjectModule { public class MyModule : NinjectModule {
...@@ -44,6 +51,7 @@ namespace TourDataManager { ...@@ -44,6 +51,7 @@ namespace TourDataManager {
Bind<HttpClientHandler>().ToConstant(httpClientHandler).InSingletonScope(); Bind<HttpClientHandler>().ToConstant(httpClientHandler).InSingletonScope();
Bind<HttpClient>().ToConstant(httpClient).InSingletonScope(); Bind<HttpClient>().ToConstant(httpClient).InSingletonScope();
Bind<IAuthenticator>().To<Authenticator>().InSingletonScope(); Bind<IAuthenticator>().To<Authenticator>().InSingletonScope();
Bind<GetEstatesUseCase>().ToSelf().InSingletonScope();
} }
} }
......
...@@ -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>
...@@ -105,6 +109,7 @@ ...@@ -105,6 +109,7 @@
<Compile Include="Debug.cs" /> <Compile Include="Debug.cs" />
<Compile Include="Entities\Estate.cs" /> <Compile Include="Entities\Estate.cs" />
<Compile Include="Entities\Tour.cs" /> <Compile Include="Entities\Tour.cs" />
<Compile Include="GetEstatesUseCase.cs" />
<Compile Include="IAuthenticator.cs" /> <Compile Include="IAuthenticator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TourDataManager.cs" /> <Compile Include="TourDataManager.cs" />
......
<?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" />
......
...@@ -11,12 +11,18 @@ namespace TourDataManagerConsoleApplication { ...@@ -11,12 +11,18 @@ 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().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