-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPushTriggerController.cs
83 lines (69 loc) · 3.43 KB
/
PushTriggerController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using QuickLearn.SampleApi.Models;
using System.Web.Http;
using System;
using System.Threading.Tasks;
using TRex.Metadata;
using Swashbuckle.Swagger.Annotations;
using System.Net;
namespace QuickLearn.SampleApi.Controllers
{
[RoutePrefix("api/marketwatcher")]
public class PushTriggerController : ApiController
{
private static InMemoryCallbackStore<PriceAlertConfig> callbacks = new InMemoryCallbackStore<PriceAlertConfig>();
[HttpPost, Route("$subscriptions")]
[Metadata("Target Price Reached", "Fires whenever the target price of a given security is reached", VisibilityType.Important)]
[Trigger(TriggerType.Subscription, typeof(PriceAlert), "Price Alert")]
[SwaggerResponseRemoveDefaults]
[SwaggerResponse(HttpStatusCode.Created, "Subscription created")]
[SwaggerResponse(HttpStatusCode.BadRequest, "Invalid subscription configuration")]
public async Task<IHttpActionResult> Subscribe([FromBody]PriceAlertConfig config)
{
if (config.TargetPrice < 0)
{
BadRequest("Cannot subscribe for negative price alerts");
}
var triggerId = Guid.NewGuid().ToString("N");
await callbacks.WriteCallbackAsync(triggerId, new Uri(config.CallbackUrl), config);
return CreatedAtRoute(nameof(Unsubscribe), new { triggerId = triggerId }, string.Empty);
}
[HttpDelete, Route("$subscriptions/{triggerId}", Name = nameof(Unsubscribe))]
[Metadata("Unsubscribe", Visibility = VisibilityType.Internal)]
[SwaggerResponse(HttpStatusCode.OK)]
public async Task<IHttpActionResult> Unsubscribe(string triggerId)
{
await callbacks.DeleteCallbackAsync(triggerId);
return Ok();
}
[HttpPut, Route("{symbol}")]
[Metadata("PriceUpdate", Visibility = VisibilityType.Internal)]
[SwaggerResponse(HttpStatusCode.OK)]
public async Task<IHttpActionResult> PriceUpdate(string symbol, decimal newPrice)
{
// This operation can be used manually to trigger any matching configured price alerts
// Search the in-memory store of callbacks for price alert subscriptions matching
// the current price update
await Task.Run(async () =>
Parallel.ForEach(await callbacks.ReadCallbacksAsync(),
async c =>
{
// Ensure the symbol matches before comparing prices
if (c.Configuration.Symbol != symbol) return;
// Determine if the current price update is worth firing a trigger for
if ((c.Configuration.HigherIsBetter
&& newPrice >= c.Configuration.TargetPrice) ||
(!c.Configuration.HigherIsBetter
&& newPrice <= c.Configuration.TargetPrice))
{
// Fire the push trigger at the callback URL that we were provided
await c.InvokeAsync(new PriceAlert()
{
Symbol = symbol,
Price = newPrice
});
}
}));
return Ok();
}
}
}