Skip to content

Commit 4f47319

Browse files
authored
Merge pull request #139 from cronofy/availability-rules
Add support for the availability rules APIs
2 parents ca60afe + b8f4284 commit 4f47319

15 files changed

+1301
-127
lines changed

src/Cronofy.Example/Cronofy.Example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<PropertyGroup>
2525
<OutputType>Exe</OutputType>
26-
<TargetFramework>netcoreapp3.1</TargetFramework>
26+
<TargetFramework>net7</TargetFramework>
2727
<IsPackable>false</IsPackable>
2828
</PropertyGroup>
2929

src/Cronofy.Example/Program.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static void Main(string[] args)
3434
AttachmentsExample();
3535
return;
3636
}
37+
else if (args.Any(t => t == "availability-rules"))
38+
{
39+
AvailabilityRulesExample();
40+
return;
41+
}
3742

3843
Console.Write("Enter access token: ");
3944
var accessToken = Console.ReadLine();
@@ -241,6 +246,89 @@ private static void AttachmentsExample()
241246
Console.ReadLine();
242247
}
243248

249+
/// <summary>
250+
/// Availability rules usage example.
251+
/// </summary>
252+
private static void AvailabilityRulesExample()
253+
{
254+
Console.Write("Enter access token: ");
255+
var accessToken = Console.ReadLine();
256+
257+
Console.WriteLine();
258+
var client = new CronofyAccountClient(accessToken);
259+
260+
FetchAndPrintCalendars(client);
261+
262+
Console.Write("Enter calendar ID: ");
263+
var calendarId = Console.ReadLine();
264+
Console.WriteLine();
265+
266+
Console.WriteLine("Creating an example availability rule");
267+
Console.WriteLine();
268+
269+
const string AvailabilityRuleId = "CSharpExampleAvailabilityRule";
270+
271+
client.UpsertAvailabilityRule(new AvailabilityRule
272+
{
273+
AvailabilityRuleId = AvailabilityRuleId,
274+
TimeZoneId = "America/Chicago",
275+
CalendarIds = new[] { calendarId },
276+
WeeklyPeriods = new[]
277+
{
278+
new AvailabilityRule.WeeklyPeriod
279+
{
280+
Day = DayOfWeek.Monday,
281+
StartTime = "09:30",
282+
EndTime = "12:30",
283+
},
284+
new AvailabilityRule.WeeklyPeriod
285+
{
286+
Day = DayOfWeek.Monday,
287+
StartTime = "14:00",
288+
EndTime = "17:00",
289+
},
290+
new AvailabilityRule.WeeklyPeriod
291+
{
292+
Day = DayOfWeek.Wednesday,
293+
StartTime = "09:30",
294+
EndTime = "12:30",
295+
},
296+
},
297+
});
298+
299+
Console.WriteLine("Availability rule created");
300+
Console.WriteLine();
301+
302+
Console.WriteLine("Fetching created availability rule...");
303+
var availabilityRule = client.GetAvailabilityRule(AvailabilityRuleId);
304+
305+
Console.WriteLine();
306+
Console.WriteLine(availabilityRule.ToString());
307+
Console.WriteLine();
308+
309+
Console.WriteLine("Fetching all availability rules...");
310+
var availabilityRules = client.GetAvailabilityRules();
311+
312+
Console.WriteLine();
313+
314+
foreach (var rule in availabilityRules)
315+
{
316+
Console.WriteLine(rule.ToString());
317+
}
318+
319+
Console.WriteLine();
320+
321+
Console.WriteLine("Press enter to delete the example rule...");
322+
Console.ReadLine();
323+
324+
client.DeleteAvailabilityRule(AvailabilityRuleId);
325+
Console.WriteLine("Availability rule deleted");
326+
Console.WriteLine();
327+
328+
Console.WriteLine("Press enter to continue...");
329+
Console.ReadLine();
330+
}
331+
244332
/// <summary>
245333
/// Fetches a list of all of the users calendars and prints a summary to the console.
246334
/// </summary>

src/Cronofy/AvailabilityRule.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
namespace Cronofy
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
/// <summary>
7+
/// Class for representing an availability rule.
8+
/// </summary>
9+
public sealed class AvailabilityRule
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier of the availability rule.
13+
/// </summary>
14+
/// <value>
15+
/// The unique identifier of the availability rule.
16+
/// </value>
17+
public string AvailabilityRuleId { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the time zone for which the availability rule start and end times are represented in.
21+
/// </summary>
22+
/// <value>
23+
/// The time zone for which the availability rule start and end times are represented in.
24+
/// </value>
25+
public string TimeZoneId { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets the calendars that should impact the user's availability.
29+
/// </summary>
30+
/// <value>
31+
/// The calendars that should impact the user's availability.
32+
/// </value>
33+
public string[] CalendarIds { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the weekly recurring periods for the availability rule.
37+
/// </summary>
38+
/// <value>
39+
/// The weekly recurring periods for the availability rule.
40+
/// </value>
41+
public WeeklyPeriod[] WeeklyPeriods { get; set; }
42+
43+
/// <inheritdoc />
44+
public override int GetHashCode()
45+
{
46+
return this.AvailabilityRuleId.GetHashCode();
47+
}
48+
49+
/// <inheritdoc/>
50+
public override bool Equals(object obj)
51+
{
52+
if (ReferenceEquals(null, obj))
53+
{
54+
return false;
55+
}
56+
57+
if (ReferenceEquals(this, obj))
58+
{
59+
return true;
60+
}
61+
62+
return obj is AvailabilityRule && this.Equals((AvailabilityRule)obj);
63+
}
64+
65+
/// <summary>
66+
/// Determines whether the specified <see cref="AvailabilityRule"/>
67+
/// is equal to the current <see cref="AvailabilityRule"/>.
68+
/// </summary>
69+
/// <param name="other">
70+
/// The <see cref="AvailabilityRule"/> to compare with the current
71+
/// <see cref="AvailabilityRule"/>.
72+
/// </param>
73+
/// <returns>
74+
/// <c>true</c> if the specified <see cref="AvailabilityRule"/> is
75+
/// equal to the current <see cref="AvailabilityRule"/>; otherwise,
76+
/// <c>false</c>.
77+
/// </returns>
78+
public bool Equals(AvailabilityRule other)
79+
{
80+
return this.AvailabilityRuleId == other.AvailabilityRuleId &&
81+
this.TimeZoneId == other.TimeZoneId &&
82+
((this.CalendarIds == null && other.CalendarIds == null) || (this.CalendarIds != null && other.CalendarIds != null && this.CalendarIds.SequenceEqual(other.CalendarIds))) &&
83+
this.WeeklyPeriods.SequenceEqual(other.WeeklyPeriods);
84+
}
85+
86+
/// <inheritdoc/>
87+
public override string ToString()
88+
{
89+
return string.Format(
90+
"<{0} AvailabilityRuleId={1}, TimeZoneId={2}, CalendarIds={3}, WeeklyPeriods=[{4}]>",
91+
this.GetType(),
92+
this.AvailabilityRuleId,
93+
this.TimeZoneId,
94+
this.CalendarIds == null ? "null" : string.Format("[{0}]", string.Join(", ", this.CalendarIds)),
95+
string.Join(", ", this.WeeklyPeriods.Select(weeklyPeriod => weeklyPeriod.ToString())));
96+
}
97+
98+
/// <summary>
99+
/// Class to represent a weekly period.
100+
/// </summary>
101+
public class WeeklyPeriod
102+
{
103+
/// <summary>
104+
/// Gets or sets the week day the period applies to.
105+
/// </summary>
106+
/// <value>
107+
/// The week day the period applies to.
108+
/// </value>
109+
public DayOfWeek Day { get; set; }
110+
111+
/// <summary>
112+
/// Gets or sets the time of day the period should start.
113+
/// </summary>
114+
/// <value>
115+
/// The time of day the period should start.
116+
/// </value>
117+
public string StartTime { get; set; }
118+
119+
/// <summary>
120+
/// Gets or sets the time of day the period should end.
121+
/// </summary>
122+
/// <value>
123+
/// The time of day the period should end.
124+
/// </value>
125+
public string EndTime { get; set; }
126+
127+
/// <inheritdoc />
128+
public override int GetHashCode()
129+
{
130+
return this.Day.GetHashCode() ^ this.StartTime.GetHashCode() ^ this.EndTime.GetHashCode();
131+
}
132+
133+
/// <inheritdoc/>
134+
public override bool Equals(object obj)
135+
{
136+
if (ReferenceEquals(null, obj))
137+
{
138+
return false;
139+
}
140+
141+
if (ReferenceEquals(this, obj))
142+
{
143+
return true;
144+
}
145+
146+
return obj is WeeklyPeriod && this.Equals((WeeklyPeriod)obj);
147+
}
148+
149+
/// <summary>
150+
/// Determines whether the specified <see cref="WeeklyPeriod"/>
151+
/// is equal to the current <see cref="WeeklyPeriod"/>.
152+
/// </summary>
153+
/// <param name="other">
154+
/// The <see cref="WeeklyPeriod"/> to compare with the current
155+
/// <see cref="WeeklyPeriod"/>.
156+
/// </param>
157+
/// <returns>
158+
/// <c>true</c> if the specified <see cref="WeeklyPeriod"/> is
159+
/// equal to the current <see cref="WeeklyPeriod"/>; otherwise,
160+
/// <c>false</c>.
161+
/// </returns>
162+
public bool Equals(WeeklyPeriod other)
163+
{
164+
return this.Day == other.Day &&
165+
this.StartTime == other.StartTime &&
166+
this.EndTime == other.EndTime;
167+
}
168+
169+
/// <inheritdoc/>
170+
public override string ToString()
171+
{
172+
return string.Format(
173+
"<{0} Day={1}, StartTime={2}, EndTime={3}>",
174+
this.GetType(),
175+
this.Day,
176+
this.StartTime,
177+
this.EndTime);
178+
}
179+
}
180+
}
181+
}

src/Cronofy/CronofyAccountClient.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,69 @@ public string GetConferencingServiceAuthorizationUrl(ConferencingServiceAuthoriz
560560
return response.AuthorizationRequest.Url;
561561
}
562562

563+
/// <inheritdoc/>
564+
public AvailabilityRule GetAvailabilityRule(string availabilityRuleId)
565+
{
566+
Preconditions.NotEmpty(nameof(availabilityRuleId), availabilityRuleId);
567+
568+
var request = new HttpRequest();
569+
570+
request.Method = "GET";
571+
request.Url = string.Format(this.UrlProvider.AvailabilityRuleUrl, availabilityRuleId);
572+
request.AddOAuthAuthorization(this.AccessToken);
573+
574+
var response = this.HttpClient.GetJsonResponse<GetAvailabilityRuleResponse>(request);
575+
576+
return response.AvailabilityRule.ToAvailabilityRule();
577+
}
578+
579+
/// <inheritdoc/>
580+
public IEnumerable<AvailabilityRule> GetAvailabilityRules()
581+
{
582+
var request = new HttpRequest();
583+
584+
request.Method = "GET";
585+
request.Url = this.UrlProvider.AvailabilityRulesUrl;
586+
request.AddOAuthAuthorization(this.AccessToken);
587+
588+
var response = this.HttpClient.GetJsonResponse<ListAvailabilityRulesResponse>(request);
589+
590+
return response.AvailabilityRules.Select(ap => ap.ToAvailabilityRule());
591+
}
592+
593+
/// <inheritdoc/>
594+
public AvailabilityRule UpsertAvailabilityRule(AvailabilityRule availabilityRule)
595+
{
596+
Preconditions.NotNull(nameof(availabilityRule), availabilityRule);
597+
598+
var request = new HttpRequest();
599+
600+
request.Method = "POST";
601+
request.Url = this.UrlProvider.AvailabilityRulesUrl;
602+
request.AddOAuthAuthorization(this.AccessToken);
603+
604+
var upsertAvailabilityRuleRequest = UpsertAvailabilityRuleRequest.FromAvailabilityRule(availabilityRule);
605+
request.SetJsonBody(upsertAvailabilityRuleRequest);
606+
607+
var response = this.HttpClient.GetJsonResponse<UpsertAvailabilityRuleResponse>(request);
608+
609+
return response.AvailabilityRule.ToAvailabilityRule();
610+
}
611+
612+
/// <inheritdoc/>
613+
public void DeleteAvailabilityRule(string availabilityRuleId)
614+
{
615+
Preconditions.NotEmpty(nameof(availabilityRuleId), availabilityRuleId);
616+
617+
var request = new HttpRequest();
618+
619+
request.Method = "DELETE";
620+
request.Url = string.Format(this.UrlProvider.AvailabilityRuleUrl, availabilityRuleId);
621+
request.AddOAuthAuthorization(this.AccessToken);
622+
623+
this.HttpClient.GetValidResponse(request);
624+
}
625+
563626
/// <summary>
564627
/// Creates a calendar.
565628
/// </summary>

0 commit comments

Comments
 (0)