add IsEmailAvailable method
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace UptimeSharp.Tests
|
||||
{
|
||||
public class AccountTest : TestsBase
|
||||
{
|
||||
public AccountTest() : base() { }
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task IsAvailabilityCheckWorking()
|
||||
{
|
||||
Assert.False(await client.IsEmailAvailable("cee@live.at"));
|
||||
Assert.True(await client.IsEmailAvailable(Guid.NewGuid().ToString() + "@live.at"));
|
||||
|
||||
await ThrowsAsync<ArgumentNullException>(async () =>
|
||||
{
|
||||
await client.IsEmailAvailable("");
|
||||
});
|
||||
|
||||
await ThrowsAsync<ArgumentNullException>(async () =>
|
||||
{
|
||||
await client.IsEmailAvailable("");
|
||||
});
|
||||
|
||||
await ThrowsAsync<ArgumentException>(async () =>
|
||||
{
|
||||
await client.IsEmailAvailable("opiu@opiuast");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AccountTest.cs" />
|
||||
<Compile Include="AlertsTest.cs" />
|
||||
<Compile Include="ClientTest.cs" />
|
||||
<Compile Include="MonitorsTest.cs" />
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// UptimeClient
|
||||
/// </summary>
|
||||
public partial class UptimeClient
|
||||
{
|
||||
private Regex isEmailRegex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,10}))$", RegexOptions.IgnoreCase);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether a e-mail is available.
|
||||
/// </summary>
|
||||
/// <param name="email">The email.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="System.ArgumentNullException">Please provide an e-mail address</exception>
|
||||
/// <exception cref="System.ArgumentException">Please provide a valid e-mail address</exception>
|
||||
public async Task<bool> IsEmailAvailable(string email, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (String.IsNullOrEmpty(email))
|
||||
{
|
||||
throw new ArgumentNullException("Please provide an e-mail address");
|
||||
}
|
||||
|
||||
if (!isEmailRegex.IsMatch(email))
|
||||
{
|
||||
throw new ArgumentException("Please provide a valid e-mail address");
|
||||
}
|
||||
|
||||
return (await AccountRequest("checkUserEmail", cancellationToken, new Dictionary<string, string>()
|
||||
{
|
||||
{ "userEmail", email }
|
||||
})).Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,5 +187,18 @@ namespace UptimeSharp
|
||||
/// <exception cref="UptimeSharpException"></exception>
|
||||
Task<bool> DeleteAlert(Alert alert, CancellationToken cancellationToken = default(CancellationToken));
|
||||
#endregion
|
||||
|
||||
|
||||
#region Account
|
||||
/// <summary>
|
||||
/// Determines whether a e-mail is available.
|
||||
/// </summary>
|
||||
/// <param name="email">The email.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="System.ArgumentNullException">Please provide an e-mail address</exception>
|
||||
/// <exception cref="System.ArgumentException">Please provide a valid e-mail address</exception>
|
||||
Task<bool> IsEmailAvailable(string email, CancellationToken cancellationToken = default(CancellationToken));
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace UptimeSharp.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Account Response
|
||||
/// </summary>
|
||||
[JsonObject]
|
||||
internal class AccountResponse : Response { }
|
||||
}
|
||||
@@ -22,6 +22,11 @@ namespace UptimeSharp
|
||||
/// </summary>
|
||||
protected readonly HttpClient _restClient;
|
||||
|
||||
/// <summary>
|
||||
/// REST client used for the account communication
|
||||
/// </summary>
|
||||
protected readonly HttpClient _accountClient;
|
||||
|
||||
/// <summary>
|
||||
/// Caches HTTP headers from last response
|
||||
/// </summary>
|
||||
@@ -33,10 +38,16 @@ namespace UptimeSharp
|
||||
public string lastResponseData;
|
||||
|
||||
/// <summary>
|
||||
/// The base URL for the Pocket API
|
||||
/// The base URL for the UptimeRobot API
|
||||
/// </summary>
|
||||
protected static Uri baseUri = new Uri("http://api.uptimerobot.com/");
|
||||
|
||||
/// <summary>
|
||||
/// The account base URL for the UptimeRobot API
|
||||
/// Does not use the official API endpoint
|
||||
/// </summary>
|
||||
protected static Uri accountBaseUri = new Uri("http://uptimerobot.com/inc/dml/userDML.php");
|
||||
|
||||
/// <summary>
|
||||
/// Accessor for the UptimeRebot API key
|
||||
/// see: http://www.uptimerobot.com/api
|
||||
@@ -77,17 +88,23 @@ namespace UptimeSharp
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
|
||||
});
|
||||
_accountClient = new HttpClient(handler ?? new HttpClientHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
|
||||
});
|
||||
|
||||
if (timeout.HasValue)
|
||||
{
|
||||
_restClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
|
||||
_accountClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
|
||||
}
|
||||
|
||||
// set base target
|
||||
_restClient.BaseAddress = baseUri;
|
||||
_accountClient.BaseAddress = accountBaseUri;
|
||||
|
||||
// defines the response format
|
||||
_restClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
_accountClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +115,10 @@ namespace UptimeSharp
|
||||
/// <param name="method">Requested method (appended to base path)</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <param name="parameters">Additional POST parameters</param>
|
||||
/// <param name="isAccountRequest">if set to <c>true</c> [is account request].</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UptimeSharpException">
|
||||
/// </exception>
|
||||
protected async Task<T> Request<T>(string method, CancellationToken cancellationToken, Dictionary<string, string> parameters = null) where T : class, new()
|
||||
{
|
||||
HttpRequestMessage request;
|
||||
@@ -150,13 +170,14 @@ namespace UptimeSharp
|
||||
lastHeaders = response.Headers;
|
||||
|
||||
// read response
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
string responseString = await response.Content.ReadAsStringAsync();
|
||||
T parsedResponse;
|
||||
|
||||
// cache response
|
||||
lastResponseData = responseString;
|
||||
|
||||
// deserialize object
|
||||
T parsedResponse = JsonConvert.DeserializeObject<T>(
|
||||
parsedResponse = JsonConvert.DeserializeObject<T>(
|
||||
responseString,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
@@ -181,6 +202,78 @@ namespace UptimeSharp
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a typed resource
|
||||
/// </summary>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <param name="parameters">Additional POST parameters</param>
|
||||
/// <param name="isPost">if set to <c>true</c> [is post].</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UptimeSharpException">
|
||||
/// </exception>
|
||||
internal async Task<AccountResponse> AccountRequest(string action, CancellationToken cancellationToken, Dictionary<string, string> parameters = null, bool isPost = false)
|
||||
{
|
||||
HttpRequestMessage request;
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
if (parameters == null)
|
||||
{
|
||||
parameters = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
if (!isPost)
|
||||
{
|
||||
IEnumerable<string> paramEnumerable = parameters.Where(item => !String.IsNullOrEmpty(item.Value)).Select(item => Uri.EscapeDataString(item.Key) + "=" + Uri.EscapeDataString(item.Value));
|
||||
request = new HttpRequestMessage(HttpMethod.Get, "?action=" + Uri.EscapeDataString(action) + "&" + String.Join("&", paramEnumerable));
|
||||
}
|
||||
else
|
||||
{
|
||||
request = new HttpRequestMessage(HttpMethod.Post, "?action=" + Uri.EscapeDataString(action))
|
||||
{
|
||||
Content = new FormUrlEncodedContent(parameters.Where(item => !String.IsNullOrEmpty(item.Value)))
|
||||
};
|
||||
}
|
||||
|
||||
// call pre request action
|
||||
if (PreRequest != null)
|
||||
{
|
||||
PreRequest(action);
|
||||
}
|
||||
|
||||
// make async request
|
||||
try
|
||||
{
|
||||
response = await _accountClient.SendAsync(request, cancellationToken);
|
||||
}
|
||||
catch (HttpRequestException exc)
|
||||
{
|
||||
throw new UptimeSharpException(exc.Message, exc);
|
||||
}
|
||||
|
||||
// HTTP error
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
throw new UptimeSharpException("Request Exception: {0} (Code: {1})", response.ReasonPhrase, response.StatusCode.ToString());
|
||||
}
|
||||
|
||||
// cache headers
|
||||
lastHeaders = response.Headers;
|
||||
|
||||
// read response
|
||||
string responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// cache response
|
||||
lastResponseData = responseString;
|
||||
|
||||
// deserialize object
|
||||
return new AccountResponse()
|
||||
{
|
||||
RawStatus = responseString == "true" ? "ok" : "fail"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validates the response.
|
||||
/// </summary>
|
||||
@@ -198,7 +291,7 @@ namespace UptimeSharp
|
||||
}
|
||||
|
||||
// don't raise exceptions for minor error codes
|
||||
if (successFailCodes.Contains(response.ErrorCode))
|
||||
if (!String.IsNullOrEmpty(response.ErrorCode) && successFailCodes.Contains(response.ErrorCode))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Components\Account.cs" />
|
||||
<Compile Include="Components\Alert.cs" />
|
||||
<Compile Include="Components\Monitor.cs" />
|
||||
<Compile Include="IUptimeClient.cs" />
|
||||
@@ -48,6 +49,7 @@
|
||||
<Compile Include="Models\Parameters\MonitorParameters.cs" />
|
||||
<Compile Include="Models\Parameters\Parameters.cs" />
|
||||
<Compile Include="Models\Parameters\RetrieveParameters.cs" />
|
||||
<Compile Include="Models\Response\AccountRepsonse.cs" />
|
||||
<Compile Include="Models\Response\AddAlertResonse.cs" />
|
||||
<Compile Include="Models\Response\AddMonitorResponse.cs" />
|
||||
<Compile Include="Models\Response\AlertResponse.cs" />
|
||||
|
||||
Reference in New Issue
Block a user