Реализован вход в систему в IntersvyazClient.
This commit is contained in:
parent
cb36edfd2e
commit
6ee66e4020
7 changed files with 158 additions and 8 deletions
|
|
@ -1,8 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace Intersvyaz.Net
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -4,4 +4,8 @@
|
|||
<TargetFramework>netstandard1.4</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
67
src/Intersvyaz.Net/IntersvyazClient.cs
Normal file
67
src/Intersvyaz.Net/IntersvyazClient.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Intersvyaz.Net.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Intersvyaz.Net
|
||||
{
|
||||
public class IntersvyazClient
|
||||
{
|
||||
private readonly Uri BASE_ADDRESS = new Uri("https://api.is74.ru");
|
||||
|
||||
private HttpClient httpClient;
|
||||
|
||||
public IntersvyazClient()
|
||||
{
|
||||
httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = BASE_ADDRESS,
|
||||
};
|
||||
|
||||
// TODO: Добавить здесь ссылку на репозитории.
|
||||
httpClient.DefaultRequestHeaders.UserAgent.Add(
|
||||
new ProductInfoHeaderValue("Intersvyaz.Net", "1.0"));
|
||||
}
|
||||
|
||||
public IntersvyazClient(string token) : base()
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Authorization
|
||||
= new AuthenticationHeaderValue("Bearer", token);
|
||||
}
|
||||
|
||||
public async Task<LoginResponseDto> Login(string username, string password)
|
||||
{
|
||||
var content = new JsonContent(new LoginRequestDto()
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
});
|
||||
|
||||
using (var message = await httpClient.PostAsync("/auth/mobile", content))
|
||||
{
|
||||
var data = await message.Content.ReadAsStringAsync();
|
||||
|
||||
if ((int)message.StatusCode == 422) // NOTE: Data Validation Error
|
||||
{
|
||||
var errors = JsonConvert.DeserializeObject<ValidationErrorDto[]>(data);
|
||||
throw new InvalidOperationException(string.Join("; ", errors.Select(e => e.Message)));
|
||||
}
|
||||
|
||||
if (!message.IsSuccessStatusCode)
|
||||
{
|
||||
throw new InvalidOperationException("Неизвестная ошибка сервера");
|
||||
}
|
||||
|
||||
var result = JsonConvert.DeserializeObject<LoginResponseDto>(data);
|
||||
|
||||
httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", result.Token);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/Intersvyaz.Net/JsonContent.cs
Normal file
20
src/Intersvyaz.Net/JsonContent.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Intersvyaz.Net
|
||||
{
|
||||
public class JsonContent : StringContent
|
||||
{
|
||||
public JsonContent(object value) : base(Serialize(value), Encoding.UTF8, "application/json") { }
|
||||
|
||||
private static string Serialize(object value)
|
||||
{
|
||||
return JsonConvert.SerializeObject(value, Formatting.None, new JsonSerializerSettings()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/Intersvyaz.Net/Models/LoginRequestDto.cs
Normal file
13
src/Intersvyaz.Net/Models/LoginRequestDto.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Intersvyaz.Net.Models
|
||||
{
|
||||
public class LoginRequestDto
|
||||
{
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
41
src/Intersvyaz.Net/Models/LoginResponseDto.cs
Normal file
41
src/Intersvyaz.Net/Models/LoginResponseDto.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Intersvyaz.Net.Models
|
||||
{
|
||||
public class LoginResponseDto
|
||||
{
|
||||
[JsonProperty("USER_ID")]
|
||||
public long UserId { get; set; }
|
||||
|
||||
[JsonProperty("PROFILE_ID")]
|
||||
public long? ProfileId { get; set; }
|
||||
|
||||
[JsonProperty("APP")]
|
||||
public string Application { get; set; }
|
||||
|
||||
[JsonProperty("ACCESS_BEGIN")]
|
||||
public DateTime AccessBegin { get; set; }
|
||||
|
||||
[JsonProperty("ACCESS_END")]
|
||||
public DateTime AccessEnd { get; set; }
|
||||
|
||||
[JsonProperty("TOKEN")]
|
||||
public string Token { get; set; }
|
||||
|
||||
[JsonProperty("AUTH_TYPE")]
|
||||
public string AuthenticationType { get; set; }
|
||||
|
||||
[JsonProperty("UNIQUE_DEVICE_ID")]
|
||||
public string UniqueDeviceId { get; set; }
|
||||
|
||||
[JsonProperty("PHONE")]
|
||||
public int Phone { get; set; }
|
||||
|
||||
[JsonProperty("LAST_CHECK_DATE")]
|
||||
public DateTime LastCheckTime { get; set; }
|
||||
|
||||
[JsonProperty("ACCESS_END_LEFT")]
|
||||
public long AccessEndLeft { get; set; }
|
||||
}
|
||||
}
|
||||
13
src/Intersvyaz.Net/Models/ValidationErrorDto.cs
Normal file
13
src/Intersvyaz.Net/Models/ValidationErrorDto.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Intersvyaz.Net.Models
|
||||
{
|
||||
public class ValidationErrorDto
|
||||
{
|
||||
public string Field { get; set; }
|
||||
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue