Esempio n. 1
0
        /// <summary>
        /// Requests an Engagement with Engage.  The engagement is populated with the result of the request and
        /// returned in the onCompleted callback.  The engagement's json field can be queried for the returned json.
        /// A cache is maintained that will return the last valid response if available.
        /// </summary>
        /// <param name="engagement">The engagement the request is for.</param>
        /// <param name="onCompleted">Method called with the Engagement populated by Engage.</param>
        /// <exception cref="System.Exception">Thrown if the SDK has not been started, and if the Engage URL has not been set.</exception>
        public void RequestEngagement(Engagement engagement, Action <Engagement> onCompleted, Action <Exception> onError)
        {
            if (!this.started)
            {
                throw new Exception("You must first start the SDK via the StartSDK method.");
            }

            if (String.IsNullOrEmpty(this.EngageURL))
            {
                throw new Exception("Engage URL not configured.");
            }

            try {
                var dict = engagement.AsDictionary();

                var request = new EngageRequest(dict["decisionPoint"] as string);
                request.Flavour    = dict["flavour"] as string;
                request.Parameters = dict["parameters"] as Dictionary <string, object>;

                EngageResponse handler = (string response, int statusCode, string error) => {
                    engagement.Raw        = response;
                    engagement.StatusCode = statusCode;
                    engagement.Error      = error;

                    onCompleted(engagement);
                };

                StartCoroutine(Engage.Request(this, request, handler));
            } catch (Exception ex) {
                Logger.LogWarning("Engagement request failed: " + ex.Message);
            }
        }
Esempio n. 2
0
        public void CreateAValidEngagement()
        {
            var engagement = new Engagement("testDecisionPoint");

            Assert.AreEqual("testDecisionPoint", engagement.DecisionPoint);
            Assert.AreEqual("engagement", engagement.Flavour);
            CollectionAssert.AreEquivalent(new Dictionary <string, object>()
            {
                { "decisionPoint", "testDecisionPoint" },
                { "flavour", "engagement" },
                { "parameters", new Dictionary <string, object>() }
            }, engagement.AsDictionary());
        }
Esempio n. 3
0
        override internal void RequestEngagement(Engagement engagement, Action <Dictionary <string, object> > callback)
        {
            if (!this.started)
            {
                throw new Exception("You must first start the SDK via the StartSDK method.");
            }
            else if (whitelistDps.Count != 0 &&
                     !whitelistDps.Contains(engagement.GetDecisionPointAndFlavour()))
            {
                Logger.LogDebug(string.Format(
                                    "Decision point {0} is not whitelisted",
                                    engagement.GetDecisionPointAndFlavour()));
                engagement.StatusCode = 200;
                engagement.Raw        = "{}";
                callback(engagement.JSON);
                return;
            }

            if (String.IsNullOrEmpty(this.EngageURL))
            {
                throw new Exception("Engage URL not configured.");
            }

            try {
                var dict = engagement.AsDictionary();

                var request = new EngageRequest(dict["decisionPoint"] as string);
                request.Flavour    = dict["flavour"] as string;
                request.Parameters = dict["parameters"] as Dictionary <string, object>;

                EngageResponse handler = (string response, int statusCode, string error) => {
                    JSONObject responseJSON = new JSONObject();
                    if (response != null)
                    {
                        try {
                            responseJSON = DeltaDNA.MiniJSON.Json.Deserialize(response) as JSONObject;
                        } catch (Exception exception) {
                            Logger.LogError("Engagement " + engagement.DecisionPoint + " responded with invalid JSON: " + exception.Message);
                        }
                    }
                    callback(responseJSON);
                };

                StartCoroutine(Engage.Request(ddna, engageCache, request, handler));
            } catch (Exception ex) {
                Logger.LogWarning("Engagement request failed: " + ex.Message);
            }
        }
Esempio n. 4
0
        override internal void RequestEngagement(Engagement engagement, Action <Engagement> onCompleted, Action <Exception> onError)
        {
            if (!this.started)
            {
                throw new Exception("You must first start the SDK via the StartSDK method.");
            }
            else if (whitelistDps.Count != 0 &&
                     !whitelistDps.Contains(engagement.GetDecisionPointAndFlavour()))
            {
                Logger.LogDebug(string.Format(
                                    "Decision point {0} is not whitelisted",
                                    engagement.GetDecisionPointAndFlavour()));
                engagement.StatusCode = 200;
                engagement.Raw        = "{}";
                onCompleted(engagement);
                return;
            }

            if (String.IsNullOrEmpty(this.EngageURL))
            {
                throw new Exception("Engage URL not configured.");
            }

            try {
                var dict = engagement.AsDictionary();

                var request = new EngageRequest(dict["decisionPoint"] as string);
                request.Flavour    = dict["flavour"] as string;
                request.Parameters = dict["parameters"] as Dictionary <string, object>;

                EngageResponse handler = (string response, int statusCode, string error) => {
                    engagement.Raw        = response;
                    engagement.StatusCode = statusCode;
                    engagement.Error      = error;

                    onCompleted(engagement);
                };

                StartCoroutine(Engage.Request(ddna, engageCache, request, handler));
            } catch (Exception ex) {
                Logger.LogWarning("Engagement request failed: " + ex.Message);
            }
        }
Esempio n. 5
0
        public void CreateAValidEngagementWithExtraParameters()
        {
            var engagement = new Engagement("testDecisionPoint");

            engagement.AddParam("A", 1);
            engagement.AddParam("B", "two");
            engagement.AddParam("C", true);

            Assert.AreEqual("testDecisionPoint", engagement.DecisionPoint);
            Assert.AreEqual("engagement", engagement.Flavour);
            CollectionAssert.AreEquivalent(new Dictionary <string, object>()
            {
                { "decisionPoint", "testDecisionPoint" },
                { "flavour", "engagement" },
                { "parameters", new Dictionary <string, object>()
                  {
                      { "A", 1 },
                      { "B", "two" },
                      { "C", true }
                  } }
            }, engagement.AsDictionary());
        }
Esempio n. 6
0
        /// <summary>
        /// Makes an Engage request.  The result of the engagement will be passed as a dictionary object to your callback method. The dictionary
        /// will be empty if engage couldn't be reached on a campaign is not running.
        /// A cache is maintained that will return the last valid response if available.
        /// </summary>
        /// <param name="engagement">The engagement the request is for.</param>
        /// <param name="callback">Method called with the response from Engage.</param>
        /// <exception cref="System.Exception">Thrown if the SDK has not been started, and if the Engage URL has not been set.</exception>
        public void RequestEngagement(Engagement engagement, Action <Dictionary <string, object> > callback)
        {
            if (!this.started)
            {
                throw new Exception("You must first start the SDK via the StartSDK method.");
            }

            if (String.IsNullOrEmpty(this.EngageURL))
            {
                throw new Exception("Engage URL not configured.");
            }

            try {
                var dict = engagement.AsDictionary();

                var request = new EngageRequest(dict["decisionPoint"] as string);
                request.Flavour    = dict["flavour"] as string;
                request.Parameters = dict["parameters"] as Dictionary <string, object>;

                EngageResponse handler = (string response, int statusCode, string error) => {
                    JSONObject responseJSON = new JSONObject();
                    if (response != null)
                    {
                        try {
                            responseJSON = DeltaDNA.MiniJSON.Json.Deserialize(response) as JSONObject;
                        } catch (Exception exception) {
                            Logger.LogError("Engagement " + engagement.DecisionPoint + " responded with invalid JSON: " + exception.Message);
                        }
                    }
                    callback(responseJSON);
                };

                StartCoroutine(Engage.Request(this, request, handler));
            } catch (Exception ex) {
                Logger.LogWarning("Engagement request failed: " + ex.Message);
            }
        }