Skip to content

Commit

Permalink
Support availability rule APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
CronofyMatt committed Oct 8, 2024
1 parent 011e926 commit c348d2c
Show file tree
Hide file tree
Showing 11 changed files with 916 additions and 126 deletions.
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.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,
this.WeeklyPeriods);
}

/// <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 string 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);
}
}
}
}
61 changes: 61 additions & 0 deletions src/Cronofy/CronofyAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,67 @@ public string GetConferencingServiceAuthorizationUrl(ConferencingServiceAuthoriz
return response.AuthorizationRequest.Url;
}

/// <inheritdoc/>
public AvailabilityRule GetAvailabilityRule(string availabilityRuleId)
{
Preconditions.NotEmpty("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<AvailabilityRuleResponse>(request);

return response.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(UpsertAvailabilityRuleRequest upsertAvailabilityRuleRequest)
{
Preconditions.NotNull("upsertAvailabilityRuleRequest", upsertAvailabilityRuleRequest);

var request = new HttpRequest();

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

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

return response.ToAvailabilityRule();
}

/// <inheritdoc/>
public void DeleteAvailabilityRule(string availabilityRuleId)
{
Preconditions.NotEmpty("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
55 changes: 54 additions & 1 deletion src/Cronofy/ICronofyAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,64 @@ public interface ICronofyAccountClient : ICronofyUserInfoClient
/// </param>
/// <returns>The URL which the end-user should visit.</returns>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="conferencingServiceAuthorizationRequest"/> if null, or it doesn't contain a Redirect URI.
/// Thrown if <paramref name="conferencingServiceAuthorizationRequest"/> is null, or it doesn't contain a Redirect URI.
/// </exception>
/// <exception cref="CronofyException">
/// Thrown if an error is encountered whilst making the request.
/// </exception>
string GetConferencingServiceAuthorizationUrl(ConferencingServiceAuthorizationRequest conferencingServiceAuthorizationRequest);

/// <summary>
/// Reads a single availability rule.
/// </summary>
/// <param name="availabilityRuleId">
/// The unique identifier of the availability rule.
/// </param>
/// <returns>The availability rule.</returns>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="availabilityRuleId"/> is null, or empty.
/// </exception>
/// <exception cref="CronofyException">
/// Thrown if an error is encountered whilst making the request.
/// </exception>
AvailabilityRule GetAvailabilityRule(string availabilityRuleId);

/// <summary>
/// Retrieves all availability rules saved against an account.
/// </summary>
/// <returns>The list of all availability rules.</returns>
/// <exception cref="CronofyException">
/// Thrown if an error is encountered whilst making the request.
/// </exception>
IEnumerable<AvailabilityRule> GetAvailabilityRules();

/// <summary>
/// Creates or updates an availability rule.
/// </summary>
/// <param name="upsertAvailabilityRuleRequest">
/// The parameters for the request, must not be null.
/// </param>
/// <returns>The created or updated availability rule.</returns>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="upsertAvailabilityRuleRequest"/> is null.
/// </exception>
/// <exception cref="CronofyException">
/// Thrown if an error is encountered whilst making the request.
/// </exception>
AvailabilityRule UpsertAvailabilityRule(UpsertAvailabilityRuleRequest upsertAvailabilityRuleRequest);

/// <summary>
/// Deletes an availability rule for the authenticated account.
/// </summary>
/// <param name="availabilityRuleId">
/// The unique identifier of the availability rule.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="availabilityRuleId"/> is null, or empty.
/// </exception>
/// <exception cref="CronofyException">
/// Thrown if an error is encountered whilst making the request.
/// </exception>
void DeleteAvailabilityRule(string availabilityRuleId);
}
}
82 changes: 82 additions & 0 deletions src/Cronofy/Requests/UpsertAvailabilityRuleRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace Cronofy.Requests
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// Class for the serialization of an upsert availability rule request.
/// </summary>
public sealed class UpsertAvailabilityRuleRequest
{
/// <summary>
/// Gets or sets the unique identifier of the availability rule.
/// </summary>
/// <value>
/// The unique identifier of the availability rule.
/// </value>
[JsonProperty("availability_rule_id")]
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>
[JsonProperty("tzid")]
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>
[JsonProperty("calendar_ids")]
public IEnumerable<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>
[JsonProperty("weekly_periods")]
public IEnumerable<WeeklyPeriod> WeeklyPeriods { get; set; }

/// <summary>
/// Class for the serialization of a weekly period within an availability rule.
/// </summary>
public sealed class WeeklyPeriod
{
/// <summary>
/// Gets or sets the week day the period applies to.
/// </summary>
/// <value>
/// The week day the period applies to.
/// </value>
[JsonProperty("day")]
public string 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>
[JsonProperty("start_time")]
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>
[JsonProperty("end_time")]
public string EndTime { get; set; }
}
}
}
Loading

0 comments on commit c348d2c

Please sign in to comment.