Example #1
0
        private T callReturn <T>(string path,
                                 string method,
                                 object body,
                                 int timeout,
                                 int retry,
                                 bool throwable,
                                 Encoding encoding,
                                 TransferCallback uploadProgress,
                                 TransferCallback downloadProgress,
                                 IDictionary <string, string> headers,
                                 CookieCollection cookies)
        {
            var t = 0;

            Exception error = null;

            while (t++ < retry)
            {
                try
                {
                    var request = createRequest(path, method, timeout, headers, cookies);

                    var stream = request.GetRequestStream();

                    if (body != null)
                    {
                        writeIntoStream(stream, body, encoding, uploadProgress);
                    }

                    var respose = request.GetResponse();

                    var contentLengh = respose.ContentLength;

                    return(readStream <T>(contentLengh, respose.GetResponseStream(), encoding, downloadProgress));
                }
                catch (Exception ex)
                {
                    ErrorInterceptor?.Invoke(ex);
                    error = ex;
                }
            }

            if (throwable && error != null)
            {
                throw error;
            }
            else
            {
                return(default(T));
            }
        }
Example #2
0
        public void Intercept_Schema_Error()
        {
            // arrange
            var errorInterceptor = new ErrorInterceptor();

            // act
            void Action() => SchemaBuilder.New()
            .TryAddSchemaInterceptor(errorInterceptor)
            .Create();

            // assert
            Assert.Throws <SchemaException>(Action);
            Assert.Collection(
                errorInterceptor.Exceptions,
                ex => Assert.IsType <SchemaException>(ex));
        }
Example #3
0
        private async Task callNoReturnAsync(string path,
                                             string method,
                                             object body,
                                             int timeout,
                                             int retry,
                                             bool throwable,
                                             Encoding encoding,
                                             TransferCallback uploadProgress,
                                             IDictionary <string, string> headers,
                                             CookieCollection cookies)
        {
            var t = 0;

            Exception error = null;

            while (t++ < retry)
            {
                try
                {
                    var request = createRequest(path, method, timeout, headers, cookies);

                    var stream = await request.GetRequestStreamAsync().ConfigureAwait(false);

                    if (body != null)
                    {
                        await  writeIntoStreamAsync(stream, body, encoding, uploadProgress).ConfigureAwait(false);
                    }

                    await request.GetResponseAsync().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    ErrorInterceptor?.Invoke(ex);
                    error = ex;
                }
            }

            if (throwable && error != null)
            {
                throw error;
            }
        }