private static byte[] GetGcmData(string userNameFromComment, IEnumerable<User> users, string message, Feeling feeling) { var jsonObject = new JObject(); var arr = new JArray(); users.Where(u => !string.IsNullOrWhiteSpace(u.Key)).ForEach(u => arr.Add(u.Key)); if (!arr.Any()) return null; jsonObject.Add("registration_ids", arr); var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var data = new JObject { {"message", message}, {"user", userNameFromComment}, {"feeling", JsonConvert.SerializeObject(feeling.Id, Formatting.Indented, jsonSerializerSettings)} }; jsonObject.Add("data", data); var postData = JsonConvert.SerializeObject(jsonObject); var byteArray = Encoding.UTF8.GetBytes(postData); return byteArray; }
/// <summary> /// Merges the donor array values into the receiver array /// </summary> /// <param name="receiver"></param> /// <param name="donor"></param> internal static void MergeJArrays(JArray receiver, JArray donor) { foreach (var item in donor) { if (!receiver.Any(x => x.Equals(item))) { receiver.Add(item); } } }
public Task StoreData(string tableName, JArray data, bool overwrite = true) { Debug.WriteLine("Storing data for table {0}", tableName); if (!data.Any()) { return completed; } EnsureDatabaseThread(); // the actual columns for the item IDictionary<string, Column> columns = this.GetColumnsFromItems(data); this.EnsureSchemaForTable(tableName, db, columns); try { Debug.WriteLine("Inserting..."); foreach (JObject item in data) { db.InsertIntoTable(tableName, columns, item, overwrite); } return completed; } catch (SQLiteException ex) { throw new Exception(string.Format("Error occurred during interaction with the local database: {0}", ex.Message)); } }
/// <summary> /// Get the max value of the key property. /// </summary> /// <param name="entries">The feed. (The collection/set of the entities.)</param> /// <param name="keyPropertyName">The name of the key property.</param> /// <returns>Returns the max value of the key property.</returns> private object GetMaxEntityKey(JArray entries, string keyPropertyName) { object value = null; if (null != entries && entries.Any()) { value = entries.First[keyPropertyName]; foreach (var entry in entries) { if (Convert.ToInt64(value) < (Int64)entry[keyPropertyName]) { value = entry[keyPropertyName]; } } } return value; }
private async Task TestIncrementalSync(MobileServiceTableQueryDescription query, JArray result, DateTime maxUpdatedAt, bool savesMax, string firstQuery, string secondQuery) { var action = new PullAction(this.table.Object, MobileServiceTableKind.Table, this.context.Object, "latestItems", query, null, null, this.opQueue.Object, this.settings.Object, this.store.Object, MobileServiceRemoteTableOptions.All, null, CancellationToken.None); this.opQueue.Setup(q => q.LockTableAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult<IDisposable>(null)); this.opQueue.Setup(q => q.CountPending(It.IsAny<string>())).Returns(Task.FromResult(0L)); this.table.Setup(t => t.ReadAsync(firstQuery, It.IsAny<IDictionary<string, string>>(), It.IsAny<MobileServiceFeatures>())) .Returns(Task.FromResult(QueryResult.Parse(result, null, false))); if (result.Any()) { this.table.Setup(t => t.ReadAsync(secondQuery, It.IsAny<IDictionary<string, string>>(), It.IsAny<MobileServiceFeatures>())) .Returns(Task.FromResult(QueryResult.Parse(new JArray(), null, false))); } if (result.Any()) { this.store.Setup(s => s.UpsertAsync("test", It.IsAny<IEnumerable<JObject>>(), true)).Returns(Task.FromResult(0)); } this.settings.Setup(s => s.GetDeltaTokenAsync("test", "latestItems")).Returns(Task.FromResult(new DateTimeOffset(2013, 1, 1, 0, 0, 0, TimeSpan.Zero))); if (savesMax) { this.settings.Setup(s => s.SetDeltaTokenAsync("test", "latestItems", maxUpdatedAt)).Returns(Task.FromResult(0)); } await action.ExecuteAsync(); this.store.VerifyAll(); this.opQueue.VerifyAll(); this.table.VerifyAll(); this.settings.VerifyAll(); store.Verify(s => s.DeleteAsync("test", It.IsAny<IEnumerable<string>>()), Times.Never(), "There shouldn't be any call to delete"); }
public void PutSale_Sale_CollectionNavigationProperty() { var location = default(string); var token = new JObject(); token["SalesPersonName"] = "sale person post"; token["CustomerName"] = "customer post"; var contact1 = new JObject(); contact1["Method"] = "Phone"; contact1["Value"] = "phone 1"; var contact2 = new JObject(); contact2["Method"] = "Phone"; contact2["Value"] = "phone 2"; var contacts = new JArray(); contacts.Add(contact1); contacts.Add(contact2); token["CustomerContacts"] = contacts; var transaction1 = new JObject(); transaction1["ProductName"] = "product 1"; transaction1["ProductDescription"] = "description 1"; transaction1["Quantity"] = 1; transaction1["UnitPrice"] = 100000; var transaction2 = new JObject(); transaction2["ProductName"] = "product 2"; transaction2["ProductDescription"] = "description 2"; transaction2["Quantity"] = 2; transaction2["UnitPrice"] = 200000; var transactions = new JArray(); transactions.Add(transaction1); transactions.Add(transaction2); token["Items"] = transactions; ODataClientHelper.InvokePost(string.Format("{0}Sales", SalesServiceRootUrl), token.ToString(Formatting.None), null, response => { location = response.Headers.Location.ToString(); return true; }); token = (JObject)ODataClientHelper.InvokeGet(location + "?$expand=CustomerContacts,Items"); token.Remove("@odata.context"); token.Remove("*****@*****.**"); token.Remove("*****@*****.**"); token["SalesPersonName"] = "new sale person"; contacts = (JArray)token["CustomerContacts"]; contacts[0]["Method"] = "QQ"; transactions = (JArray)token["Items"]; transactions[0]["ProductName"] = "new product"; ODataClientHelper.InvokePut( location, token.ToString(Formatting.None), client => { client.DefaultRequestHeaders.IfMatch.Add(EntityTagHeaderValue.Any); }); token = (JObject)ODataClientHelper.InvokeGet(location + "?$expand=CustomerContacts,Items"); Assert.AreEqual("new sale person", token["SalesPersonName"]); contacts = (JArray)token["CustomerContacts"]; Assert.AreEqual(2, contacts.Count); Assert.IsTrue(contacts.Any(t => (string)t["Method"] == "QQ")); transactions = (JArray)token["Items"]; Assert.AreEqual(2, transactions.Count); Assert.IsTrue(transactions.Any(t => (string)t["ProductName"] == "new product")); }
public void PostSale_Sale_CollectionNavigationProperty() { var location = default(string); var token = new JObject(); token["SalesPersonName"] = "sale person post"; token["CustomerName"] = "customer post"; var contact1 = new JObject(); contact1["Method"] = "Phone"; contact1["Value"] = "phone 1"; var contact2 = new JObject(); contact2["Method"] = "Phone"; contact2["Value"] = "phone 2"; var contacts = new JArray(); contacts.Add(contact1); contacts.Add(contact2); token["CustomerContacts"] = contacts; var transaction1 = new JObject(); transaction1["ProductName"] = "product 1"; transaction1["ProductDescription"] = "description 1"; transaction1["Quantity"] = 1; transaction1["UnitPrice"] = 100000; var transaction2 = new JObject(); transaction2["ProductName"] = "product 2"; transaction2["ProductDescription"] = "description 2"; transaction2["Quantity"] = 2; transaction2["UnitPrice"] = 200000; var transactions = new JArray(); transactions.Add(transaction1); transactions.Add(transaction2); token["Items"] = transactions; ODataClientHelper.InvokePost(string.Format("{0}Sales", SalesServiceRootUrl), token.ToString(Formatting.None), null, response => { location = response.Headers.Location.ToString(); return true; }); token = (JObject)ODataClientHelper.InvokeGet(location + "?$expand=CustomerContacts,Items"); Assert.AreEqual("sale person post", token["SalesPersonName"]); Assert.AreEqual("customer post", token["CustomerName"]); contacts = (JArray)token["CustomerContacts"]; Assert.IsTrue(contacts.Any(t => (string)t["Method"] == "Phone" && (string)t["Value"] == "phone 1")); Assert.IsTrue(contacts.Any(t => (string)t["Method"] == "Phone" && (string)t["Value"] == "phone 2")); transactions = (JArray)token["Items"]; Assert.IsTrue(transactions.Any(t => (string)t["ProductName"] == "product 1" && (string)t["ProductDescription"] == "description 1" && (int)t["Quantity"] == 1 && (int)t["UnitPrice"] == 100000)); Assert.IsTrue(transactions.Any(t => (string)t["ProductName"] == "product 2" && (string)t["ProductDescription"] == "description 2" && (int)t["Quantity"] == 2 && (int)t["UnitPrice"] == 200000)); }
private void LoadProfiles() { if (profiles == null || profiles.Count == 0) { string data = MyGetWebData(homeUrl, true); Regex rgx = new Regex(@"nf\.constants\.page\.contextData =(.*); }\(netflix\)\);"); Match m = rgx.Match(data); if (m.Success) { string jsonData = m.Groups[1].Value; JObject json = (JObject)JsonConvert.DeserializeObject(jsonData); profiles = json["profiles"]["data"]["allProfiles"].Value<JArray>(); if (string.IsNullOrEmpty(ProfileName) || !profiles.Any(p => p["profileName"].Value<string>() == ProfileName)) { if (!string.IsNullOrWhiteSpace(startupUserProfile) && profiles.Any(p => p["profileName"].Value<string>() == startupUserProfile)) { currentProfile = (JObject)profiles.FirstOrDefault(p => p["profileName"].Value<string>() == startupUserProfile); } else { currentProfile = (JObject)profiles.FirstOrDefault(p => p["isAccountOwner"].Value<bool>()); } } else { currentProfile = (JObject)profiles.FirstOrDefault(p => p["profileName"].Value<string>() == ProfileName); } MyGetWebData(string.Format(switchProfileUrl, apiRoot, ProfileToken), true); } else { cc = null; Settings.DynamicCategoriesDiscovered = false; Settings.Categories.Clear(); profiles = null; throw new OnlineVideosException("Error loading profiles. Please try again"); } } }