|
| 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 | +} |
0 commit comments