-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUbisoftClient.cs
More file actions
116 lines (88 loc) · 4.34 KB
/
UbisoftClient.cs
File metadata and controls
116 lines (88 loc) · 4.34 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
using System.Text;
using System.Text.Json;
using AvatarStealer.Interfaces;
using AvatarStealer.Models;
using Microsoft.Extensions.Logging;
namespace AvatarStealer;
public class UbisoftClient : IUbisoftClient
{
private readonly HttpClient _httpClient = new();
private readonly ILogger<UbisoftClient> _logger;
public UbisoftClient(ILogger<UbisoftClient> logger)
{
_logger = logger;
_httpClient.DefaultRequestHeaders.Add("Ubi-AppId", "e3d5ea9e-50bd-43b7-88bf-39794f4e3d40");
}
public async Task<UbisoftToken?> GetTokenAsync(string email, string password)
{
using var request =
new HttpRequestMessage(HttpMethod.Post, "https://public-ubiservices.ubi.com/v3/profiles/sessions");
request.Headers.Add("Authorization",
$"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{email}:{password}"))}");
request.Content = new StringContent("{\"Content-Type\": \"application/json\", \"rememberMe\": \"false\"}",
Encoding.UTF8, "application/json");
var tokenResponse = await _httpClient.SendAsync(request);
if (!tokenResponse.IsSuccessStatusCode)
{
var errorResponse = JsonSerializer.Deserialize<UbisoftErrorResponse>(
await tokenResponse.Content.ReadAsStringAsync());
if (errorResponse != null)
{
_logger.LogError("Failed to get token: {Message}", errorResponse.Message);
}
}
return await JsonSerializer.DeserializeAsync<UbisoftToken>(await tokenResponse.Content.ReadAsStreamAsync());
}
public async Task<UbisoftProfileResponse?> GetProfilesAsync(UbisoftToken token, string name)
{
using var request = new HttpRequestMessage(HttpMethod.Get,
$"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={name}&platformType=uplay");
request.Headers.Add("Authorization", $"ubi_v1 t={token.Ticket}");
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var errorResponse = JsonSerializer.Deserialize<UbisoftErrorResponse>(
await response.Content.ReadAsStringAsync());
if (errorResponse != null)
{
_logger.LogError("Failed to get profiles: {Message}", errorResponse.Message);
}
}
return await JsonSerializer.DeserializeAsync<UbisoftProfileResponse>(await response.Content.ReadAsStreamAsync());
}
public async Task<UbisoftAvatar?> GetAvatarAsync(UbisoftToken token, Guid profileId)
{
using var request = new HttpRequestMessage(HttpMethod.Get,
$"https://public-ubiservices.ubi.com/v1/profiles/{profileId}/avatars");
request.Headers.Add("Authorization", $"ubi_v1 t={token.Ticket}");
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var errorResponse = JsonSerializer.Deserialize<UbisoftErrorResponse>(
await response.Content.ReadAsStringAsync());
if (errorResponse != null)
{
_logger.LogError("Failed to get avatar: {Message}", errorResponse.Message);
}
}
return await JsonSerializer.DeserializeAsync<UbisoftAvatar>(await response.Content.ReadAsStreamAsync());
}
public async Task<UbisoftAvatar?> ChangeAvatarAsync(UbisoftToken token, Guid avatarId)
{
using var request = new HttpRequestMessage(HttpMethod.Post,
$"https://public-ubiservices.ubi.com/v1/profiles/{token.UserId}/avatars/galleries");
request.Headers.Add("Authorization", $"ubi_v1 t={token.Ticket}");
request.Content = new StringContent($"{{\n\t\"galleryAvatarId\": \"{avatarId}\"\n}}", Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var errorResponse = JsonSerializer.Deserialize<UbisoftErrorResponse>(
await response.Content.ReadAsStringAsync());
if (errorResponse != null)
{
_logger.LogError("Failed to change avatar: {Message}", errorResponse.Message);
}
}
return await JsonSerializer.DeserializeAsync<UbisoftAvatar>(await response.Content.ReadAsStreamAsync());
}
}