Skip to content

Commit 24af4e6

Browse files
committed
allow injecting custom httpclient to steamstore
1 parent f69db4e commit 24af4e6

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/SteamWebAPI2/Interfaces/SteamStore.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
using SteamWebAPI2.Models.SteamStore;
33
using SteamWebAPI2.Utilities;
44
using System.Collections.Generic;
5+
using System.Net.Http;
56
using System.Threading.Tasks;
67

78
namespace SteamWebAPI2.Interfaces
89
{
910
public class SteamStore : SteamStoreInterface, ISteamStore
1011
{
12+
public SteamStore() : base()
13+
{
14+
}
15+
16+
public SteamStore(HttpClient httpClient) : base(httpClient)
17+
{
18+
}
19+
1120
/// <summary>
1221
/// Maps to the steam store api endpoint: GET http://store.steampowered.com/api/appdetails/
1322
/// </summary>
@@ -52,4 +61,4 @@ public async Task<StoreFeaturedProductsModel> GetStoreFeaturedProductsAsync()
5261
return featuredProductsModel;
5362
}
5463
}
55-
}
64+
}

src/SteamWebAPI2/SteamStoreInterface.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5-
using System.Linq;
6-
using System.Text;
5+
using System.Net.Http;
76
using System.Threading.Tasks;
87

98
namespace SteamWebAPI2
@@ -15,7 +14,7 @@ public abstract class SteamStoreInterface
1514
{
1615
private const string steamStoreApiBaseUrl = "http://store.steampowered.com/api/";
1716
private readonly SteamStoreRequest steamStoreRequest;
18-
17+
1918
/// <summary>
2019
/// Constructs and maps the default objects for Steam Store Web API use
2120
/// </summary>
@@ -26,6 +25,17 @@ public SteamStoreInterface()
2625
AutoMapperConfiguration.Initialize();
2726
}
2827

28+
/// <summary>
29+
/// Constructs and maps based on a custom http client
30+
/// </summary>
31+
/// <param name="httpClient">Client to make requests with</param>
32+
public SteamStoreInterface(HttpClient httpClient)
33+
{
34+
this.steamStoreRequest = new SteamStoreRequest(steamStoreApiBaseUrl, httpClient);
35+
36+
AutoMapperConfiguration.Initialize();
37+
}
38+
2939
/// <summary>
3040
/// Constructs and maps based on a custom Steam Store Web API URL
3141
/// </summary>
@@ -37,6 +47,18 @@ public SteamStoreInterface(string steamStoreApiBaseUrl)
3747
AutoMapperConfiguration.Initialize();
3848
}
3949

50+
/// <summary>
51+
/// Constructs and maps based on a custom http client and custom Steam Store Web API URL
52+
/// </summary>
53+
/// <param name="steamStoreApiBaseUrl">Steam Store Web API URL</param>
54+
/// <param name="httpClient">Client to make requests with</param>
55+
public SteamStoreInterface(string steamStoreApiBaseUrl, HttpClient httpClient)
56+
{
57+
this.steamStoreRequest = new SteamStoreRequest(steamStoreApiBaseUrl, httpClient);
58+
59+
AutoMapperConfiguration.Initialize();
60+
}
61+
4062
/// <summary>
4163
/// Calls a endpoint on the constructed Web API with parameters
4264
/// </summary>

src/SteamWebAPI2/SteamStoreRequest.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace SteamWebAPI2
1414
internal class SteamStoreRequest
1515
{
1616
private string steamStoreApiBaseUrl;
17+
private readonly HttpClient httpClient;
1718

1819
/// <summary>
1920
/// Constructs a Steam Store Web API request
@@ -29,6 +30,17 @@ public SteamStoreRequest(string steamStoreApiBaseUrl)
2930
this.steamStoreApiBaseUrl = steamStoreApiBaseUrl;
3031
}
3132

33+
public SteamStoreRequest(string steamStoreApiBaseUrl, HttpClient httpClient)
34+
{
35+
if (String.IsNullOrEmpty(steamStoreApiBaseUrl))
36+
{
37+
throw new ArgumentNullException("steamStoreApiBaseUrl");
38+
}
39+
40+
this.steamStoreApiBaseUrl = steamStoreApiBaseUrl;
41+
this.httpClient = httpClient;
42+
}
43+
3244
/// <summary>
3345
/// Sends a request to a Steam Store Web API endpoint
3446
/// </summary>
@@ -71,10 +83,10 @@ public async Task<T> SendStoreRequestAsync<T>(string endpointName, IList<SteamWe
7183
/// </summary>
7284
/// <param name="command">Command (method endpoint) to send to an interface</param>
7385
/// <returns>HTTP response as a string without tabs and newlines</returns>
74-
private static async Task<string> GetHttpStringResponseAsync(string command)
86+
private async Task<string> GetHttpStringResponseAsync(string command)
7587
{
76-
HttpClient httpClient = new HttpClient();
77-
string response = await httpClient.GetStringAsync(command);
88+
HttpClient client = httpClient ?? new HttpClient();
89+
string response = await client.GetStringAsync(command);
7890
response = response.Replace("\n", "");
7991
response = response.Replace("\t", "");
8092
return response;

0 commit comments

Comments
 (0)