/// <summary> /// Syncronize data between keepzer and the service /// </summary> /// <param name="context"></param> /// <param name="fullSync">True if a full sync should be run</param> /// <exception cref="Keepzer.Data.Exceptions.ClientException">Thrown when no access token is available</exception> public void Sync(ServiceContext context, Boolean fullSync) { OpenAuth2 authObject = context.DataClient.GetSingletonData(context.UserAppId, PublicDataTypes.OpenAuth2, new OpenAuth2()); // not authorized if the access token is empty if (String.IsNullOrEmpty(authObject.AccessToken)) { throw new ClientException(ClientError.Forbidden); } LastFmUserProfile profileData = GetProfileData(context); // figure out whether to do a full sync or a partial sync // do full sync on first of each month /*String[] categories = * { * PublicDataTypes.HealthBodySteps, * PublicDataTypes.HealthBodyCalories, * PublicDataTypes.MovementWalking, * PublicDataTypes.MovementRunning, * PublicDataTypes.MovementCycling, * PublicDataTypes.MovementDriving * }; * DateTime startDate = GetLastEntryDate(context, categories) ?? profileData.FirstDate; * if (fullSync || startDate < profileData.FirstDate) * startDate = profileData.FirstDate; * GetDailySummaryData(context, startDate);*/ }
/// <summary> /// Get the user profile /// </summary> private LastFmUserProfile GetProfileData(ServiceContext context) { RestHelper rest = new RestHelper(""); HttpWebRequest request = HttpWebRequest.Create(rest.GetProfileUri()) as HttpWebRequest; AuthorizeRequest(context, request); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { // deserialize and save the user profile data LastFmUserProfile userProfile = LastFmUserProfile.Deserialize(streamReader); context.DataClient.UpdateSingletonData(context.UserAppId, LastFmDataTypes.UserProfile, new LocalDate(userProfile.RegisteredDate), userProfile); return(userProfile); } }