/// <summary> /// Gets a list of activities, that match specified parameters, associated with this user's Microsoft Health profile. /// </summary> /// <param name="startTime">Filters the set of returned activities to those starting after the specified <see cref="DateTime"/>, inclusive.</param> /// <param name="endTime">Filters the set of returned activities to those starting before the specified <see cref="DateTime"/>, exclusive. </param> /// <param name="ids">The comma-separated list of activity ids to return.</param> /// <param name="type">The <see cref="MSHealthActivityType"/> to return (supports multi-values).</param> /// <param name="include">The <see cref="MSHealthActivityInclude"/> properties to return: Details, MinuteSummaries, MapPoints (supports multi-values).</param> /// <param name="deviceIds">Filters the set of returned activities based on the comma-separated list of device ids provided.</param> /// <param name="splitDistanceType">The length of splits (<see cref="MSHealthSplitDistanceType"/>) used in each activity.</param> /// <param name="maxPageSize">The maximum number of entries to return per page.</param> /// <returns>Instance of <see cref="MSHealthActivities"/> with collection of activities that matched specified parameters.</returns> public MSHealthActivities ListActivities(DateTime? startTime = default(DateTime?), DateTime? endTime = default(DateTime?), string ids = null, MSHealthActivityType type = MSHealthActivityType.Unknown, MSHealthActivityInclude include = MSHealthActivityInclude.None, string deviceIds = null, MSHealthSplitDistanceType splitDistanceType = MSHealthSplitDistanceType.None, int? maxPageSize = default(int?)) { MSHealthActivities loActivities = null; var loQuery = new StringBuilder(); string lsResponse; string lsParamValue; // Check StartTime, and append to query if applies if (startTime != null && startTime.HasValue) loQuery.AppendFormat("&startTime={0}", Uri.EscapeDataString(startTime.Value.ToUniversalTime().ToString("O"))); // Check EndTime, and append to query if applies if (endTime != null && endTime.HasValue) loQuery.AppendFormat("&endTime={0}", Uri.EscapeDataString(endTime.Value.ToUniversalTime().ToString("O"))); // Check ActivityIds, and append to query if applies if (!string.IsNullOrEmpty(ids)) loQuery.AppendFormat("&activityIds={0}", Uri.EscapeDataString(ids)); // Check ActivityTypes, and append to query if applies if (type != MSHealthActivityType.Unknown) { lsParamValue = string.Empty; if (type.HasFlag(MSHealthActivityType.Custom)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Custom); if (type.HasFlag(MSHealthActivityType.CustomExercise)) lsParamValue += string.Format(",{0}", MSHealthActivityType.CustomExercise); if (type.HasFlag(MSHealthActivityType.CustomComposite)) lsParamValue += string.Format(",{0}", MSHealthActivityType.CustomComposite); if (type.HasFlag(MSHealthActivityType.Run)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Run); if (type.HasFlag(MSHealthActivityType.Sleep)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Sleep); if (type.HasFlag(MSHealthActivityType.FreePlay)) lsParamValue += string.Format(",{0}", MSHealthActivityType.FreePlay); if (type.HasFlag(MSHealthActivityType.GuidedWorkout)) lsParamValue += string.Format(",{0}", MSHealthActivityType.GuidedWorkout); if (type.HasFlag(MSHealthActivityType.Bike)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Bike); if (type.HasFlag(MSHealthActivityType.Golf)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Golf); if (type.HasFlag(MSHealthActivityType.RegularExercise)) lsParamValue += string.Format(",{0}", MSHealthActivityType.RegularExercise); if (type.HasFlag(MSHealthActivityType.Hike)) lsParamValue += string.Format(",{0}", MSHealthActivityType.Hike); lsParamValue = lsParamValue.TrimStart(new char[] { ',' }); loQuery.AppendFormat("&activityTypes={0}", lsParamValue); } // Check ActivityIncludes, and append to query if applies if (include != MSHealthActivityInclude.None) { lsParamValue = string.Empty; if (include.HasFlag(MSHealthActivityInclude.Details)) lsParamValue += string.Format(",{0}", MSHealthActivityInclude.Details); if (include.HasFlag(MSHealthActivityInclude.MinuteSummaries)) lsParamValue += string.Format(",{0}", MSHealthActivityInclude.MinuteSummaries); if (include.HasFlag(MSHealthActivityInclude.MapPoints)) lsParamValue += string.Format(",{0}", MSHealthActivityInclude.MapPoints); lsParamValue = lsParamValue.TrimStart(new char[] { ',' }); loQuery.AppendFormat("&activityIncludes={0}", lsParamValue); } // Check DeviceIds, and append to query if applies if (!string.IsNullOrEmpty(deviceIds)) loQuery.AppendFormat("&deviceIds={0}", Uri.EscapeDataString(deviceIds)); // Check SplitDistanceType, and append to query if applies switch (splitDistanceType) { case MSHealthSplitDistanceType.Mile: loQuery.AppendFormat("&splitDistanceType={0}", MSHealthSplitDistanceType.Mile); break; case MSHealthSplitDistanceType.Kilometer: loQuery.AppendFormat("&splitDistanceType={0}", MSHealthSplitDistanceType.Kilometer); break; } // Check MaxPageSize, and append to query if applies if (maxPageSize != null && maxPageSize.HasValue && maxPageSize.Value > 0) loQuery.AppendFormat("&maxPageSize={0}", maxPageSize.Value); // Perform request using BASE_URI, ACTIVITIES_PATH and query string lsResponse = PerformRequest(ACTIVITIES_PATH, loQuery.ToString().TrimStart(new char[] { '&' })); // Deserialize Json response (use Converters for Enum, DateTime and TimeSpan values) var loSerializerSettings = new JsonSerializerSettings(); loSerializerSettings.Converters.Add(new StringEnumConverter()); loSerializerSettings.Converters.Add(new IsoDateTimeConverter()); loSerializerSettings.Converters.Add(new TimeSpanConverter()); loActivities = JsonConvert.DeserializeObject<MSHealthActivities>(lsResponse, loSerializerSettings); return loActivities; }