/// <summary>
        /// Remove a device from the DocumentDB. If it succeeds the method will return asynchronously.
        /// If it fails for any reason it will let any exception thrown bubble up.
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task DeleteDeviceAsync(dynamic device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            string rid      = DeviceSchemaHelper.GetDocDbRid(device);
            string endpoint = string.Format("{0}dbs/{1}/colls/{2}/docs/{3}", _docDbEndpoint, _dbId, _collectionId, rid);

            await PerformRestCallAsync(endpoint, DELETE_VERB, DocDbResourceType.Document, rid, "");
        }
        /// <summary>
        /// Update the record for an existing device.
        /// </summary>
        /// <param name="updatedDevice"></param>
        /// <returns></returns>
        public async Task <JObject> UpdateDeviceAsync(dynamic updatedDevice)
        {
            if (updatedDevice == null)
            {
                throw new ArgumentNullException("updatedDevice");
            }

            string rid      = DeviceSchemaHelper.GetDocDbRid(updatedDevice);
            string endpoint = string.Format("{0}dbs/{1}/colls/{2}/docs/{3}", _docDbEndpoint, _dbId, _collectionId, rid);
            string response = await PerformRestCallAsync(endpoint, PUT_VERB, DocDbResourceType.Document, rid, updatedDevice.ToString());

            return(JObject.Parse(response));
        }
        /// <summary>
        /// Remove a device from the DocumentDB. If it succeeds the method will return asynchronously.
        /// If it fails for any reason it will let any exception thrown bubble up.
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task DeleteDeviceAsync(dynamic device)
        {
            string rid = DeviceSchemaHelper.GetDocDbRid(device);

            WebClient client = new WebClient();

            BuildHeaders(client);
            client.Headers.Add(AUTHORIZATION_HEADER_KEY, GetAuthorizationToken("DELETE", DOCUMENTS_RESOURCE_TYPE, rid));

            string endpoint = string.Format("{0}dbs/{1}/colls/{2}/docs/{3}", _docDbEndpoint, _dbId, _collectionId, rid);

            await AzureRetryHelper.OperationWithBasicRetryAsync(async() =>
                                                                await client.UploadStringTaskAsync(endpoint, "DELETE", ""));
        }
        /// <summary>
        /// Update the record for an existing device.
        /// </summary>
        /// <param name="updatedDevice"></param>
        /// <returns></returns>
        public async Task <JObject> UpdateDeviceAsync(dynamic updatedDevice)
        {
            string rid = DeviceSchemaHelper.GetDocDbRid(updatedDevice);

            WebClient client = new WebClient();

            BuildHeaders(client);
            client.Headers.Add(AUTHORIZATION_HEADER_KEY, GetAuthorizationToken("PUT", DOCUMENTS_RESOURCE_TYPE, rid));

            string endpoint = string.Format("{0}dbs/{1}/colls/{2}/docs/{3}", _docDbEndpoint, _dbId, _collectionId, rid);

            string response = await AzureRetryHelper.OperationWithBasicRetryAsync <string>(async() =>
                                                                                           await client.UploadStringTaskAsync(endpoint, "PUT", updatedDevice.ToString()));

            return(JObject.Parse(response));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Updates an existing device in the DocumentDB
        /// Throws a DeviceNotRegisteredException is the device does not already exist in the DocumentDB
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <dynamic> UpdateDeviceAsync(dynamic device)
        {
            string deviceId = DeviceSchemaHelper.GetDeviceID(device);

            dynamic existingDevice = await GetDeviceAsync(deviceId);

            if (existingDevice == null)
            {
                throw new DeviceNotRegisteredException(deviceId);
            }

            string incomingRid = DeviceSchemaHelper.GetDocDbRid(device);

            if (string.IsNullOrWhiteSpace(incomingRid))
            {
                // copy the existing _rid onto the incoming data if needed
                var existingRid = DeviceSchemaHelper.GetDocDbRid(existingDevice);
                if (string.IsNullOrWhiteSpace(existingRid))
                {
                    throw new InvalidOperationException("Could not find _rid property on existing device");
                }
                device._rid = existingRid;
            }

            string incomingId = DeviceSchemaHelper.GetDocDbId(device);

            if (string.IsNullOrWhiteSpace(incomingId))
            {
                // copy the existing id onto the incoming data if needed
                var existingId = DeviceSchemaHelper.GetDocDbId(existingDevice);
                if (string.IsNullOrWhiteSpace(existingId))
                {
                    throw new InvalidOperationException("Could not find id property on existing device");
                }
                device.id = existingId;
            }

            DeviceSchemaHelper.UpdateUpdatedTime(device);

            device = await _docDbRestUtil.UpdateDocumentAsync(device);

            return(device);
        }