Esempio n. 1
0
        public async Task <IConnectionResponse> SendAsync(object body)
        {
            logger.LogInformation("Sending request of type {0} to {1}", body.GetType(), apiName);

            // if we are in method read -> try to serialise to query string
            if (method == PrivateConnectionMethod.Read || method == PrivateConnectionMethod.Delete)
            {
                var builder = new StringBuilder(1024, 1024);
                using (var writer = new StringWriter(builder))
                    using (var logged = writer.LogWriterDebug(logger, "Generated URL {0}"))
                        using (var urlWriter = new UrlWriter(writer))
                        {
                            try
                            {
                                logger.LogDebug("Attempting to use Get Query");
                                new JsonSerializer().Serialize(urlWriter, body);
                                writer.Flush();
                                queryString = BuildQuery(writer.ToString());
                                httpMethod  = method == PrivateConnectionMethod.Read ? HttpMethod.Get : HttpMethod.Delete;
                                return(await SendAsync(null).ConfigureAwait(false));
                            }
                            catch (Exception e)
                            {
                                logger.LogDebug("Get query failed, using fallback", e);
                                queryString = BuildQuery(method);
                                httpMethod  = HttpMethod.Post;
                            }
                        }
            }
            else
            {
                queryString = BuildQuery();
                httpMethod  = method == PrivateConnectionMethod.Update ? HttpMethod.Put : HttpMethod.Post;
            }

            // serialise body at this point.
            using (var stream = new MemoryStream())
                using (var streamWriter = new StreamWriter(stream))
                    using (var writer = streamWriter.LogWriterDebug(logger, "Serialised Content {0}"))
                    {
                        JsonSerializer s = new JsonSerializer {
                            NullValueHandling = NullValueHandling.Ignore
                        };
                        if (method == PrivateConnectionMethod.Form)
                        {
                            contentType = "application/x-www-form-urlencoded";
                            using (var urlWriter = new UrlWriter(writer)
                            {
                                CloseOutput = false
                            })
                            {
                                s.Serialize(urlWriter, body);
                            }
                        }
                        else
                        {
                            contentType = "application/json";
                            using (var jsonWriter = new JsonTextWriter(writer)
                            {
                                CloseOutput = false
                            })
                            {
                                s.Serialize(jsonWriter, body);
                            }
                        }
                        writer.Flush();
                        stream.Position = 0;
                        return(await SendAsync(stream).ConfigureAwait(false));
                    }
        }
Esempio n. 2
0
        public void ComplexObjectSerialisesCorrectly()
        {
            var obj = new AnnotatedObject
            {
                TestInt = 42,
                TestKey = "wow-dude",
                TestObj = new AnnotatedObject
                {
                    TestInt  = 123,
                    TestKey  = "inner value with % escaping",
                    TestObj2 = new AnnotatedObject2 {
                        TestInt2 = 666, TestKey2 = "further inner value"
                    },
                    TestArray = new List <int> {
                        5, 3, 2
                    },
                    TestObjArray = new List <AnnotatedObject2>
                    {
                        new AnnotatedObject2 {
                            TestInt2 = 656, TestKey2 = "further inner value in array with key 0"
                        },
                        new AnnotatedObject2 {
                            TestInt2 = 676, TestKey2 = "further inner value in array with key 1"
                        },
                        new AnnotatedObject2 {
                            TestInt2 = 686, TestKey2 = "further inner value in array with key 2"
                        },
                    }
                },
                TestObj2 = new AnnotatedObject2 {
                    TestInt2 = 52, TestKey2 = "innervalue"
                },
                TestObjArray = new List <AnnotatedObject2>
                {
                    new AnnotatedObject2 {
                        TestInt2 = 656, TestKey2 = "value in array with key 0"
                    },
                    new AnnotatedObject2 {
                        TestInt2 = 676, TestKey2 = "value in array with key 1"
                    },
                    new AnnotatedObject2 {
                        TestInt2 = 686, TestKey2 = "value in array with key 2"
                    },
                }
            };

            var expected = "TestKey=wow-dude"
                           + "&TestInt=42"
                           + "&TestObj%5BTestKey%5D=inner+value+with+%25+escaping"
                           + "&TestObj%5BTestInt%5D=123"
                           + "&TestObj%5BOverridenName%5D%5BTestKey2%5D=further+inner+value"
                           + "&TestObj%5BOverridenName%5D%5BTestInt2%5D=666"
                           + "&TestObj%5BTestArray%5D%5B0%5D=5"
                           + "&TestObj%5BTestArray%5D%5B1%5D=3"
                           + "&TestObj%5BTestArray%5D%5B2%5D=2"
                           + "&TestObj%5BTestObjArray%5D%5B0%5D%5BTestKey2%5D=further+inner+value+in+array+with+key+0"
                           + "&TestObj%5BTestObjArray%5D%5B0%5D%5BTestInt2%5D=656"
                           + "&TestObj%5BTestObjArray%5D%5B1%5D%5BTestKey2%5D=further+inner+value+in+array+with+key+1"
                           + "&TestObj%5BTestObjArray%5D%5B1%5D%5BTestInt2%5D=676"
                           + "&TestObj%5BTestObjArray%5D%5B2%5D%5BTestKey2%5D=further+inner+value+in+array+with+key+2"
                           + "&TestObj%5BTestObjArray%5D%5B2%5D%5BTestInt2%5D=686"
                           + "&OverridenName%5BTestKey2%5D=innervalue"
                           + "&OverridenName%5BTestInt2%5D=52"
                           + "&TestObjArray%5B0%5D%5BTestKey2%5D=value+in+array+with+key+0"
                           + "&TestObjArray%5B0%5D%5BTestInt2%5D=656"
                           + "&TestObjArray%5B1%5D%5BTestKey2%5D=value+in+array+with+key+1"
                           + "&TestObjArray%5B1%5D%5BTestInt2%5D=676"
                           + "&TestObjArray%5B2%5D%5BTestKey2%5D=value+in+array+with+key+2"
                           + "&TestObjArray%5B2%5D%5BTestInt2%5D=686";


            using (var textWriter = new StringWriter())
                using (var urlWriter = new UrlWriter(textWriter))
                {
                    JsonSerializer s = new JsonSerializer();
                    s.Serialize(urlWriter, obj);
                    textWriter.Flush();
                    Assert.That(textWriter.ToString(), Is.EqualTo(expected));
                }
        }
Esempio n. 3
0
        public async Task <IConnectionResponse> SendAsync(object body)
        {
            // if we are in method read -> try to serialise to query string
            if (method == PrivateConnectionMethod.Read || method == PrivateConnectionMethod.Delete)
            {
                var builder = new StringBuilder(1024, 1024);
                using (var writer = new StringWriter(builder))
                    using (var urlWriter = new UrlWriter(writer))
                    {
                        try
                        {
                            new JsonSerializer().Serialize(urlWriter, body);
                            writer.Flush();
                            queryString = BuildQuery(writer.ToString());
                            httpMethod  = method == PrivateConnectionMethod.Read ? HttpMethod.Get : HttpMethod.Delete;
                            return(await SendAsync(null).ConfigureAwait(false));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception: {0}", ex.Message);
                            queryString = BuildQuery(method);
                            httpMethod  = HttpMethod.Post;
                        }
                    }
            }
            else
            {
                queryString = BuildQuery();
                httpMethod  = method == PrivateConnectionMethod.Update ? HttpMethod.Put : HttpMethod.Post;
            }

            // serialise body at this point.
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                {
                    JsonSerializer s = new JsonSerializer();
                    if (method == PrivateConnectionMethod.Form)
                    {
                        contentType = "application/x-www-form-urlencoded";
                        using (var urlWriter = new UrlWriter(writer)
                        {
                            CloseOutput = false
                        })
                        {
                            s.Serialize(urlWriter, body);
                        }
                    }
                    else
                    {
                        contentType = "application/json";
                        using (var jsonWriter = new JsonTextWriter(writer)
                        {
                            CloseOutput = false
                        })
                        {
                            s.Serialize(jsonWriter, body);
                        }
                    }
                    writer.Flush();
                    stream.Position = 0;
                    return(await SendAsync(stream).ConfigureAwait(false));
                }
        }