-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebhook.cs
141 lines (125 loc) · 5.03 KB
/
Webhook.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections;
using System.Collections.Generic;
namespace Cisco.Spark
{
/// <summary>
/// A Webhook allows notification (via HTTP) when a specific event occurs on Spark.
/// </summary>
public class Webhook : SparkObject
{
/// <summary>
/// The SparkType this SparkObject implementation represents.
/// </summary>
internal override SparkType SparkType
{
get { return SparkType.Webhook; }
}
/// <summary>
/// A user-friendly name for this Webhook.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The URL that receives POST requests for each event.
/// </summary>
public Uri Target { get; set; }
/// <summary>
/// The resource type for the Webhook. Creating a webhook requires 'read' scope on the resource the webhook is for.
/// </summary>
public SparkType Resource { get; set; }
/// <summary>
/// The event type for the Webhook.
/// </summary>
public string Event { get; set; }
/// <summary>
/// The filter that defines the webhook scope.
/// </summary>
public string Filter { get; set; }
/// <summary>
/// Secret used to generate payload signature.
/// </summary>
public string Secret { get; set; }
/// <summary>
/// Creates a Webhook from an existing Spark side Webhook.
/// </summary>
/// <param name="id">Spark UID of the Webhook.</param>
public static Webhook FromId(string id)
{
return (Webhook)SparkObjectFactory.Make(id, SparkType.Webhook);
}
public Webhook() { }
/// <summary>
/// Create a new Webhook locally.
/// </summary>
/// <param name="name">A user-friendly name for this Webhook.</param>
/// <param name="target">The URL that receives POST requests for each event.</param>
/// <param name="resource">The resource type for the Webhook. Creating a webhook requires 'read' scope on the resource the webhook is for.</param>
/// <param name="webhookEvent">The event type for the Webhook.</param>
/// <param name="filter">The filter that defines the webhook scope.</param>
/// <param name="secret">Secret used to generate payload signature.</param>
public Webhook(string name, Uri target, SparkType resource, string webhookEvent, string filter = null, string secret = null)
{
Name = name;
Target = target;
Resource = resource;
Event = webhookEvent;
Filter = filter;
Secret = secret;
}
/// <summary>
/// Returns a dictionary representation of the object.
/// </summary>
/// <param name="fields">A specific list of fields to serialise.</param>
/// <returns>The serialised object as a Dictionary.</returns>
protected override Dictionary<string, object> ToDict(List<string> fields)
{
var data = base.ToDict();
data["name"] = Name;
data["targetUrl"] = Target.AbsoluteUri;
data["targetUrl"] = Target.AbsoluteUri;
data["resource"] = Resource.GetEndpoint();
data["event"] = Event.ToString();
data["filter"] = Filter;
data["secret"] = Secret;
return CleanDict(data, fields);
}
/// <summary>
/// Populates the object with the given data.
/// </summary>
/// <param name="data">Dictionary of data to load.</param>
protected override void LoadDict(Dictionary<string, object> data)
{
base.LoadDict(data);
Name = data["name"] as string;
Target = new Uri(data["targetUrl"] as string);
Resource = SparkTypeExtensions.FromEndpoint(data["resource"] as string);
Event = data["event"] as string;
object filter;
if (data.TryGetValue("filter", out filter))
{
Filter = filter as string;
}
object secret;
if (data.TryGetValue("secret", out secret))
{
Secret = secret as string;
}
}
/// <summary>
/// List's Webhooks to which the authenticated user owns.
/// </summary>
/// <param name="error">Error from Spark, if any.</param>
/// <param name="results">List of Webhooks.</param>
/// <param name="max">Maximum number of Webhooks to retrieve.</param>
public static IEnumerator ListWebhooks(Action<SparkMessage> error, Action<List<Webhook>> results, int max = 0)
{
var constraints = new Dictionary<string, string>();
if (max > 0)
{
constraints.Add("max", max.ToString());
}
var listObjects = ListObjects(constraints, SparkType.Webhook, error, results);
yield return Request.Instance.StartCoroutine(listObjects);
}
}
}