Exemple #1
0
        public async Task <T> GetResult <T>()
        {
            waitForRequestToBeConfigured.TrySetResult(true);
            HttpResponseMessage resp = await request;

            if (typeof(T).IsCastableTo <Exception>() && resp.StatusCode.IsErrorStatus())
            {
                return((T)(object)new NoSuccessError(resp.StatusCode, await GetResult <string>()));
            }
            if (HttpStatusCode.OK != resp.StatusCode)
            {
                Log.w("response.StatusCode=" + resp.StatusCode);
            }
            if (TypeCheck.AreEqual <T, HttpResponseMessage>())
            {
                return((T)(object)resp);
            }
            if (TypeCheck.AreEqual <T, HttpStatusCode>())
            {
                return((T)(object)resp.StatusCode);
            }
            if (TypeCheck.AreEqual <T, Headers>())
            {
                return((T)(object) await GetResultHeaders());
            }
            HttpContent content = resp.Content;

            if (TypeCheck.AreEqual <T, HttpContent>())
            {
                return((T)(object)content);
            }
            if (TypeCheck.AreEqual <T, Stream>())
            {
                return((T)(object)await content.ReadAsStreamAsync());
            }
            if (TypeCheck.AreEqual <T, byte[]>())
            {
                return((T)(object)await content.ReadAsByteArrayAsync());
            }
            var respText = await content.ReadAsStringAsync();

            if (typeof(T) == typeof(string))
            {
                return((T)(object)respText);
            }
            AssertV2.IsNotNull(respText, "respText");
            AssertV2.IsNotNull(respText.IsNullOrEmpty(), "respText.IsNullOrEmpty");
            try { return(jsonReader.Read <T>(respText)); } catch (JsonReaderException e) { throw new JsonReaderException("Cant parse to JSON: " + respText, e); }
        }
Exemple #2
0
        public async Task <T> GetResult <T>()
        {
            HttpResponseMessage resp = await request;

            if (typeof(T).IsCastableTo <Exception>() && resp.StatusCode.IsErrorStatus())
            {
                return((T)(object)new NoSuccessError(resp.StatusCode, await GetResult <string>()));
            }
            AssertV2.IsTrue(HttpStatusCode.OK == resp.StatusCode, "response.StatusCode=" + resp.StatusCode);
            if (TypeCheck.AreEqual <T, HttpResponseMessage>())
            {
                return((T)(object)resp);
            }
            if (TypeCheck.AreEqual <T, HttpStatusCode>())
            {
                return((T)(object)resp.StatusCode);
            }
            if (TypeCheck.AreEqual <T, Headers>())
            {
                return((T)(object) await GetResultHeaders());
            }
            HttpContent content = resp.Content;

            if (TypeCheck.AreEqual <T, HttpContent>())
            {
                return((T)(object)content);
            }
            if (TypeCheck.AreEqual <T, Stream>())
            {
                return((T)(object)await content.ReadAsStreamAsync());
            }
            if (TypeCheck.AreEqual <T, byte[]>())
            {
                return((T)(object)await content.ReadAsByteArrayAsync());
            }
            var respText = await content.ReadAsStringAsync();

            if (typeof(T) == typeof(string))
            {
                return((T)(object)respText);
            }
            AssertV2.IsNotNull(respText, "respText");
            AssertV2.IsNotNull(respText.IsNullOrEmpty(), "respText.IsNullOrEmpty");
            return(jsonReader.Read <T>(respText));
        }
Exemple #3
0
        public async Task <T> GetResult <T>()
        {
            var c = (await request).Content;

            if (TypeCheck.AreEqual <T, Stream>())
            {
                return((T)(object)await c.ReadAsStreamAsync());
            }
            if (TypeCheck.AreEqual <T, byte[]>())
            {
                return((T)(object)await c.ReadAsByteArrayAsync());
            }
            var respText = await c.ReadAsStringAsync();

            if (typeof(T) == typeof(string))
            {
                return((T)(object)respText);
            }
            return(jsonReader.Read <T>(respText));
        }
        public void TypeExtension_Examples()
        {
            Type MySubClass1 = typeof(MySubClass1);

            // type.IsSubclassOf<..>() examples:
            Assert.True(MySubClass1.IsSubclassOf <MyClass1>());
            Assert.False(MySubClass1.IsSubclassOf <MySubClass2>());
            Assert.True(typeof(MySubClass2).IsSubclassOf <MyClass1>());
            Assert.False(typeof(MyClass1).IsSubclassOf <MySubClass1>());

            // Checking if 2 types are equal using the TypeCheck class:
            Assert.True(TypeCheck.AreEqual <MyClass1, MyClass1>());
            Assert.False(TypeCheck.AreEqual <MySubClass1, MyClass1>());

            // type.IsCastableTo<..>() examples:
            Assert.True(typeof(MySubClass2).IsCastableTo <MyClass1>());
            Assert.False(typeof(MyClass1).IsCastableTo <MySubClass2>());

            // type.IsCastableTo(..) examples:
            Assert.True(typeof(MySubClass1).IsCastableTo(typeof(MyClass1)));
            Assert.True(typeof(MyClass1).IsCastableTo(typeof(MyClass1)));
            Assert.False(typeof(MyClass1).IsCastableTo(typeof(MySubClass1)));
        }