Skip to content

Commit

Permalink
Merge pull request #139 from cronofy/availability-rules
Browse files Browse the repository at this point in the history
Add support for the availability rules APIs
  • Loading branch information
Nevett authored Oct 9, 2024
2 parents ca60afe + b8f4284 commit 4f47319
Show file tree
Hide file tree
Showing 15 changed files with 1,301 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/Cronofy.Example/Cronofy.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
88 changes: 88 additions & 0 deletions src/Cronofy.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static void Main(string[] args)
AttachmentsExample();
return;
}
else if (args.Any(t => t == "availability-rules"))
{
AvailabilityRulesExample();
return;
}

Console.Write("Enter access token: ");
var accessToken = Console.ReadLine();
Expand Down Expand Up @@ -241,6 +246,89 @@ private static void AttachmentsExample()
Console.ReadLine();
}

/// <summary>
/// Availability rules usage example.
/// </summary>
private static void AvailabilityRulesExample()
{
Console.Write("Enter access token: ");
var accessToken = Console.ReadLine();

Console.WriteLine();
var client = new CronofyAccountClient(accessToken);

FetchAndPrintCalendars(client);

Console.Write("Enter calendar ID: ");
var calendarId = Console.ReadLine();
Console.WriteLine();

Console.WriteLine("Creating an example availability rule");
Console.WriteLine();

const string AvailabilityRuleId = "CSharpExampleAvailabilityRule";

client.UpsertAvailabilityRule(new AvailabilityRule
{
AvailabilityRuleId = AvailabilityRuleId,
TimeZoneId = "America/Chicago",
CalendarIds = new[] { calendarId },
WeeklyPeriods = new[]
{
new AvailabilityRule.WeeklyPeriod
{
Day = DayOfWeek.Monday,
StartTime = "09:30",
EndTime = "12:30",
},
new AvailabilityRule.WeeklyPeriod
{
Day = DayOfWeek.Monday,
StartTime = "14:00",
EndTime = "17:00",
},
new AvailabilityRule.WeeklyPeriod
{
Day = DayOfWeek.Wednesday,
StartTime = "09:30",
EndTime = "12:30",
},
},
});

Console.WriteLine("Availability rule created");
Console.WriteLine();

Console.WriteLine("Fetching created availability rule...");
var availabilityRule = client.GetAvailabilityRule(AvailabilityRuleId);

Console.WriteLine();
Console.WriteLine(availabilityRule.ToString());
Console.WriteLine();

Console.WriteLine("Fetching all availability rules...");
var availabilityRules = client.GetAvailabilityRules();

Console.WriteLine();

foreach (var rule in availabilityRules)
{
Console.WriteLine(rule.ToString());
}

Console.WriteLine();

Console.WriteLine("Press enter to delete the example rule...");
Console.ReadLine();

client.DeleteAvailabilityRule(AvailabilityRuleId);
Console.WriteLine("Availability rule deleted");
Console.WriteLine();

Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}

/// <summary>
/// Fetches a list of all of the users calendars and prints a summary to the console.
/// </summary>
Expand Down
181 changes: 181 additions & 0 deletions src/Cronofy/AvailabilityRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
namespace Cronofy
{
using System;
using System.Linq;

/// <summary>
/// Class for representing an availability rule.
/// </summary>
public sealed class AvailabilityRule
{
/// <summary>
/// Gets or sets the unique identifier of the availability rule.
/// </summary>
/// <value>
/// The unique identifier of the availability rule.
/// </value>
public string AvailabilityRuleId { get; set; }

/// <summary>
/// Gets or sets the time zone for which the availability rule start and end times are represented in.
/// </summary>
/// <value>
/// The time zone for which the availability rule start and end times are represented in.
/// </value>
public string TimeZoneId { get; set; }

/// <summary>
/// Gets or sets the calendars that should impact the user's availability.
/// </summary>
/// <value>
/// The calendars that should impact the user's availability.
/// </value>
public string[] CalendarIds { get; set; }

/// <summary>
/// Gets or sets the weekly recurring periods for the availability rule.
/// </summary>
/// <value>
/// The weekly recurring periods for the availability rule.
/// </value>
public WeeklyPeriod[] WeeklyPeriods { get; set; }

/// <inheritdoc />
public override int GetHashCode()
{
return this.AvailabilityRuleId.GetHashCode();
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj is AvailabilityRule && this.Equals((AvailabilityRule)obj);
}

/// <summary>
/// Determines whether the specified <see cref="AvailabilityRule"/>
/// is equal to the current <see cref="AvailabilityRule"/>.
/// </summary>
/// <param name="other">
/// The <see cref="AvailabilityRule"/> to compare with the current
/// <see cref="AvailabilityRule"/>.
/// </param>
/// <returns>
/// <c>true</c> if the specified <see cref="AvailabilityRule"/> is
/// equal to the current <see cref="AvailabilityRule"/>; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(AvailabilityRule other)
{
return this.AvailabilityRuleId == other.AvailabilityRuleId &&
this.TimeZoneId == other.TimeZoneId &&
((this.CalendarIds == null && other.CalendarIds == null) || (this.CalendarIds != null && other.CalendarIds != null && this.CalendarIds.SequenceEqual(other.CalendarIds))) &&
this.WeeklyPeriods.SequenceEqual(other.WeeklyPeriods);
}

/// <inheritdoc/>
public override string ToString()
{
return string.Format(
"<{0} AvailabilityRuleId={1}, TimeZoneId={2}, CalendarIds={3}, WeeklyPeriods=[{4}]>",
this.GetType(),
this.AvailabilityRuleId,
this.TimeZoneId,
this.CalendarIds == null ? "null" : string.Format("[{0}]", string.Join(", ", this.CalendarIds)),
string.Join(", ", this.WeeklyPeriods.Select(weeklyPeriod => weeklyPeriod.ToString())));
}

/// <summary>
/// Class to represent a weekly period.
/// </summary>
public class WeeklyPeriod
{
/// <summary>
/// Gets or sets the week day the period applies to.
/// </summary>
/// <value>
/// The week day the period applies to.
/// </value>
public DayOfWeek Day { get; set; }

/// <summary>
/// Gets or sets the time of day the period should start.
/// </summary>
/// <value>
/// The time of day the period should start.
/// </value>
public string StartTime { get; set; }

/// <summary>
/// Gets or sets the time of day the period should end.
/// </summary>
/// <value>
/// The time of day the period should end.
/// </value>
public string EndTime { get; set; }

/// <inheritdoc />
public override int GetHashCode()
{
return this.Day.GetHashCode() ^ this.StartTime.GetHashCode() ^ this.EndTime.GetHashCode();
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

return obj is WeeklyPeriod && this.Equals((WeeklyPeriod)obj);
}

/// <summary>
/// Determines whether the specified <see cref="WeeklyPeriod"/>
/// is equal to the current <see cref="WeeklyPeriod"/>.
/// </summary>
/// <param name="other">
/// The <see cref="WeeklyPeriod"/> to compare with the current
/// <see cref="WeeklyPeriod"/>.
/// </param>
/// <returns>
/// <c>true</c> if the specified <see cref="WeeklyPeriod"/> is
/// equal to the current <see cref="WeeklyPeriod"/>; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(WeeklyPeriod other)
{
return this.Day == other.Day &&
this.StartTime == other.StartTime &&
this.EndTime == other.EndTime;
}

/// <inheritdoc/>
public override string ToString()
{
return string.Format(
"<{0} Day={1}, StartTime={2}, EndTime={3}>",
this.GetType(),
this.Day,
this.StartTime,
this.EndTime);
}
}
}
}
63 changes: 63 additions & 0 deletions src/Cronofy/CronofyAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,69 @@ public string GetConferencingServiceAuthorizationUrl(ConferencingServiceAuthoriz
return response.AuthorizationRequest.Url;
}

/// <inheritdoc/>
public AvailabilityRule GetAvailabilityRule(string availabilityRuleId)
{
Preconditions.NotEmpty(nameof(availabilityRuleId), availabilityRuleId);

var request = new HttpRequest();

request.Method = "GET";
request.Url = string.Format(this.UrlProvider.AvailabilityRuleUrl, availabilityRuleId);
request.AddOAuthAuthorization(this.AccessToken);

var response = this.HttpClient.GetJsonResponse<GetAvailabilityRuleResponse>(request);

return response.AvailabilityRule.ToAvailabilityRule();
}

/// <inheritdoc/>
public IEnumerable<AvailabilityRule> GetAvailabilityRules()
{
var request = new HttpRequest();

request.Method = "GET";
request.Url = this.UrlProvider.AvailabilityRulesUrl;
request.AddOAuthAuthorization(this.AccessToken);

var response = this.HttpClient.GetJsonResponse<ListAvailabilityRulesResponse>(request);

return response.AvailabilityRules.Select(ap => ap.ToAvailabilityRule());
}

/// <inheritdoc/>
public AvailabilityRule UpsertAvailabilityRule(AvailabilityRule availabilityRule)
{
Preconditions.NotNull(nameof(availabilityRule), availabilityRule);

var request = new HttpRequest();

request.Method = "POST";
request.Url = this.UrlProvider.AvailabilityRulesUrl;
request.AddOAuthAuthorization(this.AccessToken);

var upsertAvailabilityRuleRequest = UpsertAvailabilityRuleRequest.FromAvailabilityRule(availabilityRule);
request.SetJsonBody(upsertAvailabilityRuleRequest);

var response = this.HttpClient.GetJsonResponse<UpsertAvailabilityRuleResponse>(request);

return response.AvailabilityRule.ToAvailabilityRule();
}

/// <inheritdoc/>
public void DeleteAvailabilityRule(string availabilityRuleId)
{
Preconditions.NotEmpty(nameof(availabilityRuleId), availabilityRuleId);

var request = new HttpRequest();

request.Method = "DELETE";
request.Url = string.Format(this.UrlProvider.AvailabilityRuleUrl, availabilityRuleId);
request.AddOAuthAuthorization(this.AccessToken);

this.HttpClient.GetValidResponse(request);
}

/// <summary>
/// Creates a calendar.
/// </summary>
Expand Down
Loading

0 comments on commit 4f47319

Please sign in to comment.