Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Morpher.WebService.V3.Client/BadRequestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ namespace Morpher.WebService.V3
class BadRequestException : Exception
{
public int Status { get; }
public int ErrorCode { get; }

public BadRequestException(int status)
{
Status = status;
}

public BadRequestException(int status, int errorCode)
{
Status = status;
ErrorCode = errorCode;
}
}
}
10 changes: 9 additions & 1 deletion Morpher.WebService.V3.Client/MyWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Morpher.WebService.V3
{
using System.Collections.Generic;
using System.Collections.Specialized;
using Newtonsoft.Json;

Expand Down Expand Up @@ -136,7 +137,14 @@ static void TryToThrowMorpherException(WebException exc)
throw new InvalidServerResponseException(exc);
}

throw new BadRequestException(status);
var jsonResponse = new StreamReader(httpWebResponse.GetResponseStream()).ReadToEnd();

var resp = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonResponse);
string errorCode = "";
if (resp.TryGetValue("code", out errorCode))
throw new BadRequestException(status, int.Parse(errorCode));
else
throw new BadRequestException(status);
}

if (status >= 500)
Expand Down
38 changes: 38 additions & 0 deletions Morpher.WebService.V3.Client/Russian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,43 @@ public List<string> Adjectivize(string lemma)
return client.GetObject<List<string>>("/russian/adjectivize");
}
}

public Propis GetPropis(decimal n, string currency,
string padeg, string capitals, string nbsp, string delim)
{
if (string.IsNullOrWhiteSpace(currency))
{
throw new ArgumentEmptyException(nameof(currency));
}

using (var client = _newClient())
{

try
{
client.AddParam("n", n.ToString(CultureInfo.InvariantCulture));
client.AddParam("currency", currency);
client.AddParam("case", padeg);
client.AddParam("capitals", capitals);
client.AddParam("nbsp", nbsp);
client.AddParam("delim", delim);

return client.GetObject<Propis>("/russian/propis");
}
catch (BadRequestException e)
{
switch (e.ErrorCode)
{
case 20: throw new WrongCurrencyException(nameof(currency));
case 21: throw new InvalidCapitalsValueException(nameof(capitals));
case 22: throw new InvalidCaseValueException("case");
case 23: throw new InvalidDelimValueException(nameof(delim));
case 24: throw new InvalidNbspValueException(nameof(nbsp));
}

throw;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Russian
{
public class InvalidCapitalsValueException : InvalidArgumentException
{
public override string Message =>
"Неправильное значение параметра capitals. Допустимые значения: frist/all/none.";

public InvalidCapitalsValueException(string parameterName)
: base(parameterName)
{
}
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Russian/InvalidCaseValueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Russian
{
public class InvalidCaseValueException : InvalidArgumentException
{
public override string Message =>
"Неправильное значение параметра case. Допустимые значения: nominative/genitive/dative/accusative/instrumental/prepositional.";

public InvalidCaseValueException(string parameterName)
: base(parameterName)
{
}
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Russian/InvalidDelimValueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Russian
{
public class InvalidDelimValueException : InvalidArgumentException
{
public override string Message =>
"Неправильное значение параметра delim. Допустимые значения: thinsp/space/none/comma/dot.";

public InvalidDelimValueException(string parameterName)
: base(parameterName)
{
}
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Russian/InvalidNbspValueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Russian
{
public class InvalidNbspValueException : InvalidArgumentException
{
public override string Message =>
"Неправильное значение параметра nbsp. Допустимые значения: all/none.";

public InvalidNbspValueException(string parameterName)
: base(parameterName)
{
}
}
}
26 changes: 26 additions & 0 deletions Morpher.WebService.V3.Client/Russian/Propis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Runtime.Serialization;

namespace Morpher.WebService.V3.Russian
{
[DataContract]
public class Propis
{
[DataMember(Name = "propis1")]
public string Propis1 { get; set; }

[DataMember(Name = "propis2")]
public string Propis2 { get; set; }

[DataMember(Name = "propis3")]
public string Propis3 { get; set; }

[DataMember(Name = "amount")]
public string Amount { get; set; }

[DataMember(Name = "currency")]
public string Currency { get; set; }

[DataMember(Name = "cents")]
public string Cents { get; set; }
}
}
13 changes: 13 additions & 0 deletions Morpher.WebService.V3.Client/Russian/WrongCurrencyException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morpher.WebService.V3.Russian
{
public class WrongCurrencyException : InvalidArgumentException
{
public override string Message =>
"Неправильное значение параметра currency.";

public WrongCurrencyException(string parameterName)
: base(parameterName)
{
}
}
}