Skip to content

Commit

Permalink
Authenticate to google cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Jcardif committed Sep 10, 2024
1 parent db94f1b commit c80a7b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Functions/DeleteCalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun

return response;
}

// Authenticate to Google Cloud Console
var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME);
var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile);

// Check if the token was acquired successfully
if (googleCalendarService.CalendarService is null)
if (calendarService is null)
{
// get response from helper method
response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized,
Expand Down
16 changes: 10 additions & 6 deletions src/Functions/SyncNewEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CalendarSync.Functions;
public class SyncNewEvents(ILoggerFactory loggerFactory, AppDbContext context, GoogleCalendarService googleCalendarService, KeyVaultService keyVaultService)
{
private readonly ILogger _logger = loggerFactory.CreateLogger<SyncNewEvents>();
private List<CalendarEvent>? calendarEvents;
private List<CalendarEvent>? _calendarEvents;

[Function("SyncNewEvents")]
public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
Expand All @@ -37,10 +37,10 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun
{
if (bodyType == "list")
{
calendarEvents = JsonConvert.DeserializeObject<List<CalendarEvent>>(requestBody);
_calendarEvents = JsonConvert.DeserializeObject<List<CalendarEvent>>(requestBody);

// check if list is null or empty
if (calendarEvents is null || calendarEvents.Count == 0)
if (_calendarEvents is null || _calendarEvents.Count == 0)
{
// get response from helper method
response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.BadRequest, "No events were passed");
Expand All @@ -62,7 +62,7 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun
return response;
}

calendarEvents = new List<CalendarEvent> { calendarEvent };
_calendarEvents = new List<CalendarEvent> { calendarEvent };
}
}

Expand All @@ -75,8 +75,12 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun
return response;
}

// Authenticate to Google Cloud Console
var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME);
var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile);

// Check if the token was acquired successfully
if (googleCalendarService.CalendarService is null)
if (calendarService is null)
{
// get response from helper method
response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized,
Expand All @@ -88,7 +92,7 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun
// Use EF Core to insert a record into the table
var cEvents = new List<object>();

foreach (var calendarEvent in calendarEvents)
foreach (var calendarEvent in _calendarEvents)
{
var existingCalendarEvent = context?.CalendarEvents?.Find(calendarEvent.Id);
if (existingCalendarEvent != null)
Expand Down
6 changes: 5 additions & 1 deletion src/Functions/UpdateCalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Fun

return response;
}

// Authenticate to Google Cloud Console
var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME);
var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile);

// Check if the token was acquired successfully
if (googleCalendarService.CalendarService is null)
if (calendarService is null)
{
// get response from helper method
response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized,
Expand Down

0 comments on commit c80a7b5

Please sign in to comment.