public async Task <HttpResponseMessage> UpdateTagsForInstallation(string Id) { // Get the tags to update from the body of the request. var message = await this.Request.Content.ReadAsStringAsync(); // Validate the submitted tags do not try to overwrite tags created for user. if (string.IsNullOrEmpty(message) || message.Contains("sid:")) { // We can't trust users to submit their own user IDs. return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } // Verify that the tags are a valid JSON array. var tags = JArray.Parse(message); // Define a collection of PartialUpdateOperations. Note that // only one '/tags' path is permitted in a given collection. var updates = new List <PartialUpdateOperation>(); // Add a replace operation for the tag. updates.Add(new PartialUpdateOperation { Operation = UpdateOperationType.Replace, Path = "/tags", Value = tags.ToString() }); // Add the requested tags to the installation. await _hubClient.PatchInstallationAsync(Id, updates); // Return success status. return(new HttpResponseMessage(HttpStatusCode.OK)); }
private async Task PatchInstallationAsync_CreateInstallationWithoutTagsAndAddTagThroughPatch_GetInstallationWithAddedTagBack() { LoadMockData(); await DeleteAllRegistrationsAndInstallations(); var installationId = Guid.NewGuid().ToString(); var installation = new Installation { InstallationId = installationId, Platform = NotificationPlatform.Apns, PushChannel = _configuration["AppleDeviceToken"] }; await _hubClient.CreateOrUpdateInstallationAsync(installation); await _hubClient.PatchInstallationAsync(installationId, new List <PartialUpdateOperation> { new PartialUpdateOperation() { Operation = UpdateOperationType.Add, Path = "/tags", Value = "tag1" } }); await Sleep(TimeSpan.FromSeconds(1)); var updatedInstallation = await _hubClient.GetInstallationAsync(installationId); Assert.Contains("tag1", updatedInstallation.Tags); RecordTestResults(); }
private async Task <HttpResponseMessage> PatchInstallation(string Id, List <PartialUpdateOperation> updates) { try { // Add the requested tag to the installation. await hubClient.PatchInstallationAsync(Id, updates); // Return success status. return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (MessagingException) { // When an error occurs, return a failure status. return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
public async Task <HttpResponseMessage> Post(string InstallationId) { //Retrieve current user's email var customerEmail = ClaimsPrincipal.Current.FindFirst("emails").Value; // Create the notification hub client. NotificationHubClient hubClient = NotificationHubClient .CreateClientFromConnectionString(Settings.MS_NotificationHubConnectionString, Settings.MS_NotificationHubName); // Return the installation for the specific ID. var installation = await hubClient.GetInstallationAsync(InstallationId); if (installation.Tags == null || installation.Tags.Where(i => i == "uid:" + customerEmail).Count() == 0) { // Verify that the tags are a valid JSON array. var tags = JArray.Parse("[\"uid:" + customerEmail + "\"]"); // Define a collection of PartialUpdateOperations. Note that // only one '/tags' path is permitted in a given collection. var updates = new List <PartialUpdateOperation>(); // Add a update operation for the tag. updates.Add(new PartialUpdateOperation { Operation = UpdateOperationType.Add, Path = "/tags", Value = tags.ToString() }); try { // Add the requested tag to the installation. await hubClient.PatchInstallationAsync(InstallationId, updates); } catch (MessagingException) { // When an error occurs, return a failure status. return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } // Return success status. return(new HttpResponseMessage(HttpStatusCode.OK)); }
private async Task PatchTagsForUserDevicesAsync(Guid userId, UpdateOperationType op, string tag) { var devices = await _deviceRepository.GetManyByUserIdAsync(userId); var operation = new PartialUpdateOperation { Operation = op, Path = "/tags", Value = tag }; foreach (var device in devices) { await _client.PatchInstallationAsync(device.Id.ToString(), new List <PartialUpdateOperation> { operation }); } }
public async Task <HttpResponseMessage> AddTagsToInstallation(string Id) { // Get the tags to update from the body of the request. var message = await this.Request.Content.ReadAsStringAsync(); //traceWriter.Info("message: " + message); // Validate the submitted tags. if (string.IsNullOrEmpty(message) || message.Contains("sid:")) { // We can't trust users to submit their own user IDs. return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } // Verify that the tags are a valid JSON array. var tags = JArray.Parse(message); // Define a collection of PartialUpdateOperations. Note that // only one '/tags' path is permitted in a given collection. var updates = new List <PartialUpdateOperation>(); // Add a update operation for the tag. updates.Add(new PartialUpdateOperation { Operation = UpdateOperationType.Add, Path = "/tags", Value = tags.ToString() }); try { // Add the requested tag to the installation. await hubClient.PatchInstallationAsync(Id, updates); // Return success status. return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (MessagingException) { // When an error occurs, return a failure status. return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
public async Task <HttpResponseMessage> Delete(string InstallationId) { // Create the notification hub client. NotificationHubClient hubClient = NotificationHubClient .CreateClientFromConnectionString(Settings.MS_NotificationHubConnectionString, Settings.MS_NotificationHubName); // Return the installation for the specific ID. var installation = await hubClient.GetInstallationAsync(InstallationId); // Define a collection of PartialUpdateOperations. Note that // only one '/tags' path is permitted in a given collection. var updates = new List <PartialUpdateOperation>(); if (installation.Tags != null) { foreach (var removeTag in installation.Tags) { // Add a update operation for the tag. updates.Add(new PartialUpdateOperation { Operation = UpdateOperationType.Remove, Path = "/tags/" + removeTag }); } try { // Add the requested tag to the installation. await hubClient.PatchInstallationAsync(InstallationId, updates); } catch (MessagingException) { // When an error occurs, return a failure status. return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } } // Return success status. return(new HttpResponseMessage(HttpStatusCode.OK)); }
private async Task PatchTagsForUserDevicesAsync(IEnumerable <string> deviceIds, UpdateOperationType op, string tag) { if (!deviceIds.Any()) { return; } var operation = new PartialUpdateOperation { Operation = op, Path = "/tags", Value = tag }; foreach (var id in deviceIds) { await _client.PatchInstallationAsync(id, new List <PartialUpdateOperation> { operation }); } }
private async Task PatchTagsForUserDevicesAsync(IEnumerable <string> deviceIds, UpdateOperationType op, string tag) { if (!deviceIds.Any()) { return; } var operation = new PartialUpdateOperation { Operation = op, Path = "/tags" }; if (op == UpdateOperationType.Add) { operation.Value = tag; } else if (op == UpdateOperationType.Remove) { operation.Path += $"/{tag}"; } foreach (var id in deviceIds) { try { await _client.PatchInstallationAsync(id, new List <PartialUpdateOperation> { operation }); } catch (Exception e) { if (e.InnerException == null || !e.InnerException.Message.Contains("(404) Not Found")) { throw e; } } } }