Skip to content

Commit

Permalink
Add library exception and improve interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasar007 committed May 16, 2019
1 parent 7a3f5b6 commit ac4061e
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 139 deletions.
59 changes: 59 additions & 0 deletions SteamWebApiLib/CountryCode.cs
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
}
}
58 changes: 58 additions & 0 deletions SteamWebApiLib/CountryCodeConverter.cs
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;
}
}
}
51 changes: 51 additions & 0 deletions SteamWebApiLib/Exceptions/SteamApiBadRequestException.cs
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)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
namespace SteamWebApiLib.Models.Reviews
namespace SteamWebApiLib
{
public enum Language
{
Unknown,

Bulgarian,

Schinese,
Brazilian,

Tchinese,
Bulgarian,

Czech,

Expand Down Expand Up @@ -40,16 +38,24 @@ public enum Language

Portuguese,

Brazilian,

Romanian,

Russian,

/// <summary>
/// Simplified chinese language.
/// </summary>
Schinese,

Spanish,

Swedish,

/// <summary>
/// Tranditional chinese language.
/// </summary>
Tchinese,

Thai,

Turkish,
Expand Down
49 changes: 49 additions & 0 deletions SteamWebApiLib/QueryParametersBuilder.cs
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
}
}
Loading

0 comments on commit ac4061e

Please sign in to comment.