Connectster.Shopify.ShopifyCommunicator.CreateMetafield C# (CSharp) Method

CreateMetafield() public method

public CreateMetafield ( ShopifyStoreAuth storeAuth, ShopifyMetafield metaField, int productId ) : ShopifyResponse
storeAuth ShopifyStoreAuth
metaField ShopifyMetafield
productId int
return ShopifyResponse
        public ShopifyResponse<ShopifyMetafield> CreateMetafield(ShopifyStoreAuth storeAuth, ShopifyMetafield metaField,
                                                                 int? productId)
        {
            if (storeAuth == null || metaField == null)
            {
                Logger.WarnFormat("ShopifyCommunicator::CreateMetafield():storeAuth and metaField cannot be null");
                return null;
            }
            if (productId != null && productId < 1)
                //Product ID can be null if the metafield is tagged to the store itself (vs. tagged to a product).
            {
                Logger.ErrorFormat("ShopifyCommunicator::CreateMetafield(): productId must be >=1 if non-null.");
                return null;
            }

            //Build our url depending on if this metafield will apply to e product or e shop
            var url = new StringBuilder();
            url.Append(_protocol + storeAuth.StoreSubDomain + _domain + "/admin/");

            if (productId != null)
            {
                url.Append("products/" + productId + "/");
            }
            url.Append("metafields.xml");

            var xS = new XmlSerializer(typeof (ShopifyMetafield));

            var memStream = new MemoryStream();
            var xDoc = new XmlDocument();

            xS.Serialize(memStream, metaField);
            memStream.Seek(0, SeekOrigin.Begin);
            xDoc.Load(memStream);
            memStream.Close();

            return new ShopifyResponse<ShopifyMetafield>(ShopifyPutPost(url.ToString(),
                                                                        HashString(_appAuth.Secret +
                                                                                   storeAuth.StoreAuthToken), xDoc,
                                                                        "POST"));
        }