Exemple #1
0
        /// <summary>
        /// Invokes the request without checking for status code.
        /// </summary>
        /// <param name="httpClient">HttpClient instance to be used to invoke the request.</param>
        /// <param name="token">CancellationToken instance to be used to cancel the execution of the request.</param>
        /// <returns>Result instance that contains the HttpStatusCode and the deserialized request response.</returns>
        protected async Task <Result <TResponse> > InvokeUncheckedAsync(HttpClient httpClient, CancellationToken token, object body = null)
        {
            Logger.Debug("Sending Http request:");

            string query = QueryStringSerializer.ToQueryString(this);

            var pathAndQuery = string.IsNullOrEmpty(query) ? Path : $"{Path}?{query}";

            var json = JsonConvert.SerializeObject(body, Formatting.Indented, new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            var httpRequest = new HttpRequestMessage(HttpMethod, new Uri(pathAndQuery, UriKind.Relative))
            {
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };

            Logger.Debug(httpRequest.ToString());

            var httpResponse = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token)
                               .ConfigureAwait(false);

            Logger.Debug($"Response with HTTP status code '{httpResponse.StatusCode}' received.");

            return(await ReadResponseAsync(httpResponse));
        }
        private Uri BuildRelativeUrl(string relativePath, object request = null)
        {
            var queryString = queryStringSerializer.ToQueryString(request);
            var url         = string.IsNullOrEmpty(queryString)
                ? relativePath
                : $"{relativePath}?{queryString}";

            return(new Uri(url, UriKind.Relative));
        }
        /// <summary>
        /// Invokes the request without checking for status code.
        /// </summary>
        /// <param name="httpClient">HttpClient instance to be used to invoke the request.</param>
        /// <param name="token">CancellationToken instance to be used to cancel the execution of the request.</param>
        /// <returns>Result instance that contains the HttpStatusCode and the deserialized request response.</returns>
        protected async Task <Result <TResponse> > InvokeUncheckedAsync(HttpClient httpClient, CancellationToken token)
        {
            string query = QueryStringSerializer.ToQueryString(this);

            var pathAndQuery = string.IsNullOrEmpty(query) ? Path : $"{Path}?{query}";

            var httpRequest = new HttpRequestMessage(HttpMethod, new Uri(pathAndQuery, UriKind.Relative));

            var httpResponse = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token)
                               .ConfigureAwait(false);

            return(await ReadResponseAsync(httpResponse));
        }
        public void TestParseGenerated()
        {
            BarCodeSettings settings = SettingsUtils.CreateTestSettings();

            string queryString = QueryStringSerializer.ToQueryString(settings);

            // re-parse querystring
            NameValueCollection queryStringCollection = MakeCollection(queryString);
            IBarCodeSettings    parsedSettings        = QueryStringSerializer.ParseQueryString(queryStringCollection);

            // compare outcome
            SettingsUtils.AssertSettingsEqual(settings, parsedSettings);
        }
Exemple #5
0
        public async Task <TResponse> InvokeAsync(HttpClient httpClient, CancellationToken token)
        {
            var query = QueryStringSerializer.ToQueryString(this);

            var pathAndQuery = string.IsNullOrEmpty(query) ? Path : $"{Path}?{query}";

            var httpRequest = new HttpRequestMessage(HttpMethod.Get, new Uri(pathAndQuery, UriKind.Relative));

            var httpResponse = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token)
                               .ConfigureAwait(false);

            httpResponse.EnsureSuccessStatusCode();

            return(await ReadResponse(httpResponse));
        }
        /// <summary>
        /// Invokes the request without checking for status code.
        /// </summary>
        /// <param name="httpClient">HttpClient instance to be used to invoke the request.</param>
        /// <param name="token">CancellationToken instance to be used to cancel the execution of the request.</param>
        /// <returns>Result instance that contains the HttpStatusCode and the deserialized request response.</returns>
        protected async Task <Result <TResponse> > InvokeUncheckedAsync(HttpClient httpClient, CancellationToken token)
        {
            Logger.Debug("Sending Http request:");

            string query = QueryStringSerializer.ToQueryString(this);

            var pathAndQuery = string.IsNullOrEmpty(query) ? Path : $"{Path}?{query}";

            var httpRequest = new HttpRequestMessage(HttpMethod, new Uri(pathAndQuery, UriKind.Relative));

            Logger.Debug(httpRequest.ToString());

            var httpResponse = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, token)
                               .ConfigureAwait(false);

            Logger.Debug($"Response with HTTP status code '{httpResponse.StatusCode}' received.");

            return(await ReadResponseAsync(httpResponse));
        }
        public void TestQueryString()
        {
            BarCodeSettings settings = SettingsUtils.CreateTestSettings();

            string queryString = QueryStringSerializer.ToQueryString(settings);

            StringAssert.Contains(MakePair(QueryStringSerializer.TYPE_KEY, settings.Type), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.DATA_KEY, settings.Data), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.UNIT_KEY, settings.Unit), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.DPI_KEY, settings.Dpi), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.BACKCOLOR_KEY, settings.BackColor.ToArgb()), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.BARCOLOR_KEY, settings.BarColor.ToArgb()), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.BARHEIGHT_KEY, settings.BarHeight), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.FONTCOLOR_KEY, settings.FontColor.ToArgb()), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.GUARDEXTRAHEIGHT_KEY, settings.GuardExtraHeight), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.MODULEWIDTH_KEY, settings.ModuleWidth), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.NARROWWIDTH_KEY, settings.NarrowWidth), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.WIDEWIDTH_KEY, settings.WideWidth), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.OFFSETHEIGHT_KEY, settings.OffsetHeight), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.OFFSETWIDTH_KEY, settings.OffsetWidth), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.FONT_KEY, (string)new FontConverter().ConvertTo(settings.Font, typeof(string))), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.TEXTPOSITION_KEY, settings.TextPosition), queryString);
            StringAssert.Contains(MakePair(QueryStringSerializer.USECHECKSUM_KEY, settings.UseChecksum), queryString);
        }