Google Api

This commit is contained in:
Paul Schneider 2017-06-20 02:47:52 +02:00
commit 83f9fc1bd8
123 changed files with 12961 additions and 60 deletions

View file

@ -23,28 +23,27 @@ using System;
using System.Net;
using System.IO;
using System.Web;
using Yavsc.Models.Auth;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
namespace Yavsc.Models.Google.Calendar
{
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Models.Google;
using Yavsc.Helpers;
using Yavsc.Models.Calendar;
using Yavsc.ViewModels.Calendar;
/// <summary>
/// Google Calendar API client.
/// </summary>
public class CalendarManager : ICalendarManager
{
// protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
protected static string scopeCalendar = "https://www.googleapis.com/auth/calendar";
private string _ApiKey;
private readonly UserManager<ApplicationUser> _userManager;
@ -82,26 +81,34 @@ namespace Yavsc.Models.Google.Calendar
/// </summary>
private string timeZone = "+01:00";
private readonly IDataStore dataStore = new FileDataStore(GoogleWebAuthorizationBroker.Folder);
/// <summary>
/// Gets the calendar list.
/// </summary>
/// <returns>The calendars.</returns>
/// <param name="cred">Cred.</param>
/// <param name="userId">Yavsc user id</param>
public async Task<CalendarList> GetCalendarsAsync (string userId)
{
UserCredential creds = await _userManager.GetCredentialForGoogleApiAsync(
_dbContext, userId);
if (creds==null)
throw new InvalidOperationException("No credential");
CalendarList res = null;
HttpWebRequest webreq = WebRequest.CreateHttp (getCalListUri);
webreq.Headers.Add (HttpRequestHeader.Authorization, creds.GetHeader());
var login = await _userManager.GetGoogleUserLoginAsync(_dbContext,userId);
var token = await _dbContext.GetTokensAsync(login.ProviderKey);
if (token==null)
throw new InvalidOperationException("No Google token");
HttpWebRequest webreq = WebRequest.CreateHttp(getCalListUri);
webreq.Headers.Add("Authorization", "Bearer "+ token.AccessToken);
webreq.Method = "GET";
webreq.ContentType = "application/http";
using (WebResponse resp = webreq.GetResponse ()) {
using (Stream respstream = resp.GetResponseStream ()) {
using (var rdr = new StreamReader(respstream)) {
res = JsonConvert.DeserializeObject<CalendarList>(rdr.ReadToEnd());
string json = rdr.ReadToEnd();
_logger.LogInformation(">> Json calendar list : "+json);
res = JsonConvert.DeserializeObject<CalendarList>(json);
}
}
resp.Close ();
@ -119,12 +126,14 @@ namespace Yavsc.Models.Google.Calendar
/// <param name="mindate">Mindate.</param>
/// <param name="maxdate">Maxdate.</param>
/// <param name="cred">credential string.</param>
public async Task<CalendarEventList> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate,string userId)
public async Task<CalendarEventList> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate)
{
UserCredential creds = await _userManager.GetCredentialForGoogleApiAsync(
_dbContext, userId);
// ServiceAccountCredential screds = new ServiceAccountCredential(init);
var creds = GoogleHelpers.GetCredentialForApi(new string[]{scopeCalendar});
if (creds==null)
throw new InvalidOperationException("No credential");
if (string.IsNullOrWhiteSpace (calid))
throw new Exception ("the calendar identifier is not specified");
@ -136,7 +145,7 @@ namespace Yavsc.Models.Google.Calendar
HttpWebRequest webreq = WebRequest.CreateHttp (uri);
webreq.Headers.Add (HttpRequestHeader.Authorization, creds.GetHeader());
webreq.Headers.Add (HttpRequestHeader.Authorization, "Bearer "+ await creds.GetAccessTokenForRequestAsync());
webreq.Method = "GET";
webreq.ContentType = "application/http";
CalendarEventList res = null;
@ -169,7 +178,7 @@ namespace Yavsc.Models.Google.Calendar
string inputId,
string calid, DateTime mindate, DateTime maxdate, string userId)
{
var eventList = await GetCalendarAsync(calid, mindate, maxdate, userId);
var eventList = await GetCalendarAsync(calid, mindate, maxdate);
return new DateTimeChooserViewModel {
InputId = inputId,

View file

@ -18,6 +18,7 @@
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Threading.Tasks;
using Yavsc.Helpers;
using Yavsc.Models.Google;
@ -53,13 +54,13 @@ namespace Yavsc.GoogleApis
/// </summary>
/// <returns>The entity.</returns>
/// <param name="entities">Entities.</param>
public static string [] CreateEntity( Entity[] entities ) {
public static async Task<string []> CreateEntity( Entity[] entities ) {
string [] ans = null;
using (SimpleJsonPostMethod wr =
new SimpleJsonPostMethod (googleMapTracksPath + "entities/create"))
{
ans = wr.Invoke<string[]> (entities);
ans = await wr.Invoke<string[]> (entities);
}
return ans;
}
@ -69,13 +70,13 @@ namespace Yavsc.GoogleApis
/// </summary>
/// <returns>The entities.</returns>
/// <param name="eq">Eq.</param>
static Entity[] ListEntities (EntityQuery eq)
static async Task <Entity[]> ListEntities (EntityQuery eq)
{
Entity [] ans = null;
using (SimpleJsonPostMethod wr =
new SimpleJsonPostMethod (googleMapTracksPath + "entities/create"))
{
ans = wr.Invoke <Entity[]> (eq);
ans = await wr.Invoke <Entity[]> (eq);
}
return ans;
}

View file

@ -34,6 +34,6 @@ namespace Yavsc.Models.Calendar
/// </summary>
public interface ICalendarManager {
Task<CalendarList> GetCalendarsAsync (string userId);
Task<CalendarEventList> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate, string userId);
Task<CalendarEventList> GetCalendarAsync (string calid, DateTime mindate, DateTime maxdate);
}
}

View file

@ -30,30 +30,18 @@ namespace Yavsc.Services
/// </returns>
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, RdvQueryEvent ev)
{
MessageWithPayloadResponse response = null;
await Task.Run(()=>{
response = googleSettings.NotifyEvent<RdvQueryEvent>(registrationIds, ev);
});
return response;
return await googleSettings.NotifyEvent<RdvQueryEvent>(registrationIds, ev);
}
public async Task<MessageWithPayloadResponse> NotifyEstimateAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, EstimationEvent ev)
{
MessageWithPayloadResponse response = null;
await Task.Run(()=>{
response = googleSettings.NotifyEvent<EstimationEvent>(registrationIds, ev);
});
return response;
return await googleSettings.NotifyEvent<EstimationEvent>(registrationIds, ev);
}
public async Task<MessageWithPayloadResponse> NotifyHairCutQueryAsync(GoogleAuthSettings googleSettings,
IEnumerable<string> registrationIds, HairCutQueryEvent ev)
{
MessageWithPayloadResponse response = null;
await Task.Run(()=>{
response = googleSettings.NotifyEvent<HairCutQueryEvent>(registrationIds, ev);
});
return response;
return await googleSettings.NotifyEvent<HairCutQueryEvent>(registrationIds, ev);
}
public Task<bool> SendEmailAsync(SiteSettings siteSettings, SmtpSettings smtpSettings, string email, string subject, string message)