Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }