Example #1
0
        static async Task Main(string[] args)
        {
            //using (var client = new HypermediaSampleClient("http://hypermediasamplewebapi.azurewebsites.net/", ""))
            using (var client = new HypermediaSampleClient("http://localhost:50419/", ""))
            {
                //client.BatchUpdateAsync(new [] { new CommentResource() }).Wait();
                //client.UpdateAsync(new CommentResource { Text = "Hello World!" }).Wait();
                client.CreateAsync(new CommentResource {
                    Text = "Hello World!"
                }).Wait();

                //var posts = client.GetPostsAsync().Result;

                //foreach (var post in posts)
                //{
                //    Console.WriteLine(post.Title);
                //}
            }

            //var contractResolver = new Builder()
            //    //.With<PostResource>("posts")
            //    //    .Id(nameof(PostResource.Id))
            //    .Build();

            //using (var httpClient = new HttpClient { BaseAddress = new Uri("http://localhost:50419") })
            //{
            //    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.api+json"));

            //    var response = await httpClient.GetAsync($"v1/posts?skip=0&take=1");
            //    response.EnsureSuccessStatusCode();

            //    var serializerOptions = new JsonApiSerializerOptions(contractResolver)
            //    {
            //        MissingContractHandler = context =>
            //        {
            //            Console.WriteLine("The resource '{0}' is missing", context.Type);
            //            return null;
            //        }
            //    };

            //    //var entities = await response.Content.ReadAsJsonApiManyAsync<Entity>(new CustomContractResolver(contractResolver));
            //    var entities = await response.Content.ReadAsJsonApiManyAsync<Entity>(serializerOptions);

            //    foreach (var entity in entities)
            //    {
            //        Console.WriteLine(entity.Id);
            //        Console.WriteLine(entity.CreationDate);

            //        foreach (var jsonMember in ((IJsonExtension) entity).Data)
            //        {
            //            Console.WriteLine("{0} = {1}", jsonMember.Name, jsonMember.Value.Stringify());
            //        }
            //    }
            //}
        }
        public void CanDeserializeNullBelongsRelationship()
        {
            // arrange
            var serializer = new JsonApiSerializer(HypermediaSampleClient.CreateResolver());

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserializeNullBelongsRelationship)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Null(resource.OwnerUser);
        }
Example #3
0
        static void Main(string[] args)
        {
            //var timespan = TimeSpan.FromDays(1);

            //var x = TimeSpan.Parse(timespan.ToString());
            //Console.WriteLine(x);

            using (var client = new HypermediaSampleClient("http://hypermediasamplewebapi.azurewebsites.net/", ""))
            {
                var post = client.GetPostByIdAsync(38).Result;
            }
        }
        public void CanDeserialize()
        {
            // arrange
            var serializer = new JsonApiSerializer(HypermediaSampleClient.CreateResolver());

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserialize)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Equal("Did the Greeks build temples for all of the children of Cronus?", resource.Title);
            Assert.Equal("kuwaly", resource.OwnerUser.DisplayName);
        }
Example #5
0
        static void Main(string[] args)
        {
            //var timespan = TimeSpan.FromDays(1);

            //var x = TimeSpan.Parse(timespan.ToString());
            //Console.WriteLine(x);

            using (var client = new HypermediaSampleClient("http://hypermediasamplewebapi.azurewebsites.net/", ""))
            //using (var client = new HypermediaSampleClient("http://localhost:59074/", ""))
            {
                client.BatchUpdateAsync(new [] { new CommentResource() }).Wait();
            }
        }
        public void CanDeserializeNullHasManyRelationship()
        {
            // arrange
            var serializer = new JsonApiSerializer(new JsonApiSerializerOptions {
                ContractResolver = HypermediaSampleClient.CreateResolver()
            });

            // act
            var resource = (PostResource)serializer.Deserialize(JsonContent.GetObject(nameof(CanDeserializeNullHasManyRelationship)));

            // assert
            Assert.Equal(2, resource.Id);
            Assert.Null(resource.Comments);
        }
Example #7
0
        // TODO:
        // - SerializeAsEmbedded relationships
        // - BackingField field could look at the parent object if it is not set (ie, default or null)
        // - BackingField should not serialize if the parent is being serialized as a relationship, ie, "user-id" shouldn't be returned

        static void Main(string[] args)
        {
            const string Endpoint = "http://hypermedia.cainosullivan.com";

            //const string Endpoint = "http://localhost:59074/";
            using (var client = new HypermediaSampleClient(Endpoint, ""))
            {
                //var posts = client.GetPostsAsync().Result;
                //foreach (var post in posts)
                //{
                //    Console.WriteLine(post.ViewCount);
                //}

                var post = client.GetPostByIdAsync(38).Result;
                Console.WriteLine(post.ViewCount);
                Console.WriteLine(post.OwnerUserId);
                Console.WriteLine(post.OwnerUser.DisplayName);

                foreach (var comment in post.Comments)
                {
                    Console.WriteLine("{0}: {1}", comment.Id, comment.Text);
                }
            }
        }