Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating and attaching files to events #135

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 121 additions & 23 deletions src/Cronofy.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;

/// <summary>
/// Example program for interacting with the Cronofy API.
Expand All @@ -28,36 +29,20 @@ public static void Main(string[] args)
AddToCalendarExample();
return;
}
else if (args.Any(t => t == "attachments"))
{
AttachmentsExample();
return;
}

Console.Write("Enter access token: ");
var accessToken = Console.ReadLine();

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

Console.WriteLine("Fetching calendars...");
var calendars = client.GetCalendars();

Console.WriteLine();

foreach (var calendar in calendars)
{
Console.WriteLine("{0} - {1} - {2} ({3})", calendar.CalendarId, calendar.Name, calendar.Profile.Name, calendar.Profile.ProviderName);
}

Console.WriteLine();

Console.WriteLine("Fetching events...");
var events = client.GetEvents();

Console.WriteLine();

foreach (var evt in events)
{
Console.WriteLine("{0} - {1}", evt.Start, evt.Summary);
}

Console.WriteLine();
FetchAndPrintCalendars(client);
FetchAndPrintEvents(client);

const string EventId = "CronofyExample";

Expand Down Expand Up @@ -180,5 +165,118 @@ private static void RealTimeSchedulingExample()

Process.Start(actualUrl);
}

/// <summary>
/// Attachments usage example.
/// </summary>
private static void AttachmentsExample()
{
Console.Write("Enter Client id: ");
var clientId = Console.ReadLine();
Console.Write("Enter Secret: ");
var clientSecret = Console.ReadLine();

Console.WriteLine();
var oaClient = new CronofyOAuthClient(clientId, clientSecret);

Console.Write("Enter access token: ");
var accessToken = Console.ReadLine();

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

Console.WriteLine("Creating an example attachment");
Console.WriteLine();

var attachmentContent = Encoding.ASCII.GetBytes("Example file content");

var attachment = oaClient.CreateAttachment(new Requests.CreateAttachmentRequest
{
Attachment = new Requests.CreateAttachmentRequest.AttachmentSummary
{
FileName = "example.txt",
ContentType = "text/plain",
Base64Content = Convert.ToBase64String(attachmentContent),
},
});

Console.WriteLine("Attachment created");
Console.WriteLine();

FetchAndPrintCalendars(client);

const string EventId = "CronofyAttachmentExample";

Console.WriteLine("Creating event with ID {0}", EventId);
Console.WriteLine();

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

var tomorrow = DateTime.Today.AddDays(1);
var start = tomorrow.AddHours(17);
var end = start.AddMinutes(30);

var eventBuilder = new UpsertEventRequestBuilder()
.EventId(EventId)
.Summary("Cronofy Attachment Example")
.Description("Attachment example from the Cronofy .NET SDK")
.AddAttachment(attachment.AttachmentId)
.Start(start)
.End(end);

client.UpsertEvent(calendarId, eventBuilder);
Console.WriteLine("Event upserted");
Console.WriteLine();

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

client.DeleteEvent(calendarId, EventId);
Console.WriteLine("Event 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>
/// <param name="client">Account client to use to read the list of calendars.</param>
private static void FetchAndPrintCalendars(CronofyAccountClient client)
{
Console.WriteLine("Fetching calendars...");
var calendars = client.GetCalendars();

Console.WriteLine();

foreach (var calendar in calendars)
{
Console.WriteLine("{0} - {1} - {2} ({3})", calendar.CalendarId, calendar.Name, calendar.Profile.Name, calendar.Profile.ProviderName);
}

Console.WriteLine();
}

/// <summary>
/// Fetches a list of all of the users events and prints a summary to the console.
/// </summary>
/// <param name="client">Account client to use to read the list of events.</param>
private static void FetchAndPrintEvents(CronofyAccountClient client)
{
Console.WriteLine("Fetching events...");
var events = client.GetEvents();

Console.WriteLine();

foreach (var evt in events)
{
Console.WriteLine("{0} - {1}", evt.Start, evt.Summary);
}

Console.WriteLine();
}
}
}
94 changes: 94 additions & 0 deletions src/Cronofy/Attachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace Cronofy
{
using Newtonsoft.Json;

/// <summary>
/// Class for the deserialization of an attachment summary
/// response.
/// </summary>
public sealed class Attachment
{
/// <summary>
/// Gets or sets the attachment ID.
/// </summary>
/// <value>
/// The ID of the attachment.
/// </value>
public string AttachmentId { get; set; }

/// <summary>
/// Gets or sets the file name for the attachment.
/// </summary>
/// <value>
/// The file name for the attachment.
/// </value>
public string FileName { get; set; }

/// <summary>
/// Gets or sets the MIME content type for the attachment.
/// </summary>
/// <value>
/// The MIME content type for the attachment.
/// </value>
public string ContentType { get; set; }

/// <summary>
/// Gets or sets the MD5 hash of the attachment content.
/// </summary>
/// <value>
/// The MD5 hash of the attachment content.
/// </value>
public string MD5 { get; set; }

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

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

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

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

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

/// <inheritdoc/>
public override string ToString()
{
return string.Format(
"<{0} AttachmentId={1}, FileName={2}>",
this.GetType(),
this.AttachmentId,
this.FileName);
}
}
}
23 changes: 23 additions & 0 deletions src/Cronofy/CronofyOAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,29 @@ public ElementToken GetElementToken(ElementTokenRequest elementTokenRequest)
return elementTokenResponse.ToElementToken();
}

/// <inheritdoc/>
public Attachment CreateAttachment(CreateAttachmentRequest createAttachmentRequest)
{
Preconditions.NotNull(nameof(createAttachmentRequest), createAttachmentRequest);
Preconditions.NotNull(nameof(createAttachmentRequest.Attachment), createAttachmentRequest.Attachment);
Preconditions.NotEmpty(nameof(createAttachmentRequest.Attachment.FileName), createAttachmentRequest.Attachment.FileName);
Preconditions.NotEmpty(nameof(createAttachmentRequest.Attachment.ContentType), createAttachmentRequest.Attachment.ContentType);
Preconditions.NotEmpty(nameof(createAttachmentRequest.Attachment.Base64Content), createAttachmentRequest.Attachment.Base64Content);

var request = new HttpRequest
{
Method = "POST",
Url = this.urlProvider.AttachmentsUrl,
};

request.AddOAuthAuthorization(this.clientSecret);
request.SetJsonBody(createAttachmentRequest);

var attachmentResponse = this.HttpClient.GetJsonResponse<AttachmentResponse>(request);

return attachmentResponse.ToAttachment();
}

/// <summary>
/// Builder class for authorization URLs.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Cronofy/ICronofyOAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,19 @@ public interface ICronofyOAuthClient
/// Thrown if <paramref name="redirectUri"/> is null or empty.
/// </exception>
IAuthorizationUrlBuilder GetEnterpriseConnectAuthorizationUrlBuilder(string redirectUri);

/// <summary>
/// Creates a new attachment that can be added to an event as part of an upsert operation.
/// </summary>
/// <param name="createAttachmentRequest">
/// The details of the attachment to be created.
/// </param>
/// <returns>
/// The created attachment.
/// </returns>
/// <exception cref="System.ArgumentException">
/// Thrown if <paramref name="createAttachmentRequest"/> is null or empty.
/// </exception>
Attachment CreateAttachment(CreateAttachmentRequest createAttachmentRequest);
}
}
Loading
Loading