Skip to content

Commit

Permalink
Refactor GetCalendarService to accept GoogleCalendarSettings directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Edrisym committed Nov 3, 2024
1 parent f61e6db commit 3b81724
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Services/GoogleCalendarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<Event> CreateEventAsync(EventModel model)
{
var newEvent = MakeAnEvent(model);
//TODO -- to asynchronous
var service = _oAuthService.GetCalendarService(_settings);
var service = _oAuthService.GetCalendarService(_settings.Value);

var eventRequest = service.Events.Insert(newEvent, _settings.Value.CalendarId);

Expand All @@ -83,7 +83,7 @@ public async Task<Event> CreateEventAsync(EventModel model)

private async Task<Event?> GetEventByIdAsync(string eventId)
{
var service = _oAuthService.GetCalendarService(_settings);
var service = _oAuthService.GetCalendarService(_settings.Value);

var events = await service.Events.List(_settings.Value.CalendarId).ExecuteAsync();

Expand All @@ -102,7 +102,7 @@ public async Task<Event> CreateEventAsync(EventModel model)

try
{
var service = _oAuthService.GetCalendarService(_settings);
var service = _oAuthService.GetCalendarService(_settings.Value);

var request = service.Events.Update(madeEvent, _settings.Value.CalendarId, eventId);
request.SendNotifications = true;
Expand Down
2 changes: 1 addition & 1 deletion Services/IOAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public interface IOAuthService
{
Task<JObject> CredentialsFileAsync();
Task<JObject> TokenFileAsync();
CalendarService GetCalendarService(IOptionsSnapshot<GoogleCalendarSettings> calendarSetting);
CalendarService GetCalendarService(GoogleCalendarSettings calendarSetting);
}
13 changes: 7 additions & 6 deletions Services/OAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ public async Task<JObject> TokenFileAsync()
}


public CalendarService GetCalendarService(IOptionsSnapshot<GoogleCalendarSettings> calendarSetting)
public CalendarService GetCalendarService(GoogleCalendarSettings calendarSetting)
{
var secrets = new ClientSecrets
{
ClientId = calendarSetting.Value.ClientId,
ClientSecret = calendarSetting.Value.ClientSecret
ClientId = calendarSetting.ClientId,
ClientSecret = calendarSetting.ClientSecret
};

var token = new TokenResponse { RefreshToken = calendarSetting.Value.RefreshToken };
var token = new TokenResponse { RefreshToken = calendarSetting.RefreshToken };

var flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
});

var credential = new UserCredential(flow, calendarSetting.Value.User, token);

var credential = new UserCredential(flow, calendarSetting.User, token);

var services = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = calendarSetting.Value.ApplicationName,
ApplicationName = calendarSetting.ApplicationName,
});
return services;
}
Expand Down

0 comments on commit 3b81724

Please sign in to comment.