Example #1
0
        public static CyanEntity FromEnumerable(IEnumerable<KeyValuePair<string, object>> enumerable)
        {
            CyanEntity ret = new CyanEntity();

            foreach (var field in enumerable)
            {
                switch (field.Key)
                {
                    case "PartitionKey":
                        ret.PartitionKey = field.Value as string;
                        if (ret.PartitionKey == null)
                            throw new ArgumentException("PartitionKey must be of type \"System.String\".");
                        CyanUtilities.ValidateKeyField(ret.PartitionKey);
                        break;
                    case "RowKey":
                        ret.RowKey = field.Value as string;
                        if (ret.RowKey == null)
                            throw new ArgumentException("RowKey must be of type \"System.String\".");
                        CyanUtilities.ValidateKeyField(ret.RowKey);
                        break;
                    case "ETag":
                        ret.ETag = field.Value as string;
                        if (ret.ETag == null)
                            throw new ArgumentException("ETag must be of type \"System.String\".");
                        break;
                    default:
                        CyanUtilities.ValidateFieldType(field.Value.GetType());
                        ret.Fields.Add(field.Key, field.Value);
                        break;
                }
            }

            return ret;
        }
Example #2
0
        public static CyanEntity DeserializeEntity(XElement element)
        {
            var eTag = element.Attribute(mNamespace + "etag");

            var properties = element
                .Element(defNamespace + "content")
                .Element(mNamespace + "properties");

            var ret = new CyanEntity { ETag = eTag != null ? HttpUtility.UrlDecode(eTag.Value) : null };

            foreach (var item in properties.Elements())
            {
                var nullAttribute = item.Attribute(mNamespace + "null");
                if (nullAttribute != null && nullAttribute.Value == "true")
                    continue;

                switch (item.Name.LocalName)
                {
                    case "PartitionKey":
                        ret.PartitionKey = (string)DeserializeProperty(item);
                        break;
                    case "RowKey":
                        ret.RowKey = (string)DeserializeProperty(item);
                        break;
                    case "Timestamp":
                        ret.Timestamp = (DateTime)DeserializeProperty(item);
                        break;
                    default:
                        ret.Fields.Add(item.Name.LocalName, DeserializeProperty(item));
                        break;
                }
            }

            return ret;
        }
        public static CyanEntity ConvertToSimpleObject(JObject obj)
        {
            var ce = new CyanEntity();

            int numberOfChildElements = 0;

            foreach (var prop in obj.Properties())
            {

                if (prop.Value is JArray)
                {
                    ce.Fields.Add(String.Format("ChildTable_{0}", numberOfChildElements), prop.Name);
                    numberOfChildElements++;
                    continue;
                }

                switch (prop.Value.Type.ToString())
                {
                    case "Boolean":
                        ce.Fields.Add(prop.Name, prop.Value.Value<bool>());
                        break;
                    case "Float":
                        ce.Fields.Add(prop.Name, prop.Value.Value<double>());
                        break;
                    case "Integer":
                        ce.Fields.Add(prop.Name, prop.Value.Value<int>());
                        break;
                    case "String":
                        ce.Fields.Add(prop.Name, prop.Value.Value<string>());
                        break;
                    case "Date":
                        ce.Fields.Add(prop.Name, prop.Value.Value<DateTime>());
                        break;
                    default:
                        ce.Fields.Add(prop.Name, prop.Value.Value<object>());
                        break;
                }
            }

            return ce;
        }
Example #4
0
            public static EntityOperation CreateOperation(CyanEntity entity,
                string method,
                string resource,
                params Tuple<string, string>[] headers)
            {
                if (string.IsNullOrEmpty(method))
                    throw new ArgumentNullException("method");
                if (string.IsNullOrEmpty(resource))
                    throw new ArgumentNullException("resource");

                if (headers == null)
                    headers = new Tuple<string, string>[0];

                var ret = new EntityOperation
                {
                    entity = entity,
                    method = method,
                    resource = resource,
                    headers = headers
                };

                return ret;
            }
Example #5
0
        void AddOperation(CyanEntity entity,
                string method,
                string resource,
                params Tuple<string, string>[] headers)
        {
            ValidateEntity(entity.PartitionKey, entity.RowKey);

            operations.Add(EntityOperation.CreateOperation(entity, method, resource, headers));
        }
Example #6
0
        public async Task<CyanEntity> Merge(CyanEntity cyanEntity, bool unconditionalUpdate = false)
        {
            var partition = cyanEntity.PartitionKey;
            var row = cyanEntity.RowKey;
            var eTag = cyanEntity.ETag;

            var document = cyanEntity.Serialize();
            var resource = FormatResource(partition, row);

            var response =
                await RestClient.MergeRequest(resource, document.ToString(), unconditionalUpdate ? "*" : eTag).ConfigureAwait(false);

            string newETag;
            if (response.Headers.TryGetValue("ETag", out newETag))
                cyanEntity.ETag = HttpUtility.UrlDecode(newETag);

            response.ThrowIfFailed();

            return cyanEntity;
        }
Example #7
0
 public async Task Delete(CyanEntity cyanEntity)
 {
     var resource = FormatResource(cyanEntity.PartitionKey, cyanEntity.RowKey);
     var response = await RestClient.DeleteRequest(resource, cyanEntity.ETag).ConfigureAwait(false);
     response.ThrowIfFailed();
 }
Example #8
0
 public async Task<CyanEntity> Insert(CyanEntity cyanEntity)
 {
     var document = cyanEntity.Serialize();
     var response = await RestClient.PostRequest(TableName, document.ToString()).ConfigureAwait(false);
     response.ThrowIfFailed();
     return CyanSerializer.DeserializeEntity(response.ResponseBody.Root);
 }
Example #9
0
        internal static CyanEntity FromEntity(CyanEntity entity, params string[] fields)
        {
            var filterFields = new HashSet<string>(fields.GroupBy(f => f).Select(g => g.Key));

            var ret = new CyanEntity
            {
                PartitionKey = entity.PartitionKey,
                RowKey = entity.RowKey,
                ETag = entity.ETag,
                Timestamp = entity.Timestamp
            };

            var filteredFields = entity.Fields.Where(f => filterFields.Contains(f.Key));
            foreach (var field in filteredFields)
                ret.Fields.Add(field.Key, field.Value);

            return ret;
        }