@@ -19,34 +19,32 @@ internal class EpicGamesRepository : IStoreRepository
19
19
private const string OAuthHost = "account-public-service-prod03.ol.epicgames.com" ;
20
20
private const string UserAgent = "UELauncher/11.0.1-14907503+++Portal+Release-Live Windows/10.0.19041.1.256.64bit" ;
21
21
22
- private static readonly HttpClient HttpClient ;
22
+ private readonly HttpClient _httpClient ;
23
23
private readonly ILogger _log ;
24
24
private readonly AuthManager _authManager ;
25
25
26
- static EpicGamesRepository ( )
27
- {
28
- HttpClient = new HttpClient ( ) ;
29
- HttpClient . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
30
- }
31
- public EpicGamesRepository ( AuthManager authManager , ILogger logger )
26
+ public EpicGamesRepository ( AuthManager authManager , ILogger logger , HttpClient httpClient )
32
27
{
33
28
_log = logger ;
34
29
_authManager = authManager ;
30
+ _httpClient = httpClient ;
31
+ _httpClient . DefaultRequestHeaders . Add ( "User-Agent" , UserAgent ) ;
35
32
}
33
+
36
34
public async Task < Metadata > FetchGameMetaData ( string nameSpace , string catalogItemId )
37
35
{
38
36
39
37
_log . Information ( "FetchGameMetaData: Fetching game metadata" ) ;
40
38
var accessToken = await _authManager . GetAccessToken ( ) ;
41
- HttpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
39
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
42
40
43
41
// API requests parameters to be in query instead of body
44
42
var qs = $ "?id={ catalogItemId } &includeDLCDetails=true&includeMainGameDetails=true&country=US&locale=en";
45
43
46
44
try
47
45
{
48
46
// Make the API call with the form data
49
- var httpResponse = await HttpClient . GetAsync ( $ "https://{ CatalogHost } /catalog/api/shared/namespace/{ nameSpace } /bulk/items{ qs } ") ;
47
+ var httpResponse = await _httpClient . GetAsync ( $ "https://{ CatalogHost } /catalog/api/shared/namespace/{ nameSpace } /bulk/items{ qs } ") ;
50
48
// Check if the request was successful (status code 200)
51
49
if ( httpResponse . IsSuccessStatusCode )
52
50
{
@@ -85,8 +83,8 @@ public async Task<IEnumerable<Asset>> FetchGameAssets(string platform = "Windows
85
83
_log . Information ( "FetchGameAssets: Fetching game assets" ) ;
86
84
var accessToken = await _authManager . GetAccessToken ( ) ;
87
85
88
- HttpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
89
- var httpResponse = await HttpClient . GetAsync ( $ "https://{ LauncherHost } /launcher/api/public/assets/{ platform } ?label={ label } ") ;
86
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
87
+ var httpResponse = await _httpClient . GetAsync ( $ "https://{ LauncherHost } /launcher/api/public/assets/{ platform } ?label={ label } ") ;
90
88
91
89
if ( httpResponse . IsSuccessStatusCode )
92
90
{
@@ -130,7 +128,7 @@ public async Task<GetGameManifest> GetGameManifest(string nameSpace, string cata
130
128
131
129
try
132
130
{
133
- var httpResponse = await HttpClient . GetAsync ( url ) ;
131
+ var httpResponse = await _httpClient . GetAsync ( url ) ;
134
132
if ( ! httpResponse . IsSuccessStatusCode )
135
133
{
136
134
_log . Error ( $ "Failed to fetch manifests from { url } , trying next url") ;
@@ -159,30 +157,38 @@ public async Task<GetGameManifest> GetGameManifest(string nameSpace, string cata
159
157
160
158
public async Task DownloadFileAsync ( string url , string destinationPath )
161
159
{
162
- var accessToken = await _authManager . GetAccessToken ( ) ;
163
- //HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
160
+ try
161
+ {
162
+ var accessToken = await _authManager . GetAccessToken ( ) ;
163
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
164
164
165
- using var response = await HttpClient . GetAsync ( url , HttpCompletionOption . ResponseHeadersRead ) ;
166
- response . EnsureSuccessStatusCode ( ) ;
165
+ using var response = await _httpClient . GetAsync ( url ) ;
166
+ if ( ! response . IsSuccessStatusCode )
167
+ throw new Exception ( $ "File { url } failed to download") ;
167
168
168
- // Create the directory if it doesn't exist
169
- var directoryPath = Path . GetDirectoryName ( destinationPath ) ;
170
- if ( ! string . IsNullOrEmpty ( directoryPath ) )
169
+ // Create the directory if it doesn't exist
170
+ var directoryPath = Path . GetDirectoryName ( destinationPath ) ;
171
+ if ( ! string . IsNullOrEmpty ( directoryPath ) )
172
+ {
173
+ Directory . CreateDirectory ( directoryPath ) ;
174
+ }
175
+ await using var stream = await response . Content . ReadAsStreamAsync ( ) ;
176
+ await using var fileStream = new FileStream ( destinationPath , FileMode . Create , FileAccess . Write , FileShare . None ) ;
177
+ await stream . CopyToAsync ( fileStream ) ;
178
+ }
179
+ catch ( Exception ex )
171
180
{
172
- Directory . CreateDirectory ( directoryPath ) ;
181
+ _log . Error ( "Failed to download file}" , url ) ;
173
182
}
174
- await using var stream = await response . Content . ReadAsStreamAsync ( ) ;
175
- await using var fileStream = new FileStream ( destinationPath , FileMode . Create , FileAccess . Write , FileShare . None ) ;
176
- await stream . CopyToAsync ( fileStream ) ;
177
183
}
178
184
179
185
public async Task < string > GetGameToken ( )
180
186
{
181
187
try
182
188
{
183
189
var accessToken = await _authManager . GetAccessToken ( ) ;
184
- HttpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
185
- using var response = await HttpClient . GetAsync ( $ "https://{ OAuthHost } /account/api/oauth/exchange") ;
190
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
191
+ using var response = await _httpClient . GetAsync ( $ "https://{ OAuthHost } /account/api/oauth/exchange") ;
186
192
response . EnsureSuccessStatusCode ( ) ;
187
193
188
194
await using var stream = await response . Content . ReadAsStreamAsync ( ) ;
@@ -204,8 +210,8 @@ private async Task<GetManifestUrlData> GetManifestUrls(string nameSpace, string
204
210
_log . Information ( "GetGameManifest: Fetching game assets" ) ;
205
211
var accessToken = await _authManager . GetAccessToken ( ) ;
206
212
207
- HttpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
208
- var httpResponse = await HttpClient . GetAsync ( $ "https://{ LauncherHost } /launcher/api/public/assets/v2/platform/{ platform } /namespace/{ nameSpace } /catalogItem/{ catalogItem } /app/{ appName } /label/{ label } ") ;
213
+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
214
+ var httpResponse = await _httpClient . GetAsync ( $ "https://{ LauncherHost } /launcher/api/public/assets/v2/platform/{ platform } /namespace/{ nameSpace } /catalogItem/{ catalogItem } /app/{ appName } /label/{ label } ") ;
209
215
210
216
if ( httpResponse . IsSuccessStatusCode )
211
217
{
0 commit comments