SendPutContents() protected static method

Sends the body text up to the server, using the PUT method
protected static SendPutContents ( WebRequest request, string bodyText ) : void
request WebRequest
bodyText string
return void
Ejemplo n.º 1
0
    private SiteUser UpdateUser(string userId, string newRole, SiteUserAuth newAuthentication)
    {
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(userId), "missing user id");
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(newRole), "missing role");

        string newAuthenticationText = SendCreateUser.SiteUserAuthToAttributeText(newAuthentication);

        //ref: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#update_user
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("user");
        xmlWriter.WriteAttributeString("siteRole", newRole);
        xmlWriter.WriteAttributeString("authSetting", newAuthenticationText);
        xmlWriter.WriteEndElement(); //</user>
        xmlWriter.WriteEndElement(); // </tsRequest>
        xmlWriter.Close();

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

        //Create a web request
        var urlUpdateUser = _onlineUrls.Url_UpdateSiteUser(_onlineSession, userId);
        var webRequest    = this.CreateLoggedInWebRequest(urlUpdateUser, "PUT");

        TableauServerRequestBase.SendPutContents(webRequest, xmlText);

        //Get the response
        var response = GetWebReponseLogErrors(webRequest, "update yser (change auth or role)");

        using (response)
        {
            var xmlDoc = GetWebResponseAsXml(response);


            //Get all the user nodes
            var nsManager = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
            var xNodeUser = xmlDoc.SelectSingleNode("//iwsOnline:user", nsManager);

            try
            {
                return(SiteUser.FromUserXMLWithoutUserId(xNodeUser, userId));
            }
            catch (Exception parseXml)
            {
                StatusLog.AddError("Update user, error parsing XML response " + parseXml.Message + "\r\n" + xNodeUser.InnerXml);
                return(null);
            }
        }
    }
Ejemplo n.º 2
0
    private SiteFlow ChangeContentOwner(string flowId, string newOwnerId)
    {
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(flowId), "missing flow id");
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(newOwnerId), "missing owner id");

        //ref: https://onlinehelp.tableau.com/current/api/rest_api/en-us/help.htm#REST/rest_api_ref.htm#Update_Flow%3FTocPath%3DAPI%2520Reference%7C_____76
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("flow");
        xmlWriter.WriteStartElement("owner");
        xmlWriter.WriteAttributeString("id", newOwnerId);
        xmlWriter.WriteEndElement(); //</owner>
        xmlWriter.WriteEndElement(); //</flow>
        xmlWriter.WriteEndElement(); // </tsRequest>
        xmlWriter.Close();

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

        //Create a web request
        var urlUpdateFlow = _onlineUrls.Url_UpdateFlow(_onlineSession, flowId);
        var webRequest    = this.CreateLoggedInWebRequest(urlUpdateFlow, "PUT");

        TableauServerRequestBase.SendPutContents(webRequest, xmlText);

        //Get the response
        var response = GetWebReponseLogErrors(webRequest, "update flow (change owner)");

        using (response)
        {
            var xmlDoc = GetWebResponseAsXml(response);


            //Get all the flow nodes
            var nsManager = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
            var xNodeDs   = xmlDoc.SelectSingleNode("//iwsOnline:flow", nsManager);

            try
            {
                return(new SiteFlow(xNodeDs));
            }
            catch (Exception parseXml)
            {
                StatusLog.AddError("Change flow owner, error parsing XML response " + parseXml.Message + "\r\n" + xNodeDs.InnerXml);
                return(null);
            }
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Update the group
    /// </summary>
    /// <returns>TRUE: Success.  FALSE: failed/error</returns>
    private bool UpdateGroup()
    {
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(this.GroupId), "920-1053: missing group id");
        AppDiagnostics.Assert(!string.IsNullOrWhiteSpace(this.UpdatedGroupName), "920-1054: missing role");

        //[2020-09-20] NOTE: Currently this function DOES NOT work for Active Directory syncronized groups (on premises Tableau Serer)

        //ref: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#update_group
        var sb        = new StringBuilder();
        var xmlWriter = XmlWriter.Create(sb, XmlHelper.XmlSettingsForWebRequests);

        xmlWriter.WriteStartElement("tsRequest");
        xmlWriter.WriteStartElement("group");
        xmlWriter.WriteAttributeString("name", this.UpdatedGroupName);

        //If we are updating the grant license parts, write these here
        //[2020-09-20] For (on premises Server) Active Directory sync, this would need to be different XML (inside an "import" node)
        if (this.PerformUpdateGrantLicense)
        {
            xmlWriter.WriteAttributeString("grantLicenseMode", this.UpdatedGrantLicenseMode);

            //If the Grant license mode is blank, set the license role to be UNLICENSED
            //This is required to remove the licensing mode: https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#update_group
            string updateLicenseMode = this.UpdatedGrantLicenseSiteRole;
            if (string.IsNullOrEmpty(this.UpdatedGrantLicenseMode))
            {
                updateLicenseMode = "UNLICENSED";
            }
            xmlWriter.WriteAttributeString("minimumSiteRole", updateLicenseMode);
        }

        xmlWriter.WriteEndElement(); //</group>
        xmlWriter.WriteEndElement(); // </tsRequest>
        xmlWriter.Close();

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

        //Create a web request
        var urlUpdateGroup = this._onlineSession.ServerUrls.Url_UpdateSiteGroup(_onlineSession, this.GroupId);
        var webRequest     = this.CreateLoggedInWebRequest(urlUpdateGroup, "PUT");

        TableauServerRequestBase.SendPutContents(webRequest, xmlText);

        //Get the response
        var response = GetWebReponseLogErrors(webRequest, "update group (change name/grant-license)");

        using (response)
        {
            var xmlDoc = GetWebResponseAsXml(response);

            //Get all the group nodes
            var nsManager         = XmlHelper.CreateTableauXmlNamespaceManager("iwsOnline");
            var xNodeUpdatedGroup = xmlDoc.SelectSingleNode("//iwsOnline:group", nsManager);

            try
            {
                //Sanity check on the expected results
                var updatesGroupId      = xNodeUpdatedGroup.Attributes["id"].Value;
                var returnedMinSiteRole = XmlHelper.SafeParseXmlAttribute(xNodeUpdatedGroup, "minimumSiteRole", "");

                if (updatesGroupId != this.GroupId)
                {
                    IwsDiagnostics.Assert(false, "920-1102: Error. Updated groups returned mismatching group id: " + this.GroupId + "/" + updatesGroupId);
                    StatusLog.AddError("920-1102: Error. Updated groups returned mismatching group id: " + this.GroupId + "/" + updatesGroupId);
                    return(false);
                }

                //See if the returned role matches
                if (this.PerformUpdateGrantLicense)
                {
                    if (!CompareGrantLicenseRoles(returnedMinSiteRole, this.UpdatedGrantLicenseSiteRole))
                    {
                        string errorText = "920-1206: Error. Updated Grant License role for group does not match expected role: "
                                           + NullSafeText(returnedMinSiteRole) + "/" + NullSafeText(this.UpdatedGrantLicenseSiteRole);
                        StatusLog.AddError(errorText);
                        return(false);
                    }
                }
            }
            catch (Exception parseXml)
            {
                StatusLog.AddError("920-1105: Update group, error parsing XML response " + parseXml.Message + "\r\n" + xmlDoc.InnerXml);
                return(false);
            }
        }
        return(true); //Success
    }