From 893564f7d1425559b30a37e3a4664093d9973c1e Mon Sep 17 00:00:00 2001 From: nikolavn Date: Mon, 9 Nov 2015 14:25:07 +0200 Subject: [PATCH] Added Consuming Web Services homework --- .../04.ConsumingWebServices.sln | 22 +++++++ .../App.config | 6 ++ .../ConsumingServicesJSONPlaceHolder.csproj | 66 +++++++++++++++++++ .../ConsumingServicesJSONPlaceHolder/Post.cs | 16 +++++ .../Properties/AssemblyInfo.cs | 36 ++++++++++ .../Startup.cs | 62 +++++++++++++++++ .../packages.config | 4 ++ 7 files changed, 212 insertions(+) create mode 100644 Web Services And Cloud/04.ConsumingWebServices/04.ConsumingWebServices.sln create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/App.config create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/ConsumingServicesJSONPlaceHolder.csproj create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Post.cs create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Properties/AssemblyInfo.cs create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Startup.cs create mode 100644 Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/packages.config diff --git a/Web Services And Cloud/04.ConsumingWebServices/04.ConsumingWebServices.sln b/Web Services And Cloud/04.ConsumingWebServices/04.ConsumingWebServices.sln new file mode 100644 index 0000000..086d629 --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/04.ConsumingWebServices.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumingServicesJSONPlaceHolder", "ConsumingServicesJSONPlaceHolder\ConsumingServicesJSONPlaceHolder.csproj", "{9DD1FB73-ED6E-475C-989A-ECAD723DBA13}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9DD1FB73-ED6E-475C-989A-ECAD723DBA13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DD1FB73-ED6E-475C-989A-ECAD723DBA13}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DD1FB73-ED6E-475C-989A-ECAD723DBA13}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DD1FB73-ED6E-475C-989A-ECAD723DBA13}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/App.config b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/App.config new file mode 100644 index 0000000..9c05822 --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/ConsumingServicesJSONPlaceHolder.csproj b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/ConsumingServicesJSONPlaceHolder.csproj new file mode 100644 index 0000000..3bb4bb0 --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/ConsumingServicesJSONPlaceHolder.csproj @@ -0,0 +1,66 @@ + + + + + Debug + AnyCPU + {9DD1FB73-ED6E-475C-989A-ECAD723DBA13} + Exe + Properties + ConsumingServicesJSONPlaceHolder + ConsumingServicesJSONPlaceHolder + v4.5.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.8.0.1-beta1\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Post.cs b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Post.cs new file mode 100644 index 0000000..d4e1c3f --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Post.cs @@ -0,0 +1,16 @@ +namespace ConsumingServicesJSONPlaceHolder +{ + using Newtonsoft.Json; + + public class Post + { + [JsonProperty("id")] + public int Id { get; set; } + + [JsonProperty("title")] + public string Title { get; set; } + + [JsonProperty("body")] + public string Body { get; set; } + } +} diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Properties/AssemblyInfo.cs b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..fc981fb --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ConsumingServicesJSONPlaceHolder")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConsumingServicesJSONPlaceHolder")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("9dd1fb73-ed6e-475c-989a-ecad723dba13")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Startup.cs b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Startup.cs new file mode 100644 index 0000000..21fab70 --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/Startup.cs @@ -0,0 +1,62 @@ +namespace ConsumingServicesJSONPlaceHolder +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Net.Http.Headers; + + using Newtonsoft.Json; + + class Startup + { + private const string DefaultUri = "http://jsonplaceholder.typicode.com/posts"; + + static void Main() + { + var client = new HttpClient(); + client.BaseAddress = new Uri(DefaultUri); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + Console.WriteLine("Enter search phrase or enter for default (lor): "); + var phrase = Console.ReadLine(); + if (string.IsNullOrEmpty(phrase)) + { + phrase = "lor"; + } + + Console.WriteLine("Enter number of posts or enter for default(10): "); + int numberOfPosts; + if (!int.TryParse(Console.ReadLine(), out numberOfPosts)) + { + numberOfPosts = 10; + } + + var posts = GetPosts(client, phrase, numberOfPosts); + + Console.WriteLine("{0} post(s) with title containing {1}: " + Environment.NewLine, numberOfPosts, phrase); + + foreach (var post in posts) + { + Console.WriteLine("Title: {0}", post.Title); + Console.WriteLine("Content: {0}", post.Body); + Console.WriteLine(); + } + } + + private static ICollection GetPosts(HttpClient client, string postTitle, int postsCount) + { + HttpResponseMessage response = client.GetAsync("").Result; + + var allPostsJson = response.Content.ReadAsStringAsync().Result; + var allPosts = JsonConvert.DeserializeObject>(allPostsJson); + + var filtered = allPosts + .Where(p => p.Title.Contains(postTitle)) + .Take(postsCount) + .ToList(); + + return filtered; + } + } +} diff --git a/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/packages.config b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/packages.config new file mode 100644 index 0000000..39df637 --- /dev/null +++ b/Web Services And Cloud/04.ConsumingWebServices/ConsumingServicesJSONPlaceHolder/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file