예제 #1
0
        public override Task <Device> AddDeviceAsync(Device device, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            ValidateDeviceId(device);

            if (!string.IsNullOrEmpty(device.ETag))
            {
                throw new ArgumentException(ApiResources.ETagSetWhileRegisteringDevice);
            }

            ValidateDeviceAuthentication(device.Authentication, device.Id);

            // auto generate keys if not specified
            if (device.Authentication == null)
            {
                device.Authentication = new AuthenticationMechanism();
            }

            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >
            {
                {
                    HttpStatusCode.PreconditionFailed,
                    async responseMessage => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage))
                }
            };

            return(this.httpClientHelper.PutAsync(GetRequestUri(device.Id), device, PutOperationType.CreateEntity, errorMappingOverrides, cancellationToken));
        }
예제 #2
0
        private Task <JobResponse> CreateJobAsync(JobRequest jobRequest, CancellationToken cancellationToken)
        {
            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >
            {
                {
                    HttpStatusCode.PreconditionFailed,
                    async(responseMessage) =>
                    new PreconditionFailedException(
                        await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage).ConfigureAwait(false))
                },
                {
                    HttpStatusCode.NotFound, async responseMessage =>
                    {
                        string responseContent = await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage).ConfigureAwait(false);

                        return((Exception) new DeviceNotFoundException(responseContent, (Exception)null));
                    }
                }
            };

            return(_httpClientHelper.PutAsync <JobRequest, JobResponse>(
                       GetJobUri(jobRequest.JobId),
                       jobRequest,
                       errorMappingOverrides,
                       cancellationToken));
        }
예제 #3
0
        public override Task <Device> UpdateDeviceAsync(Device device, bool forceUpdate, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            ValidateDeviceId(device);

            if (string.IsNullOrWhiteSpace(device.ETag) && !forceUpdate)
            {
                throw new ArgumentException(ApiResources.ETagNotSetWhileUpdatingDevice);
            }

            ValidateDeviceAuthentication(device.Authentication, device.Id);

            // auto generate keys if not specified
            if (device.Authentication == null)
            {
                device.Authentication = new AuthenticationMechanism();
            }

            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >()
            {
                { HttpStatusCode.PreconditionFailed, async(responseMessage) => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) },
                { HttpStatusCode.NotFound, async responseMessage =>
                  {
                      string responseContent = await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage);

                      return((Exception) new DeviceNotFoundException(responseContent, (Exception)null));
                  } }
            };

            PutOperationType operationType = forceUpdate ? PutOperationType.ForceUpdateEntity : PutOperationType.UpdateEntity;

            return(this.httpClientHelper.PutAsync(GetRequestUri(device.Id), device, operationType, errorMappingOverrides, cancellationToken));
        }
        Task RemoveDeviceAsync(string deviceId, IETagHolder eTagHolder, CancellationToken cancellationToken)
        {
            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >();

            errorMappingOverrides.Add(HttpStatusCode.NotFound, async responseMessage =>
            {
                var responseContent = await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage);
                return((Exception) new DeviceNotFoundException(responseContent, (Exception)null));
            });
            errorMappingOverrides.Add(HttpStatusCode.PreconditionFailed, async(responseMessage) => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)));
            return(this.httpClientHelper.DeleteAsync(GetRequestUri(deviceId), eTagHolder, errorMappingOverrides, null, cancellationToken));
        }
예제 #5
0
        Task <Twin> UpdateTwinInternalAsync(string deviceId, Twin twin, string etag, CancellationToken cancellationToken, bool isReplace)
        {
            this.EnsureInstanceNotClosed();

            if (twin != null)
            {
                twin.DeviceId = deviceId;
            }

            ValidateTwinId(twin);

            if (string.IsNullOrEmpty(etag))
            {
                throw new ArgumentNullException(nameof(etag));
            }

            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >
            {
                {
                    HttpStatusCode.PreconditionFailed,
                    async responseMessage => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage))
                },
                {
                    HttpStatusCode.NotFound,
                    async responseMessage => new DeviceNotFoundException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage), (Exception)null)
                }
            };

            if (isReplace)
            {
                return(this.httpClientHelper.PutAsync <Twin, Twin>(
                           GetTwinUri(deviceId),
                           twin,
                           etag,
                           etag == WildcardEtag ? PutOperationType.ForceUpdateEntity : PutOperationType.UpdateEntity,
                           errorMappingOverrides,
                           cancellationToken));
            }
            else
            {
                return(this.httpClientHelper.PatchAsync <Twin, Twin>(
                           GetTwinUri(deviceId),
                           twin,
                           etag,
                           etag == WildcardEtag ? PutOperationType.ForceUpdateEntity : PutOperationType.UpdateEntity,
                           errorMappingOverrides,
                           cancellationToken));
            }
        }
예제 #6
0
        private static async Task <Exception> MapToExceptionAsync(
            HttpResponseMessage response,
            IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > errorMapping)
        {
            if (!errorMapping.TryGetValue(response.StatusCode, out Func <HttpResponseMessage, Task <Exception> > func))
            {
                return(new IotHubException(
                           await ExceptionHandlingHelper.GetExceptionMessageAsync(response).ConfigureAwait(false),
                           isTransient: true));
            }

            Func <HttpResponseMessage, Task <Exception> > mapToExceptionFunc = errorMapping[response.StatusCode];
            Task <Exception> exception = mapToExceptionFunc(response);

            return(await exception.ConfigureAwait(false));
        }
        public override Task <Device> GetDeviceAsync(string deviceId, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(deviceId))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrWhitespace, "deviceId"));
            }

            this.EnsureInstanceNotClosed();
            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >();

            errorMappingOverrides.Add(HttpStatusCode.NotFound, async responseMessage =>
            {
                return((Exception) new DeviceNotFoundException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)));
            });
            return(this.httpClientHelper.GetAsync <Device>(GetRequestUri(deviceId), errorMappingOverrides, null, false, cancellationToken));
        }
예제 #8
0
        static async Task <Exception> MapToExceptionAsync(
            HttpResponseMessage response,
            IDictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > > errorMapping)
        {
            Func <HttpResponseMessage, Task <Exception> > func;

            if (!errorMapping.TryGetValue(response.StatusCode, out func))
            {
                return(new IotHubException(
                           await ExceptionHandlingHelper.GetExceptionMessageAsync(response),
                           isTransient: true));
            }

            var mapToExceptionFunc = errorMapping[response.StatusCode];
            var exception          = mapToExceptionFunc(response);

            return(await exception);
        }
예제 #9
0
        Task <T> BulkDeviceOperationsAsync <T>(IEnumerable <ExportImportDevice> devices, string version, CancellationToken cancellationToken)
        {
            this.BulkDeviceOperationSetup(devices);

            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >
            {
                { HttpStatusCode.PreconditionFailed, async responseMessage => new PreconditionFailedException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) },
                { HttpStatusCode.RequestEntityTooLarge, async responseMessage => new TooManyDevicesException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) },
                { HttpStatusCode.BadRequest, async responseMessage => new ArgumentException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) }
            };

            return(this.httpClientHelper.PostAsync <IEnumerable <ExportImportDevice>, T>(GetBulkRequestUri(version), devices, errorMappingOverrides, null, cancellationToken));
        }
예제 #10
0
        Task <string[]> BulkDeviceOperationsAsync(IEnumerable <ExportImportDevice> devices, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            if (devices == null)
            {
                throw new ArgumentNullException("devices");
            }

            foreach (var device in devices)
            {
                // auto generate keys if not specified
                if (device.Authentication == null)
                {
                    device.Authentication = new AuthenticationMechanism();
                }

                ValidateDeviceAuthentication(device.Authentication);
            }

            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >();

            errorMappingOverrides.Add(HttpStatusCode.RequestEntityTooLarge, async(responseMessage) => new TooManyDevicesException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)));
            errorMappingOverrides.Add(HttpStatusCode.BadRequest, async(responseMessage) => new ArgumentException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)));

            return(this.httpClientHelper.PostAsync <IEnumerable <ExportImportDevice>, string[]>(GetRequestUri(string.Empty), devices, errorMappingOverrides, null, cancellationToken));
        }