Skip to content

Commit

Permalink
Attachments example
Browse files Browse the repository at this point in the history
  • Loading branch information
CronofyMatt committed Aug 12, 2024
1 parent 7bc6bfe commit b9c41a8
Showing 1 changed file with 110 additions and 23 deletions.
133 changes: 110 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,19 @@ 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);

const string EventId = "CronofyExample";

Expand Down Expand Up @@ -180,5 +164,108 @@ 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
{
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();

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();
}
}
}

0 comments on commit b9c41a8

Please sign in to comment.