public void CanFormatTheUserKey()
        {
            //arrange
            string userid  = "84ea4845-57ba-4096-909f-zt67y8";
            string account = "84ea4845-57ba-4096-909f-zt67y8";

            //act
            string userkey = LaunchDarklyServices.FormatUserKey(userid, account);

            //assert
            Assert.IsTrue(userkey == "84ea4845-57ba-4096-909f-zt67y8:84ea4845-57ba-4096-909f-zt67y8");
        }
        public void CanHashTheUserKey()
        {
            //arrange
            string userkey = "84ea4845-57ba-4096-909f-zt67y8:mikaeltest";

            Environment.SetEnvironmentVariable("LaunchDarkly_SDK_Key", "sdk-59baef5c-3851-4fef");
            //act
            string hashkey = LaunchDarklyServices.GetHashKey(userkey);

            //assert
            Assert.IsTrue(hashkey == "44745db8726d4df864787ce328edf1dd25bfbe92234c5e0d753c8e13e429cdd6");
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ExecutionContext context, TraceWriter log)
        {
            try
            {
                telemetry.Context.Operation.Id   = context.InvocationId.ToString();
                telemetry.Context.Operation.Name = "UpdateUserFeatureFlag";
                int apiversion = Helpers.GetHeaderValue(req, "api-version");


                var data = req.Content.ReadAsStringAsync().Result; //Gettings parameters in Body request
                log.Info(data);
                var startTime = DateTime.Now;
                var timer     = System.Diagnostics.Stopwatch.StartNew();

                var formValues = data.Split('&')
                                 .Select(value => value.Split('='))
                                 .ToDictionary(pair => Uri.UnescapeDataString(pair[0]).Replace("+", " "),
                                               pair => Uri.UnescapeDataString(pair[1]).Replace("+", " "));

                string account           = formValues["account"];
                string LDproject         = formValues["ldproject"];
                string LDenv             = formValues["ldenv"];
                string feature           = formValues["feature"];
                string active            = formValues["active"];
                var    appSettingExtCert = (apiversion < 4) ? formValues["appsettingextcert"] : string.Empty; //"RollUpBoard_ExtensionCertificate"
                var    ExtCertKey        = (apiversion >= 4) ? formValues["extcertkey"] : string.Empty;
                bool   useKeyVault       = (apiversion >= 4);

                //get the token passed in the header request
                string tokenuserId = Helpers.TokenIsValid(req, useKeyVault, appSettingExtCert, ExtCertKey, log);


                if (tokenuserId != null)
                {
                    var userkey = LaunchDarklyServices.FormatUserKey(tokenuserId, account);
                    HttpResponseMessage updateStatusResponse = await LaunchDarklyServices.UpdateUserFlag(LDproject, LDenv, userkey, feature, Convert.ToBoolean(active));

                    telemetry.TrackDependency("LaunchDarklyRestAPI", "UpdateUserFlag", startTime, timer.Elapsed, updateStatusResponse.Content != null);

                    if (updateStatusResponse.StatusCode == HttpStatusCode.NoContent)
                    {
                        telemetry.TrackEvent("The flag is updated");
                        return(req.CreateResponse(HttpStatusCode.OK, "The flag is updated"));
                    }
                    else
                    {
                        telemetry.TrackTrace("Error to update the flag with data: " + updateStatusResponse.Content);
                        return(req.CreateResponse(HttpStatusCode.BadRequest, "Error to update the flag with data: " + updateStatusResponse.Content));
                    }
                }
                else
                {
                    telemetry.TrackTrace("The token is not valid");
                    return(req.CreateResponse(HttpStatusCode.InternalServerError, "The token is not valid"));
                }
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
                return(req.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }