internal void RequestEngagement(string request) { Action action = delegate() { JSONObject engagement = null; try { engagement = Json.Deserialize(request) as JSONObject; } catch (Exception exception) { Logger.LogError("Failed to deserialise engage request: " + exception.Message); } if (engagement != null) { var decisionPoint = engagement["decisionPoint"] as string; var flavour = engagement["flavour"] as string; var parameters = engagement["parameters"] as JSONObject; var id = engagement["id"] as string; EngageResponse engageResponse = (response, statusCode, error) => { manager.EngageResponse(id, response, statusCode, error); }; EngageRequest engageRequest = new EngageRequest(decisionPoint); engageRequest.Flavour = flavour; engageRequest.Parameters = parameters; StartCoroutine(Engage.Request(this, engageRequest, engageResponse)); } }; actions.Enqueue(action); }
//////////////////////////////////////////////////////////////////// // Publisher MetaGame Engage Campaign Check //////////////////////////////////////////////////////////////////// public void MetaGameRequestEngagement(Engagement engagement, Action <Engagement> onCompleted, Action <Exception> onError) { DeltaDNA.Logger.LogDebug("MetaGame Campaign Check Click"); if (String.IsNullOrEmpty(ENGAGE_REST_API_URL)) { 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(MetaGameEngageRequest(this, request, handler)); } catch (Exception ex) { DeltaDNA.Logger.LogWarning("Engagement request failed: " + ex.Message); } }
/// <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); } }
internal static IEnumerator Request(MonoBehaviour caller, EngageRequest request, EngageResponse response) { string requestJSON = request.ToJSON(); string url = DDNA.Instance.ResolveEngageURL(requestJSON); HttpRequest httpRequest = new HttpRequest(url); httpRequest.HTTPMethod = HttpRequest.HTTPMethodType.POST; httpRequest.HTTPBody = requestJSON; httpRequest.TimeoutSeconds = DDNA.Instance.Settings.HttpRequestEngageTimeoutSeconds; httpRequest.setHeader("Content-Type", "application/json"); System.Action<int, string, string> httpHandler = (statusCode, data, error) => { string engagementKey = "DDSDK_ENGAGEMENT_" + request.DecisionPoint + "_" + request.Flavour; if (error == null && statusCode >= 200 && statusCode < 300) { try { PlayerPrefs.SetString(engagementKey, data); } catch (Exception exception) { Logger.LogWarning("Unable to cache engagement: "+exception.Message); } } else { Logger.LogDebug("Engagement failed with "+statusCode+" "+error); if (PlayerPrefs.HasKey(engagementKey)) { Logger.LogDebug("Using cached response"); data = "{\"isCachedResponse\":true," + PlayerPrefs.GetString(engagementKey).Substring(1); } else { data = "{}"; } } response(data, statusCode, error); }; yield return caller.StartCoroutine(Network.SendRequest(httpRequest, httpHandler)); }
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); } }
internal static IEnumerator Request( MonoBehaviour caller, EngageCache cache, EngageRequest request, EngageResponse response, bool useConfigurationTimeout = false) { string requestJSON = request.ToJSON(); string url = DDNA.Instance.ResolveEngageURL(requestJSON); HttpRequest httpRequest = new HttpRequest(url); httpRequest.HTTPMethod = HttpRequest.HTTPMethodType.POST; httpRequest.HTTPBody = requestJSON; httpRequest.TimeoutSeconds = useConfigurationTimeout ? DDNA.Instance.Settings.HttpRequestConfigurationTimeoutSeconds : DDNA.Instance.Settings.HttpRequestEngageTimeoutSeconds; httpRequest.setHeader("Content-Type", "application/json"); Action <int, string, string> httpHandler = (statusCode, data, error) => { if (error == null && statusCode >= 200 && statusCode < 300) { cache.Put(request.DecisionPoint, request.Flavour, data); } else { Logger.LogDebug("Engagement failed with " + statusCode + " " + error); var isClientError = statusCode >= 400 && statusCode < 500; var cached = cache.Get(request.DecisionPoint, request.Flavour); if (cached != null && !isClientError) { Logger.LogDebug("Using cached response"); data = "{\"isCachedResponse\":true," + cached.Substring(1); } else { data = "{}"; } } response(data, statusCode, error); }; yield return(caller.StartCoroutine(Network.SendRequest(httpRequest, httpHandler))); }
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); } }
/// <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); } }
internal static IEnumerator MetaGameEngageRequest(MonoBehaviour caller, EngageRequest request, EngageResponse response) { string requestJSON = request.ToJSON(); string url = String.Format("{0}/{1}", ENGAGE_REST_API_URL, ENVIRONMENT_KEY); HttpRequest httpRequest = new HttpRequest(url); httpRequest.HTTPMethod = HttpRequest.HTTPMethodType.POST; httpRequest.HTTPBody = requestJSON; httpRequest.TimeoutSeconds = DDNA.Instance.Settings.HttpRequestEngageTimeoutSeconds; httpRequest.setHeader("Content-Type", "application/json"); System.Action <int, string, string> httpHandler = (statusCode, data, error) => { string engagementKey = "DDSDK_ENGAGEMENT_" + request.DecisionPoint + "_" + request.Flavour; if (error == null && statusCode >= 200 && statusCode < 300) { try { PlayerPrefs.SetString(engagementKey, data); } catch (Exception exception) { DeltaDNA.Logger.LogWarning("Unable to cache engagement: " + exception.Message); } } else { DeltaDNA.Logger.LogDebug("Engagement failed with " + statusCode + " " + error); if (PlayerPrefs.HasKey(engagementKey)) { DeltaDNA.Logger.LogDebug("Using cached response"); data = "{\"isCachedResponse\":true," + PlayerPrefs.GetString(engagementKey).Substring(1); } else { data = "{}"; } } response(data, statusCode, error); }; yield return(caller.StartCoroutine(DeltaDNA.Network.SendRequest(httpRequest, httpHandler))); }