forked from babelshift/SteamWebAPI2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamStoreRequest.cs
More file actions
122 lines (104 loc) · 4.69 KB
/
SteamStoreRequest.cs
File metadata and controls
122 lines (104 loc) · 4.69 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
using Newtonsoft.Json;
using SteamWebAPI2.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
namespace SteamWebAPI2
{
/// <summary>
/// Represents a request to send to a Steam Store Web API
/// </summary>
internal class SteamStoreRequest
{
private string steamStoreApiBaseUrl;
private readonly HttpClient httpClient;
/// <summary>
/// Constructs a Steam Store Web API request
/// </summary>
/// <param name="steamStoreApiBaseUrl">Steam Store Web API URL</param>
public SteamStoreRequest(string steamStoreApiBaseUrl)
{
if (string.IsNullOrEmpty(steamStoreApiBaseUrl))
{
throw new ArgumentNullException("steamStoreApiBaseUrl");
}
this.steamStoreApiBaseUrl = steamStoreApiBaseUrl;
}
public SteamStoreRequest(string steamStoreApiBaseUrl, HttpClient httpClient)
{
if (string.IsNullOrEmpty(steamStoreApiBaseUrl))
{
throw new ArgumentNullException("steamStoreApiBaseUrl");
}
this.steamStoreApiBaseUrl = steamStoreApiBaseUrl;
this.httpClient = httpClient;
}
/// <summary>
/// Sends a request to a Steam Store Web API endpoint
/// </summary>
/// <typeparam name="T">Type of object which will be deserialized from the response</typeparam>
/// <param name="endpointName">Endpoint to call on the interface</param>
/// <returns></returns>
public async Task<T> SendStoreRequestAsync<T>(string endpointName)
{
Debug.Assert(!string.IsNullOrEmpty(endpointName));
return await SendStoreRequestAsync<T>(endpointName, null);
}
/// <summary>
/// Sends a request to a Steam Store Web API endpoint with parameters
/// </summary>
/// <typeparam name="T">Type of object which will be deserialized from the response</typeparam>
/// <param name="endpointName">Endpoint to call on the interface</param>
/// <param name="parameters">Parameters to pass to the endpoint</param>
/// <returns>Deserialized response object</returns>
public async Task<T> SendStoreRequestAsync<T>(string endpointName, IList<SteamWebRequestParameter> parameters)
{
Debug.Assert(!string.IsNullOrEmpty(endpointName));
if (parameters == null)
{
parameters = new List<SteamWebRequestParameter>();
}
string command = BuildRequestCommand(endpointName, parameters);
string response = await GetHttpStringResponseAsync(command).ConfigureAwait(false);
var deserializedResult = JsonConvert.DeserializeObject<T>(response);
return deserializedResult;
}
/// <summary>
/// Returns a string from an HTTP request and removes tabs and newlines
/// </summary>
/// <param name="command">Command (method endpoint) to send to an interface</param>
/// <returns>HTTP response as a string without tabs and newlines</returns>
private async Task<string> GetHttpStringResponseAsync(string command)
{
HttpClient client = httpClient ?? new HttpClient();
string response = await client.GetStringAsync(command);
response = response.Replace("\n", "");
response = response.Replace("\t", "");
return response;
}
/// <summary>
/// Builds a command to send with a request so that parameters and formats are correct
/// </summary>
/// <param name="endpointName">Endpoint to call on the interface</param>
/// <param name="parameters">Parameters to send to the endpoint</param>
/// <returns>Deserialized response object</returns>
public string BuildRequestCommand(string endpointName, IList<SteamWebRequestParameter> parameters)
{
Debug.Assert(!string.IsNullOrEmpty(endpointName));
if (steamStoreApiBaseUrl.EndsWith("/"))
{
steamStoreApiBaseUrl = steamStoreApiBaseUrl.Remove(steamStoreApiBaseUrl.Length - 1, 1);
}
string commandUrl = string.Format("{0}/{1}/", steamStoreApiBaseUrl, endpointName);
// if we have parameters, join them together with & delimiter and append them to the command URL
if (parameters != null && parameters.Count > 0)
{
string parameterString = string.Join("&", parameters);
commandUrl += string.Format("?{0}", parameterString);
}
return commandUrl;
}
}
}