Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kononov committed Mar 12, 2014
2 parents 45932bd + 51225e5 commit d6a0ab9
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/IronSharp.IronMQ/Alert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace IronSharp.IronMQ
{
public class Alert
{
public Alert() : this(AlertType.Fixed, AlertDirection.Asc, null, null)
{
}

public Alert(AlertType type, AlertDirection direction, int? trigger, string queue)
{
Type = type;
Direction = direction;
Trigger = trigger;
Queue = queue;
}

[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Id { get; set; }

[JsonProperty("snooze", DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? Snooze { get; set; }

[JsonProperty("queue", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Queue { get; set; }

[JsonProperty("trigger", DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? Trigger { get; set; }

[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Include)]
public AlertType Type { get; set; }

[JsonProperty("direction", DefaultValueHandling = DefaultValueHandling.Include)]
public AlertDirection Direction { get; set; }

[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Status { get; set; }

[JsonProperty("status_code", DefaultValueHandling = DefaultValueHandling.Ignore)]
protected int? Code { get; set; }

[JsonIgnore]
public HttpStatusCode StatusCode
{
get { return (HttpStatusCode) Code.GetValueOrDefault(200); }
}
}
}
36 changes: 36 additions & 0 deletions src/IronSharp.IronMQ/AlertCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Threading;
using IronSharp.Core;
using Newtonsoft.Json;

namespace IronSharp.IronMQ
{
public class AlertCollection : IInspectable
{
[JsonProperty("alerts", DefaultValueHandling = DefaultValueHandling.Ignore)] private List<Alert> _alerts;

public AlertCollection()
{
}

public AlertCollection(Alert alert) : this(new[] {alert})
{
}

public AlertCollection(IEnumerable<Alert> alerts)
{
foreach (Alert alert in alerts)
{
Alerts.Add(alert);
}
}

[JsonIgnore]
public List<Alert> Alerts
{
get { return LazyInitializer.EnsureInitialized(ref _alerts); }
set { _alerts = value; }
}
}
}
20 changes: 20 additions & 0 deletions src/IronSharp.IronMQ/AlertDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace IronSharp.IronMQ
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AlertDirection
{
[EnumMember(Value = "asc")]
Asc,
[EnumMember(Value = "desc")]
Desc
}
}
20 changes: 20 additions & 0 deletions src/IronSharp.IronMQ/AlertType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace IronSharp.IronMQ
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AlertType
{
[EnumMember(Value = "fixed")]
Fixed,
[EnumMember(Value = "progressive")]
Progressive
}
}
5 changes: 5 additions & 0 deletions src/IronSharp.IronMQ/IronSharp.IronMQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(SolutionDir)/SolutionInfo.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Alert.cs" />
<Compile Include="AlertCollection.cs" />
<Compile Include="AlertType.cs" />
<Compile Include="AlertDirection.cs" />
<Compile Include="Client.cs" />
<Compile Include="IronMqCloudHosts.cs" />
<Compile Include="IronMqRestClient.cs" />
Expand Down
69 changes: 69 additions & 0 deletions src/IronSharp.IronMQ/QueueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,75 @@ private MessageCollection LinkMessageCollection(RestResponse<MessageCollection>

#endregion

#region Alerts

/// <summary>
/// Add alerts to a queue. This is for Pull Queues only.
/// </summary>
/// <remarks>
/// http://dev.iron.io/mq/reference/api/#add_alerts_to_a_queue
/// http://dev.iron.io/mq/reference/queue_alerts/
/// </remarks>
public QueueInfo AddAlerts(AlertCollection alertCollection)
{
return RestClient.Post<QueueInfo>(_client.Config, string.Format("{0}/alerts", EndPoint), alertCollection);
}

/// <summary>
/// Update queue alerts. This is for Pull Queues only.
/// </summary>
/// <remarks>
/// http://dev.iron.io/mq/reference/api/#update_alerts_to_a_queue
/// http://dev.iron.io/mq/reference/queue_alerts/
/// </remarks>
public QueueInfo UpdateAlerts(AlertCollection alertCollection)
{
return RestClient.Put<QueueInfo>(_client.Config, string.Format("{0}/alerts", EndPoint), alertCollection);
}

/// <summary>
/// Removes an alert from the queue.
/// See http://dev.iron.io/mq/reference/queue_alerts/ for more information.
/// </summary>
/// <param name="alert"> Alert object to delete. </param>
/// <remarks>
/// http://dev.iron.io/mq/reference/api/#remove_alert_from_a_queue_by_id
/// </remarks>
public bool DeleteAlert(Alert alert)
{
if (alert == null)
return false;
return DeleteAlert(alert.Id);
}

/// <summary>
/// Removes an alert specified by id from the queue.
/// See http://dev.iron.io/mq/reference/queue_alerts/ for more information.
/// </summary>
/// <param name="alert"> Id of alert to delete. </param>
/// <remarks>
/// http://dev.iron.io/mq/reference/api/#remove_alert_from_a_queue_by_id
/// </remarks>
public bool DeleteAlert(string alertId)
{
if (String.IsNullOrEmpty(alertId))
return false;
return RestClient.Delete<ResponseMsg>(_client.Config, string.Format("{0}/alerts/{1}", EndPoint, alertId)).HasExpectedMessage("Deleted");
}

/// <summary>
/// Removes alerts from a queue. This is for Pull Queues only.
/// </summary>
/// <remarks>
/// http://dev.iron.io/mq/reference/api/#remove_alerts_from_a_queue
/// </remarks>
public QueueInfo RemoveAlerts(AlertCollection alertCollection)
{
return RestClient.Delete<QueueInfo>(_client.Config, string.Format("{0}/alerts", EndPoint), payload: alertCollection);
}

#endregion

#region Subscribers

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion src/IronSharp.IronMQ/QueueInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ namespace IronSharp.IronMQ
{
public class QueueInfo : IInspectable
{
[JsonProperty("subscribers", DefaultValueHandling = DefaultValueHandling.Ignore)] private List<Subscriber> _subscribers;
[JsonProperty("subscribers", DefaultValueHandling = DefaultValueHandling.Ignore)]
private List<Subscriber> _subscribers;

[JsonProperty("alerts", DefaultValueHandling = DefaultValueHandling.Ignore)]
private List<Alert> _alerts;

[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Id { get; set; }
Expand Down Expand Up @@ -74,5 +78,11 @@ public List<Subscriber> Subscribers
get { return LazyInitializer.EnsureInitialized(ref _subscribers); }
set { _subscribers = value; }
}

public List<Alert> Alerts
{
get { return LazyInitializer.EnsureInitialized(ref _alerts); }
set { _alerts = value; }
}
}
}

0 comments on commit d6a0ab9

Please sign in to comment.