public void Save(Asset asset, string comment)
        {
            if (asset.HasChanged || asset.Oid.IsNull)
            {
                var doc = new XmlDocument();

                var s      = new StringWriter();
                var writer = new XmlApiWriter(s, true);
                writer.WriteAsset(asset);
                var data = s.ToString();

                var path = "Data/" + asset.AssetType.Token;

                if (!asset.Oid.IsNull)
                {
                    path += "/" + asset.Oid.Key;
                }

                if (comment != null & comment != string.Empty)
                {
                    path += string.Format("?Comment='{0}'", System.Web.HttpUtility.UrlEncode(comment));
                }

                try {
                    using (var stream = connector.SendData(path, data)) {
                        doc.Load(stream);
                    }

                    ParseSaveAssetNode(doc.DocumentElement, asset);
                } catch (WebException ex) {
                    if (ex.Response == null)
                    {
                        throw new ConnectionException("Error writing to output stream", ex);
                    }

                    using (var stream = ex.Response.GetResponseStream()) {
                        doc.Load(stream);
                    }

                    var message = doc.DocumentElement.SelectSingleNode("Message").InnerText;
                    throw new APIException(message, asset.Oid.Token, ex);
                } catch (Exception ex) {
                    throw new APIException("Failed to save", asset.Oid.Token, ex);
                }
            }
        }
Ejemplo n.º 2
0
        public void Save(Asset asset, string comment)
        {
            if (asset.HasChanged || asset.Oid.IsNull)
            {
                var doc = new XmlDocument();

                var s      = new StringWriter();
                var writer = new XmlApiWriter(s);
                writer.WriteAsset(asset);
                var data = s.ToString();

                var path = asset.AssetType.Token;

                if (!asset.Oid.IsNull)
                {
                    path += $"/{asset.Oid.Key}";
                }

                if (comment != null & comment != string.Empty)
                {
                    path += $"?Comment='{System.Web.HttpUtility.UrlEncode(comment)}'";
                }

                try
                {
                    Stream stream;
                    if (_connector != null)
                    {
                        path   = $"Data/{path}";
                        stream = _connector.SendData(path, data);
                    }
                    else
                    {
                        _v1Connector.UseDataApi();
                        stream = _v1Connector.SendData(path, data);
                    }
                    doc.Load(stream);
                    stream.Dispose();

                    ParseSaveAssetNode(doc.DocumentElement, asset);
                }
                catch (WebException ex)
                {
                    if (ex.Response == null)
                    {
                        throw new ConnectionException("Error writing to output stream", ex);
                    }

                    using (var stream = ex.Response.GetResponseStream())
                    {
                        doc.Load(stream);
                    }

                    var message = doc.DocumentElement.SelectSingleNode("Message").InnerText;
                    throw new APIException(message, asset.Oid.Token, ex);
                }
                catch (Exception ex)
                {
                    throw new APIException("Failed to save", asset.Oid.Token, ex);
                }
            }
        }
        public void Save(Asset asset, string comment) {
            if (asset.HasChanged || asset.Oid.IsNull) {
                var doc = new XmlDocument();

                var s = new StringWriter();
                var writer = new XmlApiWriter(s, true);
                writer.WriteAsset(asset);
                var data = s.ToString();

                var path = "Data/" + asset.AssetType.Token;

                if(!asset.Oid.IsNull) {
                    path += "/" + asset.Oid.Key;
                }

                if (comment != null & comment != string.Empty) {
                    path += string.Format("?Comment='{0}'", System.Web.HttpUtility.UrlEncode(comment));
                }

                try {
                    using(var stream = connector.SendData(path, data)) {
                        doc.Load(stream);
                    }

                    ParseSaveAssetNode(doc.DocumentElement, asset);
                } catch (WebException ex) {
                    if(ex.Response == null) {
                        throw new ConnectionException("Error writing to output stream", ex);
                    }

                    using(var stream = ex.Response.GetResponseStream()) {
                        doc.Load(stream);
                    }

                    var message = doc.DocumentElement.SelectSingleNode("Message").InnerText;
                    throw new APIException(message, asset.Oid.Token, ex);
                } catch (Exception ex) {
                    throw new APIException("Failed to save", asset.Oid.Token, ex);
                }
            }
        }