<commit_before>// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Tracing;
using Microsoft.Azure.Mobile.Server;
using Microsoft.Azure.Mobile.Server.Authentication;
using Microsoft.Azure.Mobile.Server.Config;
using Microsoft.Azure.Mobile.Server.Notifications;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Azure.NotificationHubs.Messaging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ZumoE2EServerApp.Controllers
{
    [MobileAppController]
    public class PushApiController : ApiController
    {
        private ITraceWriter traceWriter;
        private PushClient pushClient;

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            this.traceWriter = this.Configuration.Services.GetTraceWriter();
            this.pushClient = new PushClient(this.Configuration);
        }

        [Route("api/push")]
        public async Task<HttpResponseMessage> Post()
        {
            var data = await this.Request.Content.ReadAsAsync<JObject>();
            var method = (string)data["method"];

            if (method == null)
            {
                return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
            }

            if (method == "send")
            {
                var serialize = new JsonSerializer();

                var token = (string)data["token"];


                if (data["payload"] == null || token == null)
                {
                    return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
                }

                // Payload could be a string or a dictionary
                var payloadString = data["payload"].ToString();

                var type = (string)data["type"];
                var tag = (string)data["tag"];


                if (type == "template")
                {
                    TemplatePushMessage message = new TemplatePushMessage();
                    var payload = JObject.Parse(payloadString);
                    var keys = payload.Properties();
                    foreach (JProperty key in keys)
                    {
                        this.traceWriter.Info("Key: " + key.Name);

                        message.Add(key.Name, (string)key.Value);
                    }
                    var result = await this.pushClient.SendAsync(message);
                }
                else if (type == "gcm")
                {
                    GooglePushMessage message = new GooglePushMessage();
                    message.JsonPayload = payloadString;
                    if (tag != null)
                    {
                        await this.pushClient.SendAsync(message, tag);
                    }
                    else
                    {
                        await this.pushClient.SendAsync(message);
                    }
                }
                else if (type == "apns")
                {
                    ApplePushMessage message = new ApplePushMessage();
                    this.traceWriter.Info(payloadString.ToString());
                    message.JsonPayload = payloadString.ToString();
                    var result = await this.pushClient.SendAsync(message);
                }
                else if (type == "wns")
                {
                    var wnsType = (string)data["wnsType"];
                    WindowsPushMessage message = new WindowsPushMessage();
                    message.XmlPayload = payloadString;
                    message.Headers.Add("X-WNS-Type", type + '/' + wnsType);
                    if (tag != null)
                    {
                        await this.pushClient.SendAsync(message, tag);
                    }<commit_msg>Add iOS/Apns push with tags scenario to PushApiController<commit_after>// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Tracing;
using Microsoft.Azure.Mobile.Server;
using Microsoft.Azure.Mobile.Server.Config;
using Microsoft.Azure.Mobile.Server.Notifications;
using Microsoft.Azure.NotificationHubs;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ZumoE2EServerApp.Controllers
{
    [MobileAppController]
    public class PushApiController : ApiController
    {
        private ITraceWriter traceWriter;
        private PushClient pushClient;

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            this.traceWriter = this.Configuration.Services.GetTraceWriter();
            this.pushClient = new PushClient(this.Configuration);
        }

        [Route("api/push")]
        public async Task<HttpResponseMessage> Post()
        {
            var data = await this.Request.Content.ReadAsAsync<JObject>();
            var method = (string)data["method"];

            if (method == null)
            {
                return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
            }

            if (method == "send")
            {
                var serialize = new JsonSerializer();

                var token = (string)data["token"];


                if (data["payload"] == null || token == null)
                {
                    return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
                }

                // Payload could be a string or a dictionary
                var payloadString = data["payload"].ToString();

                var type = (string)data["type"];
                var tag = (string)data["tag"];


                if (type == "template")
                {
                    TemplatePushMessage message = new TemplatePushMessage();
                    var payload = JObject.Parse(payloadString);
                    var keys = payload.Properties();
                    foreach (JProperty key in keys)
                    {
                        this.traceWriter.Info("Key: " + key.Name);

                        message.Add(key.Name, (string)key.Value);
                    }
                    var result = await this.pushClient.SendAsync(message);
                }
                else if (type == "gcm")
                {
                    GooglePushMessage message = new GooglePushMessage();
                    message.JsonPayload = payloadString;
                    if (tag != null)
                    {
                        await this.pushClient.SendAsync(message, tag);
                    }
                    else
                    {
                        await this.pushClient.SendAsync(message);
                    }
                }
                else if (type == "apns")
                {
                    ApplePushMessage message = new ApplePushMessage();
                    this.traceWriter.Info(payloadString.ToString());
                    message.JsonPayload = payloadString.ToString();
                    if (tag != null)
                    {
                        await this.pushClient.SendAsync(message, tag);
                    }
                    else
                    {
                        await this.pushClient.SendAsync(message);
                    }
                }
                else if (type == "wns")
                {
                    var wnsType = (string)data["wnsType"];
                    WindowsPushMessage message = new WindowsPushMessage();
                    message.XmlPayload = payloadString;
                    message.Headers.Add("X-WNS-Type", type + '/' + wnsType);
                    if (tag != null)
                    {
                        await this.pushClient.SendAsync(message, tag);
                    }
{'fraction_non_alphanumeric': 0.12401574803149606, 'fraction_numerical': 0.0002315886984715146, 'mean_word_length': 1.3799944888399007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403693', 'n_tokens_mistral': 1998, 'n_tokens_neox': 1871, 'n_words': 422}