Skip to content

Commit

Permalink
Merged in release/0.1.1 (pull request #2)
Browse files Browse the repository at this point in the history
Release/0.1.1
  • Loading branch information
Tony Lechner committed May 24, 2016
2 parents 8cd54dd + b9f7ea9 commit ee44c8c
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Libraries/CloseIoDotNet/CloseIoDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Entities\Definitions\Lead.cs" />
<Compile Include="Entities\Definitions\Opportunities\Opportunity.cs" />
<Compile Include="Entities\Definitions\Tasks\Task.cs" />
<Compile Include="Entities\Enumerations\ScanType.cs" />
<Compile Include="Entities\Fields\EntityFieldAttribute.cs" />
<Compile Include="Entities\Fields\IEntityField.cs" />
<Compile Include="ICloseIoDotNetContext.cs" />
Expand All @@ -81,6 +82,8 @@
<Compile Include="Rest\Entities\Responses\Enumerables\IScanEnumerable.cs" />
<Compile Include="Rest\Entities\Responses\Enumerables\ScanEnumerable.cs" />
<Compile Include="Rest\Entities\Responses\ScanResponse.cs" />
<Compile Include="Rest\Exceptions\CloseIoRequestException.cs" />
<Compile Include="Rest\Exceptions\InternalServerErrorException.cs" />
<Compile Include="Rest\RequestFactories\IRestRequestFactory.cs" />
<Compile Include="Rest\RequestFactories\RestRequestFactory.cs" />
<Compile Include="Rest\Serialization\NewtonsoftDeserializer.cs" />
Expand Down
28 changes: 28 additions & 0 deletions Libraries/CloseIoDotNet/CloseIoDotNetContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CloseIoDotNet.Entities.Enumerations;
using CloseIoDotNet.Rest.Entities.Requests.Queries;
using CloseIoDotNet.Rest.Entities.Requests.Scans;
using CloseIoDotNet.Rest.Entities.Responses.Enumerables;
Expand Down Expand Up @@ -91,6 +92,11 @@ public void Dispose()

public IEnumerable<T> Scan<T>() where T : IEntityScannable, new()
{
if (ValidateScanTypeSupported<T>(ScanType.Base) == false)
{
throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan.");
}

var scanRequest = Factory.Create<IScanRequest<T>, ScanRequest<T>>();
scanRequest.ApiKey = ApiKey;

Expand All @@ -105,6 +111,11 @@ public void Dispose()
throw new ArgumentException("searchQuery cannot be null, empty, or whitespace.", nameof(searchQuery));
}

if (ValidateScanTypeSupported<T>(ScanType.Query) == false)
{
throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan.");
}

var scanRequest = Factory.Create<IScanRequest<T>, ScanRequest<T>>();
scanRequest.ApiKey = ApiKey;
scanRequest.SearchQuery = searchQuery;
Expand Down Expand Up @@ -136,6 +147,11 @@ public IEnumerable<T> Scan<T>(string searchQuery, IEnumerable<IEntityField> fiel
throw new ArgumentException("All fields must be a member of the entity being scanned.", nameof(fields));
}

if (ValidateScanTypeSupported<T>(ScanType.Query) == false || ValidateScanTypeSupported<T>(ScanType.Fields) == false)
{
throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan.");
}

var scanRequest = Factory.Create<IScanRequest<T>, ScanRequest<T>>();
scanRequest.ApiKey = ApiKey;
scanRequest.SearchQuery = searchQuery;
Expand All @@ -162,6 +178,11 @@ public IEnumerable<T> Scan<T>(string searchQuery, IEnumerable<IEntityField> fiel
throw new ArgumentException("All fields must be a member of the entity being scanned.", nameof(fields));
}

if (ValidateScanTypeSupported<T>(ScanType.Fields) == false)
{
throw new InvalidOperationException($"Entity of type {typeof (T).Name} does not support this type of scan.");
}

var scanRequest = Factory.Create<IScanRequest<T>, ScanRequest<T>>();
scanRequest.ApiKey = ApiKey;
scanRequest.Fields = fields;
Expand All @@ -170,5 +191,12 @@ public IEnumerable<T> Scan<T>(string searchQuery, IEnumerable<IEntityField> fiel
return result;
}
#endregion

#region Methods
private static bool ValidateScanTypeSupported<T>(ScanType scanType) where T : IEntityScannable, new()
{
return new T().ScanTypesSupported.Contains(scanType);
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CloseIoDotNet.Entities.Enumerations;
using Emails;
using Fields;
using Newtonsoft.Json;
Expand All @@ -13,6 +14,10 @@ public class Contact : AEntity<Contact>, IEntityQueryable, IEntityScannable
{
#region Constants
private const string ScanResource = "contact/";
private static readonly IEnumerable<ScanType> ScanTypes = new []
{
ScanType.Base
};
#endregion

#region Instance Variables
Expand Down Expand Up @@ -102,10 +107,12 @@ public IEnumerable<IEntityField> ScannableFields
get
{
var result = new List<IEntityField>();
result.AddRange(EntityFields);
return result;
}
}

[JsonIgnore]
public IEnumerable<ScanType> ScanTypesSupported => ScanTypes;
#endregion

#region Methods - Interface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace CloseIoDotNet.Entities.Definitions
{
using System.Collections.Generic;
using CloseIoDotNet.Entities.Enumerations;
using Fields;

public interface IEntityScannable : IEntity
{
IEnumerable<IEntityField> ScannableFields { get; }
IEnumerable<ScanType> ScanTypesSupported { get; }
string GenerateScanResource();
}
}
10 changes: 10 additions & 0 deletions Libraries/CloseIoDotNet/Entities/Definitions/Lead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using Addresses;
using CloseIoDotNet.Entities.Enumerations;
using Contacts;
using Contacts.Emails;
using Contacts.Phones;
Expand All @@ -16,6 +17,12 @@ public class Lead : AEntity<Lead>, IEntityQueryable, IEntityScannable
{
#region Constants
private const string ScanResource = "lead/";
private static readonly IEnumerable<ScanType> ScanTypes = new []
{
ScanType.Base,
ScanType.Fields,
ScanType.Query
};
#endregion

#region Instance Variables
Expand Down Expand Up @@ -154,6 +161,9 @@ public IEnumerable<IEntityField> ScannableFields
return result;
}
}

[JsonIgnore]
public IEnumerable<ScanType> ScanTypesSupported => ScanTypes;
#endregion

#region Methods - Interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
{
using System;
using System.Collections.Generic;
using System.Linq;
using Addresses;
using Contacts;
using Contacts.Emails;
using Contacts.Phones;
using Contacts.Urls;
using CloseIoDotNet.Entities.Enumerations;
using Fields;
using Newtonsoft.Json;
using Tasks;

public class Opportunity : AEntity<Opportunity>, IEntityQueryable, IEntityScannable
{
#region Constants
private const string ScanResource = "opportunity/";
private static readonly IEnumerable<ScanType> ScanTypes = new []
{
ScanType.Base,
ScanType.Query,
ScanType.Fields
};
#endregion

#region Instance Variables
Expand Down Expand Up @@ -175,10 +175,12 @@ public IEnumerable<IEntityField> ScannableFields
return result;
}
}

[JsonIgnore]
public IEnumerable<ScanType> ScanTypesSupported => ScanTypes;
#endregion

#region Methods - Interface

public string GenerateQueryResource(string id)
{
return BaseEntityQueryable.GenerateQueryResource(id);
Expand Down
8 changes: 8 additions & 0 deletions Libraries/CloseIoDotNet/Entities/Definitions/Tasks/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
{
using System;
using System.Collections.Generic;
using CloseIoDotNet.Entities.Enumerations;
using Fields;
using Newtonsoft.Json;

public class Task : AEntity<Task>, IEntityQueryable, IEntityScannable
{
#region Constants
private const string ScanResource = "task/";
private static readonly IEnumerable<ScanType> ScanTypes = new []
{
ScanType.Base
};
#endregion

#region Instance Variables
Expand Down Expand Up @@ -126,6 +131,9 @@ public IEnumerable<IEntityField> ScannableFields
return result;
}
}

[JsonIgnore]
public IEnumerable<ScanType> ScanTypesSupported => ScanTypes;
#endregion

#region Methods - Interface
Expand Down
9 changes: 9 additions & 0 deletions Libraries/CloseIoDotNet/Entities/Enumerations/ScanType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CloseIoDotNet.Entities.Enumerations
{
public enum ScanType
{
Base = 0,
Fields,
Query
}
}
71 changes: 71 additions & 0 deletions Libraries/CloseIoDotNet/Rest/Exceptions/CloseIoRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace CloseIoDotNet.Rest.Exceptions
{
using System;
using System.Net;
using RestSharp;

public class CloseIoRequestException : Exception
{
#region Constants
private const string DefaultMessage =
"Close.Io returned an unknown error to your request";
#endregion

#region Instance Variables
private HttpStatusCode? _statusCode;
private string _body;
#endregion

#region Properties
public IRestRequest RestRequest { get; set; }
public IRestResponse RestResponse { get; set; }
public HttpStatusCode ResponseStatusCode
{
get
{
if (_statusCode.HasValue == false)
{
try
{
_statusCode = RestResponse.StatusCode;
}
catch (Exception)
{
_statusCode = new HttpStatusCode();
}
}
return _statusCode.Value;
}
set { _statusCode = value; }
}
public string ResponseBody
{
get
{
if (_body == null)
{
try
{
_body = RestResponse.Content;
}
catch (Exception)
{
_body = string.Empty;
}
}
return _body;
}
set { _body = value; }
}

#endregion

#region Constructors
public CloseIoRequestException() : base(DefaultMessage) { }

public CloseIoRequestException(string message) : base(message) { }

public CloseIoRequestException(string message, Exception innerException) : base(message, innerException) { }
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace CloseIoDotNet.Rest.Exceptions
{
using System;
public class InternalServerErrorException : Exception
{
#region Constants
public const string DefaultMessage =
"Close.Io encountered an unexpected internal server error (HTTP 500) while processing your request.";
#endregion

#region Constructors
public InternalServerErrorException() : base(DefaultMessage) { }

public InternalServerErrorException(string message) : base(message) { }

public InternalServerErrorException(string message, Exception innerException) : base(message, innerException) { }
#endregion
}
}
Loading

0 comments on commit ee44c8c

Please sign in to comment.