-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAuthServiceClient.cs
More file actions
151 lines (132 loc) · 5.6 KB
/
AuthServiceClient.cs
File metadata and controls
151 lines (132 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using HwProj.HttpUtils;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.AuthService.DTO;
using Newtonsoft.Json;
using HwProj.Models.Result;
using Microsoft.Extensions.Configuration;
namespace HwProj.AuthService.Client
{
public class AuthServiceClient : IAuthServiceClient
{
private readonly HttpClient _httpClient;
private readonly Uri _authServiceUri;
public AuthServiceClient(IHttpClientFactory clientFactory, IConfiguration configuration)
{
_httpClient = clientFactory.CreateClient();
_authServiceUri = new Uri(configuration.GetSection("Services")["Auth"]);
}
public async Task<AccountDataDto> GetAccountData(string userId)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Get,
_authServiceUri + $"api/account/getUserData/{userId}");
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<AccountDataDto>().ConfigureAwait(false);
}
public async Task<Result<TokenCredentials>> Register(RegisterViewModel model)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Post,
_authServiceUri + "api/account/register")
{
Content = new StringContent(
JsonConvert.SerializeObject(model),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result<TokenCredentials>>();
}
public async Task<Result<TokenCredentials>> Login(LoginViewModel model)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Post,
_authServiceUri + "api/account/login")
{
Content = new StringContent(
JsonConvert.SerializeObject(model),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result<TokenCredentials>>();
}
public async Task<Result> Edit(EditAccountViewModel model, string userId)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Put,
_authServiceUri + $"api/account/edit/{userId}")
{
Content = new StringContent(
JsonConvert.SerializeObject(model),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result>();
}
public async Task<Result> InviteNewLecturer(InviteLecturerViewModel model)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Post,
_authServiceUri + "api/account/inviteNewLecturer")
{
Content = new StringContent(
JsonConvert.SerializeObject(model),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result>();
}
public async Task<Result<TokenCredentials>> LoginByGoogle(string tokenId)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Post,
_authServiceUri + $"api/account/google/{tokenId}");
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result<TokenCredentials>>();;
}
public async Task<Result> EditExternal(EditExternalViewModel model, string userId)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Put,
_authServiceUri + $"api/account/editExternal/{userId}")
{
Content = new StringContent(
JsonConvert.SerializeObject(model),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<Result>();
}
public async Task<string> FindByEmailAsync(string email)
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Get,
_authServiceUri + $"api/account/findByEmail/{email}")
{
Content = new StringContent(
JsonConvert.SerializeObject(email),
Encoding.UTF8,
"application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
var user = await response.DeserializeAsync<UserViewModel>();
return user?.Id;
}
public async Task<AccountDataDto[]> GetAllStudents()
{
using var httpRequest = new HttpRequestMessage(
HttpMethod.Get,
_authServiceUri + $"api/account/getAllStudents");
var response = await _httpClient.SendAsync(httpRequest);
return await response.DeserializeAsync<AccountDataDto[]>().ConfigureAwait(false);
}
}
}