Esempio n. 1
0
        /// <summary>
        /// After a file has been uploaded in chunks, we need to make a call to COMMIT the file to server as a published Data Source
        /// </summary>
        /// <param name="uploadSessionId"></param>
        /// <param name="publishedContentName"></param>
        private SiteDatasource FinalizePublish(
            string uploadSessionId,
            string publishedContentName,
            string publishedContentType,
            string projectId,
            CredentialManager.Credential dbCredentials)
        {
            //See definition: http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_ref.htm#Publish_Datasource%3FTocPath%3DAPI%2520Reference%7C_____29
            var sb        = new StringBuilder();
            var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

            xmlWriter.WriteStartElement("tsRequest");
            xmlWriter.WriteStartElement("datasource");
            xmlWriter.WriteAttributeString("name", publishedContentName);

            //If we have an associated database credential, write it out
            if (dbCredentials != null)
            {
                CredentialXmlHelper.WriteCredential(
                    xmlWriter,
                    dbCredentials);
            }

            xmlWriter.WriteStartElement("project"); //<project>
            xmlWriter.WriteAttributeString("id", projectId);
            xmlWriter.WriteEndElement();            //</project>
            xmlWriter.WriteEndElement();            // </datasource>
            //Currently not supporting <connectionCredentials>
            xmlWriter.WriteEndElement();            // </tsRequest>
            xmlWriter.Dispose();

            var xmlText = sb.ToString(); //Get the XML text out

            //Generate the MIME message
            var mimeGenerator = new MimeWriterXml(xmlText);

            //Create a web request to push the
            var urlFinalizeUpload = Urls.Url_FinalizeDataSourcePublish(Login, uploadSessionId, publishedContentType);

            //NOTE: The publish finalization step can take several minutes, because server needs to unpack the uploaded ZIP and file it away.
            //      For this reason, we pass in a long timeout
            var response = this.CreateAndSendMimeLoggedInRequest(urlFinalizeUpload, HttpMethod.Post, mimeGenerator);
            var xmlDoc   = GetHttpResponseAsXml(response);

            //Get all the datasource node from the response
            var xDoc          = xmlDoc.ToXDocument();
            var dataSourceXml = xDoc.Root.Descendants(XName.Get("datasource", XmlNamespace)).FirstOrDefault();

            try
            {
                return(new SiteDatasource(dataSourceXml.ToXmlNode(), XmlNamespace));
            }
            catch (Exception parseXml)
            {
                Login.Logger.Error("Data source upload, error parsing XML response " + parseXml.Message + "\r\n" + dataSourceXml.ToXmlNode());
                return(null);
            }
        }
Esempio n. 2
0
    /// <summary>
    /// After a file has been uploaded in chunks, we need to make a call to COMMIT the file to server as a published Data Source
    /// </summary>
    /// <param name="uploadSessionId"></param>
    /// <param name="publishedContentName"></param>
    private SiteDatasource FinalizePublish(
        string uploadSessionId,
        string publishedContentName,
        string publishedContentType,
        string projectId,
        CredentialManager.Credential dbCredentials)
    {
        //See definition: http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_ref.htm#Publish_Datasource%3FTocPath%3DAPI%2520Reference%7C_____29
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("datasource");
        xmlWriter.WriteAttributeString("name", publishedContentName);

        //If we have an associated database credential, write it out
        if (dbCredentials != null)
        {
            CredentialXmlHelper.WriteCredential(
                xmlWriter,
                dbCredentials);
        }

        xmlWriter.WriteStartElement("project"); //<project>
        xmlWriter.WriteAttributeString("id", projectId);
        xmlWriter.WriteEndElement();            //</project>
        xmlWriter.WriteEndElement();            // </datasource>
        //Currently not supporting <connectionCredentials>
        xmlWriter.WriteEndElement();            // </tsRequest>
        xmlWriter.Close();

        var xmlText = sb.ToString(); //Get the XML text out

        //Generate the MIME message
        var mimeGenerator = new MimeWriterXml(xmlText);

        //Create a web request to push the
        var urlFinalizeUpload = _onlineUrls.Url_FinalizeDataSourcePublish(_onlineSession, uploadSessionId, publishedContentType);

        //NOTE: The publish finalization step can take several minutes, because server needs to unpack the uploaded ZIP and file it away.
        //      For this reason, we pass in a long timeout
        var webRequest = this.CreateAndSendMimeLoggedInRequest(urlFinalizeUpload, "POST", mimeGenerator, TableauServerWebClient.DefaultLongRequestTimeOutMs);
        var xmlDoc     = GetWebReponseLogErrors_AsXmlDoc(webRequest, "finalize datasource publish");

        //Get all the datasource node from the response
        var nsManager     = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
        var dataSourceXml = xmlDoc.SelectSingleNode("//iwsOnline:datasource", nsManager);

        try
        {
            return(new SiteDatasource(dataSourceXml));
        }
        catch (Exception parseXml)
        {
            StatusLog.AddError("Data source upload, error parsing XML response " + parseXml.Message + "\r\n" + dataSourceXml.InnerXml);
            return(null);
        }
    }
Esempio n. 3
0
        /// <summary>
        /// After a file has been uploaded in chunks, we need to make a call to COMMIT the file to server as a published Workbook
        /// </summary>
        /// <param name="uploadSessionId"></param>
        /// <param name="publishedContentName"></param>
        private SiteWorkbook FinalizePublish(
            string uploadSessionId,
            string publishedContentName,
            string publishedContentType,
            string projectId,
            CredentialManager.Credential dbCredentials,
            WorkbookPublishSettings publishSettings)
        {
            if (projectId == null)
            {
                projectId = "";
            }
            //See definition: http://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_ref.htm#Publish_Workbook%3FTocPath%3DAPI%2520Reference%7C_____29
            var sb = new StringBuilder();

            //Build the XML part of the MIME message we will post up to server
            var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

            xmlWriter.WriteStartElement("tsRequest");
            xmlWriter.WriteStartElement("workbook");
            xmlWriter.WriteAttributeString("name", publishedContentName);
            xmlWriter.WriteAttributeString("showTabs", XmlHelper.BoolToXmlText(publishSettings.ShowTabs));

            //If we have an associated database credential, write it out
            if (dbCredentials != null)
            {
                CredentialXmlHelper.WriteCredential(
                    xmlWriter,
                    dbCredentials);
            }

            xmlWriter.WriteStartElement("project"); //<project>
            xmlWriter.WriteAttributeString("id", projectId);
            xmlWriter.WriteEndElement();            //</project>
            xmlWriter.WriteEndElement();            // </workbook>
            xmlWriter.WriteEndElement();            // </tsRequest>
            xmlWriter.Dispose();

            var xmlText = sb.ToString(); //Get the XML text out

            //Generate the MIME message and pack the XML into it
            var mimeGenerator = new MimeWriterXml(xmlText);

            //Create a web request to POST the MIME message to server to finalize the publish
            var urlFinalizeUpload = Urls.Url_FinalizeWorkbookPublish(Login, uploadSessionId, publishedContentType);

            //NOTE: The publish finalization step can take several minutes, because server needs to unpack the uploaded ZIP and file it away.
            //      For this reason, we pass in a long timeout
            var response = CreateAndSendMimeLoggedInRequest(urlFinalizeUpload, HttpMethod.Post, mimeGenerator);

            using (response)
            {
                var xmlDoc      = GetHttpResponseAsXml(response);
                var xDoc        = xmlDoc.ToXDocument();
                var workbookXml = xDoc.Root.Descendants(XName.Get("workbook", XmlNamespace)).FirstOrDefault();
                try
                {
                    return(new SiteWorkbook(workbookXml.ToXmlNode(), XmlNamespace));
                }
                catch (Exception parseXml)
                {
                    Login.Logger.Error("Workbook upload, error parsing XML response " + parseXml.Message + "\r\n" + workbookXml.ToXmlNode().InnerXml);
                    return(null);
                }
            }
        }