├── .gitignore
├── README.md
├── RESTfulAPIConsume.sln
└── RESTfulAPIConsume
├── App.config
├── Constants
└── RequestConstants.cs
├── Model
└── GitHubRelease.cs
├── Program.cs
├── Properties
└── AssemblyInfo.cs
├── RESTfulAPIConsume.csproj
├── RequestHandlers
├── DalSoftRequestHandler.cs
├── FlurlRequestHandler.cs
├── HttpClientRequestHandler.cs
├── HttpWebRequestHandler.cs
├── IRequestHandler.cs
├── RestSharpRequestHandler.cs
├── ServiceStackRequestHandler.cs
└── WebClientRequestHandler.cs
└── packages.config
/.gitignore:
--------------------------------------------------------------------------------
1 | /*.suo
2 | /RESTfulAPIConsume/bin
3 | /RESTfulAPIConsume/obj
4 | /packages
5 | /.vs/RESTfulAPIConsume/v15/*.suo
6 | /.vs
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Consume RESTful APIs Examples
2 |
3 | Examples included in the blog post: https://code-maze.com/different-ways-consume-restful-api-csharp
4 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27703.2026
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RESTfulAPIConsume", "RESTfulAPIConsume\RESTfulAPIConsume.csproj", "{08765779-E3B5-4611-84B7-E2DEF366EB68}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {08765779-E3B5-4611-84B7-E2DEF366EB68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {08765779-E3B5-4611-84B7-E2DEF366EB68}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {08765779-E3B5-4611-84B7-E2DEF366EB68}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {08765779-E3B5-4611-84B7-E2DEF366EB68}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {93986419-4CFA-4409-A458-FB382168F0FE}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/Constants/RequestConstants.cs:
--------------------------------------------------------------------------------
1 | namespace RESTfulAPIConsume.Constants
2 | {
3 | public static class RequestConstants
4 | {
5 | public const string BaseUrl = "https://api.github.com";
6 | public const string Url = "https://api.github.com/repos/restsharp/restsharp/releases";
7 | public const string UserAgent = "User-Agent";
8 | public const string UserAgentValue = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/Model/GitHubRelease.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 |
3 | namespace RESTfulAPIConsume.Model
4 | {
5 | public class GitHubRelease
6 | {
7 | [JsonProperty(PropertyName = "name")]
8 | public string Name { get; set; }
9 | [JsonProperty(PropertyName = "published_at")]
10 | public string PublishedAt { get; set; }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/Program.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using RESTfulAPIConsume.Constants;
3 | using RESTfulAPIConsume.Model;
4 | using RESTfulAPIConsume.RequestHandlers;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Net;
8 |
9 | namespace RESTfulAPIConsume
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | Console.WriteLine("Fetching the list of RestSharp releases and their publish dates.");
16 | Console.WriteLine();
17 |
18 | //These are the ways to consume RESTful APIs as described in the blog post
19 | IRequestHandler httpWebRequestHandler = new HttpWebRequestHandler();
20 | IRequestHandler webClientRequestHandler = new WebClientRequestHandler();
21 | IRequestHandler httpClientRequestHandler = new HttpClientRequestHandler();
22 | IRequestHandler restSharpRequestHandler = new RestSharpRequestHandler();
23 | IRequestHandler serviceStackRequestHandler = new ServiceStackRequestHandler();
24 | IRequestHandler flurlRequestHandler = new FlurlRequestHandler();
25 | IRequestHandler dalSoftRequestHandler = new DalSoftRequestHandler();
26 |
27 | //to support github's depreciation of older cryptographic standards (might be useful for some other APIs too)
28 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
29 |
30 | //By default HttpWebRequest is used to get the RestSharp releases
31 | //Replace the httpWebRequestHandler variable with one of the above to test out different libraries
32 | //Results should be the same
33 | var response = GetReleases(httpWebRequestHandler);
34 |
35 | var githubReleases = JsonConvert.DeserializeObject>(response);
36 |
37 | foreach (var release in githubReleases)
38 | {
39 | Console.WriteLine("Release: {0}", release.Name);
40 | Console.WriteLine("Published: {0}", DateTime.Parse(release.PublishedAt));
41 | Console.WriteLine();
42 | }
43 |
44 | Console.ReadLine();
45 | }
46 |
47 | public static string GetReleases(IRequestHandler requestHandler)
48 | {
49 | return requestHandler.GetReleases(RequestConstants.Url);
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("RESTfulAPIConsume")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("RESTfulAPIConsume")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ef414955-25e1-4b91-8f3e-770f35a45862")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RESTfulAPIConsume.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {08765779-E3B5-4611-84B7-E2DEF366EB68}
8 | Exe
9 | Properties
10 | RESTfulAPIConsume
11 | RESTfulAPIConsume
12 | v4.6.2
13 | 512
14 |
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 | ..\packages\DalSoft.RestClient.3.3.2\lib\netstandard2.0\DalSoft.RestClient.dll
38 |
39 |
40 | ..\packages\Flurl.2.7.1\lib\net40\Flurl.dll
41 |
42 |
43 | ..\packages\Flurl.Http.2.3.2\lib\net45\Flurl.Http.dll
44 |
45 |
46 |
47 | ..\packages\Microsoft.Extensions.Configuration.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll
48 |
49 |
50 | ..\packages\Microsoft.Extensions.Configuration.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll
51 |
52 |
53 | ..\packages\Microsoft.Extensions.Configuration.Binder.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Binder.dll
54 |
55 |
56 | ..\packages\Microsoft.Extensions.DependencyInjection.2.1.1\lib\net461\Microsoft.Extensions.DependencyInjection.dll
57 |
58 |
59 | ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll
60 |
61 |
62 | ..\packages\Microsoft.Extensions.Http.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Http.dll
63 |
64 |
65 | ..\packages\Microsoft.Extensions.Logging.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.dll
66 |
67 |
68 | ..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll
69 |
70 |
71 | ..\packages\Microsoft.Extensions.Options.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Options.dll
72 |
73 |
74 | ..\packages\Microsoft.Extensions.Primitives.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll
75 |
76 |
77 | ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll
78 |
79 |
80 | ..\packages\RestSharp.105.2.3\lib\net45\RestSharp.dll
81 | True
82 |
83 |
84 | ..\packages\ServiceStack.4.5.8\lib\net45\ServiceStack.dll
85 | True
86 |
87 |
88 | ..\packages\ServiceStack.Client.4.5.8\lib\net45\ServiceStack.Client.dll
89 | True
90 |
91 |
92 | ..\packages\ServiceStack.Common.4.5.8\lib\net45\ServiceStack.Common.dll
93 | True
94 |
95 |
96 | ..\packages\ServiceStack.Interfaces.4.5.8\lib\portable-wp80+sl5+net45+win8+wpa81+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll
97 | True
98 |
99 |
100 | ..\packages\ServiceStack.Text.4.5.8\lib\net45\ServiceStack.Text.dll
101 | True
102 |
103 |
104 |
105 | ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll
106 |
107 |
108 |
109 |
110 | ..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll
111 |
112 |
113 | ..\packages\System.Net.Http.4.3.3\lib\net46\System.Net.Http.dll
114 | True
115 | True
116 |
117 |
118 |
119 | ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll
120 |
121 |
122 | ..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll
123 | True
124 | True
125 |
126 |
127 | ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
128 |
129 |
130 | ..\packages\System.Runtime.InteropServices.4.3.0\lib\net462\System.Runtime.InteropServices.dll
131 | True
132 | True
133 |
134 |
135 | ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll
136 | True
137 | True
138 |
139 |
140 | ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll
141 | True
142 | True
143 |
144 |
145 | ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll
146 | True
147 | True
148 |
149 |
150 | ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll
151 | True
152 | True
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
187 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/DalSoftRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using DalSoft.RestClient;
2 | using RESTfulAPIConsume.Constants;
3 | using RESTfulAPIConsume.Model;
4 | using System.Collections.Generic;
5 | using System.Threading.Tasks;
6 |
7 | namespace RESTfulAPIConsume.RequestHandlers
8 | {
9 | class DalSoftRequestHandler : IRequestHandler
10 | {
11 | public string GetReleases(string url)
12 | {
13 | dynamic client = new RestClient(RequestConstants.BaseUrl,
14 | new Headers { { RequestConstants.UserAgent, RequestConstants.UserAgentValue } });
15 |
16 | var response = client.repos.restsharp.restsharp.releases.Get().Result.ToString();
17 |
18 | return response;
19 | }
20 |
21 | //Here's a great way to deserialize the response "organically"
22 | public async Task> GetDeserializedReleases(string url)
23 | {
24 | dynamic client = new RestClient(RequestConstants.BaseUrl,
25 | new Headers { { RequestConstants.UserAgent, RequestConstants.UserAgentValue } });
26 |
27 | var response = await client.repos.restsharp.restsharp.releases.Get();
28 |
29 | return response;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/FlurlRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using Flurl.Http;
2 | using Newtonsoft.Json;
3 | using RESTfulAPIConsume.Constants;
4 | using RESTfulAPIConsume.Model;
5 | using System.Collections.Generic;
6 | using System.Threading.Tasks;
7 |
8 | namespace RESTfulAPIConsume.RequestHandlers
9 | {
10 | public class FlurlRequestHandler: IRequestHandler
11 | {
12 | // A really bad way to do things, but for demonstration purposes it will do
13 | // Flurl has it's own deserializer so it's better to do it like demonstrated in GetDeserializedReleases method
14 | public string GetReleases(string url)
15 | {
16 | var result = url
17 | .WithHeader(RequestConstants.UserAgent, RequestConstants.UserAgentValue)
18 | .GetJsonAsync>()
19 | .Result;
20 |
21 | return JsonConvert.SerializeObject(result);
22 | }
23 |
24 | public List GetDeserializedReleases(string url)
25 | {
26 | var result = url
27 | .WithHeader(RequestConstants.UserAgent, RequestConstants.UserAgentValue)
28 | .GetJsonAsync>()
29 | .Result;
30 |
31 | return result;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/HttpClientRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using RESTfulAPIConsume.Constants;
2 | using System;
3 | using System.Net.Http;
4 |
5 | namespace RESTfulAPIConsume.RequestHandlers
6 | {
7 | public class HttpClientRequestHandler: IRequestHandler
8 | {
9 | public string GetReleases(string url)
10 | {
11 | using (var httpClient = new HttpClient())
12 | {
13 | httpClient.DefaultRequestHeaders.Add(RequestConstants.UserAgent, RequestConstants.UserAgentValue);
14 |
15 | var response = httpClient.GetStringAsync(new Uri(url)).Result;
16 |
17 | return response;
18 | }
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/HttpWebRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using RESTfulAPIConsume.Constants;
2 | using System.IO;
3 | using System.Net;
4 |
5 | namespace RESTfulAPIConsume.RequestHandlers
6 | {
7 | public class HttpWebRequestHandler : IRequestHandler
8 | {
9 | public string GetReleases(string url)
10 | {
11 | var request = (HttpWebRequest)WebRequest.Create(url);
12 |
13 | request.Method = "GET";
14 | request.UserAgent = RequestConstants.UserAgentValue;
15 | request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
16 |
17 | var content = string.Empty;
18 |
19 | using (var response = (HttpWebResponse)request.GetResponse())
20 | {
21 | using (var stream = response.GetResponseStream())
22 | {
23 | using (var sr = new StreamReader(stream))
24 | {
25 | content = sr.ReadToEnd();
26 | }
27 | }
28 | }
29 |
30 | return content;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/IRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json.Linq;
2 |
3 | namespace RESTfulAPIConsume.RequestHandlers
4 | {
5 | public interface IRequestHandler
6 | {
7 | //Method to get the releases of the repo provided by the url
8 | string GetReleases(string url);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/RestSharpRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using RESTfulAPIConsume.Model;
2 | using RestSharp;
3 | using System.Collections.Generic;
4 |
5 | namespace RESTfulAPIConsume.RequestHandlers
6 | {
7 | public class RestSharpRequestHandler : IRequestHandler
8 | {
9 | public string GetReleases(string url)
10 | {
11 | var client = new RestClient(url);
12 |
13 | var response = client.Execute(new RestRequest());
14 |
15 | return response.Content;
16 | }
17 |
18 | //Alternative way, using RestSharp's parser
19 | public List GetDeserializedReleases(string url)
20 | {
21 | var client = new RestClient(url);
22 |
23 | var response = client.Execute>(new RestRequest());
24 |
25 | return response.Data;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/ServiceStackRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using RESTfulAPIConsume.Constants;
2 | using RESTfulAPIConsume.Model;
3 | using ServiceStack;
4 | using System.Collections.Generic;
5 |
6 | namespace RESTfulAPIConsume.RequestHandlers
7 | {
8 | public class ServiceStackRequestHandler: IRequestHandler
9 | {
10 | public string GetReleases(string url)
11 | {
12 | var response = url.GetJsonFromUrl(webReq =>
13 | {
14 | webReq.UserAgent = RequestConstants.UserAgentValue;
15 | });
16 |
17 | return response;
18 | }
19 |
20 | //Alternative way, using ServiceStack's parser
21 | public List GetDeserializedReleases(string url)
22 | {
23 | var releases = url.GetJsonFromUrl(webReq =>
24 | {
25 | webReq.UserAgent = RequestConstants.UserAgentValue;
26 | }).FromJson>();
27 |
28 | return releases;
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/RequestHandlers/WebClientRequestHandler.cs:
--------------------------------------------------------------------------------
1 | using RESTfulAPIConsume.Constants;
2 | using System.Net;
3 |
4 | namespace RESTfulAPIConsume.RequestHandlers
5 | {
6 | public class WebClientRequestHandler: IRequestHandler
7 | {
8 | public string GetReleases(string url)
9 | {
10 | var client = new WebClient();
11 | client.Headers.Add(RequestConstants.UserAgent, RequestConstants.UserAgentValue);
12 |
13 | var response = client.DownloadString(url);
14 |
15 | return response;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/RESTfulAPIConsume/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------