8000 Added exact same code as a .net 4.7.1 prohect · SidwellDee/csharp-samples@324a6df · GitHub
[go: up one dir, main page]

Skip to content

Commit 324a6df

Browse files
committed
Added exact same code as a .net 4.7.1 prohect
1 parent a8097ff commit 324a6df

File tree

6 files changed

+246
-1
lines changed

6 files changed

+246
-1
lines changed

CSharpSamples.sln

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26730.15
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample1", "Sample1\Sample1.csproj", "{1CD44A87-008C-4BA5-B56F-D8F26E90D817}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleNetCore", "Sample1\SampleNetCore.csproj", "{1CD44A87-008C-4BA5-B56F-D8F26E90D817}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleNetFramework", "SampleNetFramework\SampleNetFramework.csproj", "{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{1CD44A87-008C-4BA5-B56F-D8F26E90D817}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{1CD44A87-008C-4BA5-B56F-D8F26E90D817}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{1CD44A87-008C-4BA5-B56F-D8F26E90D817}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

SampleNetFramework/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
5+
</startup>
6+
</configuration>

SampleNetFramework/Program.cs

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using IdentityModel.Client;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using System;
5+
using System.IO;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Threading.Tasks;
9+
using System.Linq;
10+
11+
//********************************************************************************************************************************
12+
//********** ICD API allows programmatic access to the International Classification of Diseases(ICD).
13+
//********** More Information on ICD API and getting access to it
14+
//**********
15+
//********** https://icd.who.int/icdapi
16+
//**********
17+
//**********
18+
//********************************************************************************************************************************
19+
namespace Sample1
20+
{
21+
class Program
22+
{
23+
//The _secureFile is a text file with two lines in it. The first line contains the client id and the second line client key
24+
static 9E88 string _secureFile = @"c:\users\can\securefile.txt";
25+
26+
static void Main(string[] args)
27+
{
28+
Sample1().GetAwaiter().GetResult();
29+
}
30+
31+
static async Task Sample1()
32+
{
33+
var lines = File.ReadLines(_secureFile).ToArray();
34+
if (lines.Count() != 2)
35+
{
36+
Console.WriteLine("the securefile should have two lines in it. The first line contains the client id and the second line client key");
37+
return;
38+
}
39+
40+
var clientId = lines[0];
41+
var clientSecret = lines[1];
42+
43+
var client = new HttpClient();
44+
var disco = await client.GetDiscoveryDocumentAsync("https://icdaccessmanagement.who.int");
45+
if (disco.IsError) throw new Exception(disco.Error);
46+
47+
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
48+
{
49+
Address = disco.TokenEndpoint,
50+
ClientId = clientId,
51+
ClientSecret = clientSecret,
52+
Scope = "icdapi_access",
53+
GrantType = "client_credentials",
54+
ClientCredentialStyle = ClientCredentialStyle.AuthorizationHeader
55+
});
56+
57+
if (tokenResponse.IsError)
58+
{
59+
Console.WriteLine(tokenResponse.Error);
60+
return;
61+
}
62+
63+
Console.WriteLine(tokenResponse.Json);
64+
Console.WriteLine("\n\n");
65+
66+
// call api
67+
client = new HttpClient();
68+
client.SetBearerToken(tokenResponse.AccessToken);
69+
70+
HttpRequestMessage request;
71+
72+
73+
Console.WriteLine();
74+
Console.WriteLine("****************************************************************");
75+
Console.WriteLine("Requesting the root foundation URI...");
76+
request = new HttpRequestMessage(HttpMethod.Get, "https://id.who.int/icd/entity");
77+
78+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
79+
request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en"));
80+
var response = await client.SendAsync(request);
81+
if (!response.IsSuccessStatusCode)
82+
{
83+
Console.WriteLine(response.StatusCode);
84+
}
85+
86+
var resultJson = response.Content.ReadAsStringAsync().Result;
87+
var prettyJson = JValue.Parse(resultJson).ToString(Formatting.Indented); //convert json to a more human readable fashion
88+
Console.WriteLine(prettyJson);
89+
90+
Console.ReadKey();//Wait until a key is pressed
91+
92+
Console.WriteLine("****************************************************************");
93+
Console.WriteLine("Enter a search term:");
94+
var term = Console.ReadLine();
95+
request = new HttpRequestMessage(HttpMethod.Get, "https://id.who.int/icd/release/11/beta/mms/search?q=" + term);
96+
97+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
98+
request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en"));
99+
response = await client.SendAsync(request);
100+
101+
if (!response.IsSuccessStatusCode)
102+
{
103+
Console.WriteLine(response.StatusCode);
104+
}
105+
106+
resultJson = response.Content.ReadAsStringAsync().Result; //Now resultJson has the resulting json string
107+
Console.WriteLine("****** Search result json *****");
108+
Console.WriteLine(resultJson);
109+
110+
prettyJson = JValue.Parse(resultJson).ToString(Formatting.Indented); //convert json to a more human readable fashion
111+
Console.WriteLine("****** And the pretty json output *****");
112+
Console.WriteLine(prettyJson);
113+
114+
//Now trying to parse and get titles from the search result
115+
116+
Console.WriteLine("****** ICD code and titles from the search *****");
117+
dynamic searchResult = JsonConvert.DeserializeObject(resultJson);
118+
119+
foreach (var de in searchResult.DestinationEntities)
120+
{
121+
Console.WriteLine(de.TheCode + " " + de.Title);
122+
}
123+
124+
Console.ReadKey(); //Wait until a key is pressed
125+
126+
}
127+
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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("SampleNetFramework")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SampleNetFramework")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
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("90b5ac75-556d-4d7e-a4c9-d33df5f1d3c2")]
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")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{90B5AC75-556D-4D7E-A4C9-D33DF5F1D3C2}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>SampleNetFramework</RootNamespace>
10+
<AssemblyName>SampleNetFramework</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=e7877f4675df049f, processorArchitecture=MSIL">
37+
<HintPath>..\packages\IdentityModel.4.0.0-arran\lib\net461\IdentityModel.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
40+
<HintPath>..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
41+
</Reference>
42+
<Reference Include="System" />
43+
<Reference Include="System.Core" />
44+
<Reference Include="System.Text.Encodings.Web, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
45+
<HintPath>..\packages\System.Text.Encodings.Web.4.5.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System.Xml.Linq" />
48+
<Reference Include="System.Data.DataSetExtensions" />
49+
<Reference Include="Microsoft.CSharp" />
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.Net.Http" />
52+
<Reference Include="System.Xml" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<Compile Include="Program.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<None Include="App.config" />
60+
<None Include="packages.config" />
61+
</ItemGroup>
62+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
63+
</Project>

SampleNetFramework/packages.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="IdentityModel" version="4.0.0-arran" targetFramework="net471" />
4+
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net471" />
5+
<package id="System.Text.Encodings.Web" version="4.5.0" targetFramework="net471" />
6+
</packages>

0 commit comments

Comments
 (0)
0