示例#1
0
        /// <summary>
        /// Optimize a source image using a custom request and wait for the optimization to complete.
        /// </summary>
        /// <param name="request">An OptimizeRequest instance.</param>
        /// <returns>An OptimizeResponse.</returns>
        public async Task <OptimizeResponse> OptimizeWaitAsync(
            OptimizeRequest request)
        {
            //  Check if the request is null and return an invalid response if it is.

            if (request is null)
            {
                return(NullRequestResponse);
            }

            //  Check if a file name was provided.

            if (!request.FileName.HasValue())
            {
                return(InvalidFileNameResponse);
            }

            if (request.Resize.HasItems())
            {
                //  Check if the resize count is more than the max allowed.

                if (request.Resize.Count() > MaxResizeCount)
                {
                    return(InvalidResizeCountResponse);
                }

                //  Validate if the resize dimensions are correct.

                var validResizeDimensions = ValidateResizeDimensions(request.Resize);

                if (!validResizeDimensions)
                {
                    return(InvalidResizeDimensionsResponse);
                }
            }

            //  Make sure to update the request to be synchronous.

            request.Wait = true;

            //  Get the request's response and deserialize it into an OptimizeResponse instance.

            var response = await GetResponseAsync(request).ConfigureAwait(false);

            var optimizeWaitResponse = JsonConvert.DeserializeObject <OptimizeResponse>(response);

            if (_debug)
            {
                optimizeWaitResponse.Json = response;
            }

            return(optimizeWaitResponse);
        }
示例#2
0
        //  ========================================================================
        //  Response
        //  ========================================================================

        private static ByteArrayContent GetByteContent(
            OptimizeRequest request) => request is null ? null : new ByteArrayContent(request.FileBlob);