private void OnDownloadDialogResp(RESTConnector.Request req, RESTConnector.Response resp) { DownloadDialogReq downloadReq = req as DownloadDialogReq; if (downloadReq == null) { throw new WatsonException("Unexpected type."); } if (resp.Success) { try { if (SaveFile != null) { SaveFile(downloadReq.DialogFileName, resp.Data); } else { #if !UNITY_WEBPLAYER File.WriteAllBytes(downloadReq.DialogFileName, resp.Data); #endif } } catch (Exception e) { Log.Error("Dialog", "Caught exception: {0}", e.ToString()); resp.Success = false; } } if (((DownloadDialogReq)req).Callback != null) { ((DownloadDialogReq)req).Callback(resp.Success); } }
/// <summary> /// Downloads the dialog by the dialog ID into the provided filename. /// </summary> /// <param name="dialogId">The ID of the dialog to download.</param> /// <param name="dialogFileName">The filename to download the dialog into.</param> /// <param name="callback">The callback to invoke on failure or success.</param> /// <param name="format">The format to download.</param> /// <returns>Returns true if request is sent.</returns> public bool DownloadDialog(string dialogId, string dialogFileName, OnDialogCallback callback, DialogFormat format = DialogFormat.XML) { if (string.IsNullOrEmpty(dialogId)) { throw new ArgumentNullException("dialogId"); } if (string.IsNullOrEmpty(dialogFileName)) { throw new ArgumentNullException("dialogFileName"); } if (callback == null) { throw new ArgumentNullException("callback"); } RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/dialogs/" + dialogId); if (connector == null) { return(false); } DownloadDialogReq req = new DownloadDialogReq(); req.DialogFileName = dialogFileName; req.Callback = callback; req.OnResponse = OnDownloadDialogResp; if (format == DialogFormat.XML) { req.Headers["Accept"] = "application/wds+xml"; } else if (format == DialogFormat.JSON) { req.Headers["Accept"] = "application/wds+json"; } else { req.Headers["Accept"] = "application/octet-stream"; } return(connector.Send(req)); }