internal async Task <StreamResponse <T> > GetWithOptions <T>(GetOptions options = null) where T : Activity { // build request options = options ?? GetOptions.Default; var request = _client.BuildRequest(this, "/", Method.GET); options.Apply(request); // make request var response = await _client.MakeRequest(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw StreamException.FromResponse(response); } // handle response var result = new StreamResponse <T>(); JObject obj = JObject.Parse(response.Content); foreach (var prop in obj.Properties()) { switch (prop.Name) { case "results": case "activities": { // get the results var array = prop.Value as JArray; result.Results = array.Select(a => Activity.FromJson((JObject)a) as T).ToList(); break; } case "unseen": { result.Unseen = prop.Value.Value <long>(); break; } case "unread": { result.Unread = prop.Value.Value <long>(); break; } case "duration": { result.Duration = prop.Value.Value <String>(); break; } default: break; } } return(result); }
internal IEnumerable <Activity> GetResults(String json) { JObject obj = JObject.Parse(json); foreach (var prop in obj.Properties()) { if ((prop.Name == "results") || (prop.Name == "activities")) { // get the array var array = prop.Value as JArray; foreach (var val in array) { yield return(Activity.FromJson((JObject)val)); } } } }
/// <summary> /// Add an activity to the feed /// </summary> /// <param name="activity"></param> /// <returns>An activity with ID and Date supplied</returns> public async Task <Activity> AddActivity(Activity activity) { if (activity == null) { throw new ArgumentNullException("activity", "Must have an activity to add"); } var request = _client.BuildRequest(this, "/", Method.POST); request.AddParameter("application/json", activity.ToJson(this._client), ParameterType.RequestBody); var response = await _client.MakeRequest(request); if (response.StatusCode == System.Net.HttpStatusCode.Created) { return(Activity.FromJson(response.Content)); } throw StreamException.FromResponse(response); }
internal static UpdateToTargetsResponse FromJson(JObject obj) { var result = new UpdateToTargetsResponse(); foreach (var jsonProp in obj.Properties()) { switch (jsonProp.Name) { case "added": result.Added = jsonProp.Value.ToObject <List <string> >(); break; case "removed": result.Removed = jsonProp.Value.ToObject <List <string> >(); break; case "activity": result.Activity = Activity.FromJson(jsonProp.Value.ToObject <JObject>()); break; } } return(result); }