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
17 changes: 17 additions & 0 deletions SnipeSharp/Attributes/EndpointObjectNotFoundMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace SnipeSharp.Attributes
{
/// <summary>
/// Since the SnipeIT Api uses inconsistent error string to note whether or not an object exists or not, we can use this attribute to declare it.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class EndpointObjectNotFoundMessage : Attribute
{
public string Message { get; private set; }
public EndpointObjectNotFoundMessage(string notFoundMessage)
{
Message = notFoundMessage;
}
}
}
50 changes: 39 additions & 11 deletions SnipeSharp/Endpoints/EndPointManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using SnipeSharp.Attributes;
using SnipeSharp.Common;
using SnipeSharp.Endpoints.Models;
using SnipeSharp.Endpoints.SearchFilters;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -15,6 +17,7 @@ public class EndPointManager<T> where T : CommonEndpointModel
{
protected IRequestManager _reqManager;
protected string _endPoint;
protected string _notFoundMessage;

/// <summary>
///
Expand All @@ -25,6 +28,11 @@ public EndPointManager(IRequestManager reqManager, string endPoint)
{
_reqManager = reqManager;
_endPoint = endPoint;
var attribute = typeof(T).GetCustomAttributes(typeof(EndpointObjectNotFoundMessage), true).FirstOrDefault() as EndpointObjectNotFoundMessage;
if(attribute != null)
{
_notFoundMessage = attribute.Message;
}
}

/// <summary>
Expand All @@ -40,9 +48,7 @@ public ResponseCollection<T> GetAll()
// If there are more than 1000 assets split up the requests to avoid timeouts
if (count.Total < 1000)
{
string response = _reqManager.Get(_endPoint);
ResponseCollection<T> results = JsonConvert.DeserializeObject<ResponseCollection<T>>(response);
return results;
return FindAll(new SearchFilter { Limit = (int) count.Total });

} else
{
Expand Down Expand Up @@ -81,8 +87,27 @@ public ResponseCollection<T> GetAll()
/// <returns></returns>
public ResponseCollection<T> FindAll(ISearchFilter filter)
{
string response = _reqManager.Get(_endPoint, filter);
ResponseCollection<T> results = JsonConvert.DeserializeObject<ResponseCollection<T>>(response);
var response = _reqManager.Get(_endPoint, filter);
var results = JsonConvert.DeserializeObject<ResponseCollection<T>>(response);

var baseOffset = filter.Offset == null ? 0 : filter.Offset;
// If there is no limit and there are more total than retrieved
if(filter.Limit == null && baseOffset + results.Rows.Count < results.Total)
{
filter.Limit = 1000;
filter.Offset = baseOffset + results.Rows.Count;

while (baseOffset + results.Rows.Count < results.Total)
{
response = _reqManager.Get(_endPoint, filter);
var batch = JsonConvert.DeserializeObject<ResponseCollection<T>>(response);

results.Rows.AddRange(batch.Rows);

filter.Offset += 1000;
}
}

return results;
}

Expand All @@ -95,7 +120,7 @@ public T FindOne(ISearchFilter filter)
{
string response = _reqManager.Get(_endPoint, filter);
ResponseCollection<T> result = JsonConvert.DeserializeObject<ResponseCollection<T>>(response);
return (result.Rows != null) ? result.Rows[0] : default(T);
return (result.Rows != null && result.Rows.Count > 0) ? result.Rows[0] : default(T);
}

/// <summary>
Expand All @@ -105,11 +130,14 @@ public T FindOne(ISearchFilter filter)
/// <returns></returns>
public T Get(int id)
{
// TODO: Find better way to deal with objects that are not found
T result;
string response = _reqManager.Get(string.Format("{0}/{1}", _endPoint, id.ToString()));
result = JsonConvert.DeserializeObject<T>(response);
return result;
var response = _reqManager.Get(string.Format("{0}/{1}", _endPoint, id.ToString()));
// Parse the response as a message to see if there's a result.
var message = JsonConvert.DeserializeObject<RequestResponse>(response);
// If there isn't a result, return default(T).
if(message.Status == "error" && message.Messages.ContainsKey("general") && message.Messages["general"] == _notFoundMessage)
return default(T);
// We do have one, so re-deserialize the response as the type we want.
return JsonConvert.DeserializeObject<T>(response);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Accessory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Accessory not found")]
public class Accessory : CommonEndpointModel
{
[JsonProperty("company")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace SnipeSharp.Endpoints.Models
{
// TODO: Make constructor that forces required fields
[EndpointObjectNotFoundMessage("Asset not found")]
public class Asset : CommonEndpointModel
{

Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Category not found")]
public class Category : CommonEndpointModel
{
[JsonProperty("image")]
Expand Down
4 changes: 3 additions & 1 deletion SnipeSharp/Endpoints/Models/Company.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using SnipeSharp.Common;
using SnipeSharp.Attributes;
using SnipeSharp.Common;
using Newtonsoft.Json;

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Company not found")]
public class Company : CommonEndpointModel
{
[JsonProperty("image")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Component not found")]
public class Component : CommonEndpointModel
{
[JsonProperty("serial_number")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Consumable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Consumable not found")]
public class Consumable : CommonEndpointModel
{
[JsonProperty("category")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Department.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Department not found")]
public class Department : CommonEndpointModel
{
[JsonProperty("company_id")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Depreciation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Depreciation not found")]
public class Depreciation : CommonEndpointModel
{

Expand Down
4 changes: 3 additions & 1 deletion SnipeSharp/Endpoints/Models/FieldSet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using SnipeSharp.Common;
using SnipeSharp.Attributes;
using SnipeSharp.Common;
using Newtonsoft.Json;

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Fieldset does not exist")]
public class FieldSet : CommonEndpointModel
{
[JsonProperty("fields")]
Expand Down
2 changes: 2 additions & 0 deletions SnipeSharp/Endpoints/Models/License.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using SnipeSharp.Attributes;
using SnipeSharp.Common;

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("License not found")]
public class License : CommonEndpointModel
{

Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Location not found")]
public class Location : CommonEndpointModel
{
[JsonProperty("image")]
Expand Down
2 changes: 1 addition & 1 deletion SnipeSharp/Endpoints/Models/Manufacturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SnipeSharp.Endpoints.Models
{


[EndpointObjectNotFoundMessage("Manufacturer not found")]
public class Manufacturer : CommonEndpointModel
{
[JsonProperty("url")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("AssetModel not found")]
public class Model : CommonEndpointModel
{

Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/StatusLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Statuslabel not found")]
public class StatusLabel : CommonEndpointModel
{
private string _type;
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/Supplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("Supplier not found")]
public class Supplier : CommonEndpointModel
{
[JsonProperty("name")]
Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/Endpoints/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace SnipeSharp.Endpoints.Models
{
[EndpointObjectNotFoundMessage("User not found")]
public class User : CommonEndpointModel
{

Expand Down
1 change: 1 addition & 0 deletions SnipeSharp/SnipeSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\EndpointObjectNotFoundMessage.cs" />
<Compile Include="Attributes\FilterParamName.cs" />
<Compile Include="Attributes\OptionalRequestHeader.cs" />
<Compile Include="Attributes\RequiredRequestHeader.cs" />
Expand Down