-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add library exception and improve interfaces
- Loading branch information
Showing
8 changed files
with
519 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace SteamWebApiLib | ||
{ | ||
public enum CountryCode | ||
{ | ||
Unknown, | ||
|
||
Brazil, | ||
|
||
Bulgaria, | ||
|
||
China, | ||
|
||
Czechia, | ||
|
||
Denmark, | ||
|
||
Finland, | ||
|
||
France, | ||
|
||
Germany, | ||
|
||
GreatBritain, | ||
|
||
Greece, | ||
|
||
Hungary, | ||
|
||
Italy, | ||
|
||
Japan, | ||
|
||
Korea, | ||
|
||
Netherlands, | ||
|
||
Norway, | ||
|
||
Poland, | ||
|
||
Portugal, | ||
|
||
Romania, | ||
|
||
Russia, | ||
|
||
Spain, | ||
|
||
Sweden, | ||
|
||
Thailand, | ||
|
||
Turkey, | ||
|
||
Ukraine, | ||
|
||
USA | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SteamWebApiLib | ||
{ | ||
internal static class CountryCodeConverter | ||
{ | ||
private static readonly Dictionary<CountryCode, string> _countryCodes = | ||
new Dictionary<CountryCode, string>(); | ||
|
||
|
||
static CountryCodeConverter() | ||
{ | ||
RegisterCountryCodes(); | ||
} | ||
|
||
private static void RegisterCountryCodes() | ||
{ | ||
_countryCodes.Add(CountryCode.Brazil, "BR"); | ||
_countryCodes.Add(CountryCode.Bulgaria, "BG"); | ||
_countryCodes.Add(CountryCode.China, "CN"); | ||
_countryCodes.Add(CountryCode.Czechia, "CZ"); | ||
_countryCodes.Add(CountryCode.Denmark, "DK"); | ||
_countryCodes.Add(CountryCode.Finland, "FI"); | ||
_countryCodes.Add(CountryCode.France, "FR"); | ||
_countryCodes.Add(CountryCode.Germany, "DE"); | ||
_countryCodes.Add(CountryCode.GreatBritain, "GB"); | ||
_countryCodes.Add(CountryCode.Greece, "GR"); | ||
_countryCodes.Add(CountryCode.Hungary, "HU"); | ||
_countryCodes.Add(CountryCode.Italy, "IT"); | ||
_countryCodes.Add(CountryCode.Japan, "JP"); | ||
_countryCodes.Add(CountryCode.Korea, "KR"); | ||
_countryCodes.Add(CountryCode.Netherlands, "NL"); | ||
_countryCodes.Add(CountryCode.Norway, "NO"); | ||
_countryCodes.Add(CountryCode.Poland, "PL"); | ||
_countryCodes.Add(CountryCode.Portugal, "PT"); | ||
_countryCodes.Add(CountryCode.Romania, "RO"); | ||
_countryCodes.Add(CountryCode.Russia, "RU"); | ||
_countryCodes.Add(CountryCode.Spain, "ES"); | ||
_countryCodes.Add(CountryCode.Sweden, "SE"); | ||
_countryCodes.Add(CountryCode.Thailand, "TH"); | ||
_countryCodes.Add(CountryCode.Turkey, "TR"); | ||
_countryCodes.Add(CountryCode.Ukraine, "UA"); | ||
_countryCodes.Add(CountryCode.USA, "US"); | ||
} | ||
|
||
public static string GetCountryCodeStringValue(CountryCode countryCode) | ||
{ | ||
if (!_countryCodes.TryGetValue(countryCode, out string value)) | ||
{ | ||
throw new ArgumentException( | ||
$"Country code {countryCode} didn't register.", nameof(countryCode) | ||
); | ||
} | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
namespace SteamWebApiLib.Exceptions | ||
{ | ||
/// <summary> | ||
/// Provides library specific exception to show that the problem occurred while the client was | ||
/// running. | ||
/// </summary> | ||
[Serializable] | ||
public class SteamApiBadRequestException : Exception | ||
{ | ||
/// <summary> | ||
/// Creates the exception. | ||
/// </summary> | ||
public SteamApiBadRequestException() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates the exception with description. | ||
/// </summary> | ||
/// <param name="message">Exception description.</param> | ||
public SteamApiBadRequestException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates the exception with description and inner cause. | ||
/// </summary> | ||
/// <param name="message">Exception description.</param> | ||
/// <param name="innerException">Exception inner cause.</param> | ||
public SteamApiBadRequestException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates the exception from serialized data. | ||
/// Usual scenario is when exception is occured somewhere on the remote workstation | ||
/// and we have to re-create/re-throw the exception on the local machine. | ||
/// </summary> | ||
/// <param name="info">Serialization info.</param> | ||
/// <param name="context">Serialization context.</param> | ||
protected SteamApiBadRequestException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Text; | ||
|
||
namespace SteamWebApiLib | ||
{ | ||
internal class QueryParametersBuilder | ||
{ | ||
private readonly StringBuilder _stringBuilder; | ||
|
||
internal bool HasValue { get; private set; } | ||
|
||
|
||
internal QueryParametersBuilder() | ||
{ | ||
_stringBuilder = new StringBuilder(); | ||
} | ||
|
||
internal QueryParametersBuilder(int capacity) | ||
{ | ||
_stringBuilder = new StringBuilder(capacity); | ||
} | ||
|
||
internal void Reset() | ||
{ | ||
HasValue = false; | ||
_stringBuilder.Clear(); | ||
} | ||
|
||
internal void AppendParameter(string name, string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(value)) return; | ||
|
||
if (HasValue) | ||
{ | ||
_stringBuilder.Append($"&{name}={value}"); | ||
} | ||
else | ||
{ | ||
HasValue = true; | ||
_stringBuilder.Append($"?{name}={value}"); | ||
} | ||
} | ||
|
||
#region Object Overridden Methods | ||
|
||
public override string ToString() => _stringBuilder.ToString(); | ||
|
||
#endregion | ||
} | ||
} |
Oops, something went wrong.