Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
117 lines (95 sloc) 4.27 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace CSharpExamples
{
class WBIndCoun
{
/// <summary>
/// Store the client key to be used through the program
/// </summary>
static string _clientKey = "guest:guest";
static void Main(string[] args)
{
// set the client key
Console.WriteLine("Provide a API key; otherwise, press ENTER to use the default test key...");
string k = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(k))
_clientKey = k;
// get world bank information by indicators series code
Console.WriteLine("**********Get world bank by indicators series code**********");
var getWBSeries = GetWBSeries(new string [] { "fr.inr.rinr"}).Result;
Console.WriteLine(getWBSeries);
// Filtering by url
Console.WriteLine("**********Get WB by url**********");
var getWBUrl = GetWBUrl("united-states/real-interest-rate-percent-wb-data.html").Result;
Console.WriteLine(getWBUrl);
// Filtering by the country and page number
Console.WriteLine("**********Get WB by country and page number**********");
var getWBCountryPage = GetWBCountryPage("united states", 4).Result;
Console.WriteLine(getWBCountryPage);
Console.ReadLine();
}
/// <summary>
/// get main categories
/// </summary>
/// <param name="series_code">series code</param>
/// <returns>A task that will be resolved in a string with the request result</returns>
public async static Task<string> GetWBSeries(string[] series_code)
{
return await HttpRequester($"/worldBank/indicator?s={string.Join(",", series_code)}");
}
/// <summary>
/// Filter by url
/// </summary>
/// <param name="url1">url</param>
/// <returns>A task that will be resolved in a string with the request result</returns>
public async static Task<string> GetWBUrl(string url1)
{
return await HttpRequester($"/worldBank/indicator?url={url1}");
}
/// <summary>
/// Get WB by country and page number
/// </summary>
/// <param name="country">country</param>
/// <param name="page_number">pagination</param>
/// <returns>A task that will be resolved in a string with the request result</returns>
public async static Task<string> GetWBCountryPage(string country, int page_number)
{
return await HttpRequester($"/worldBank/country/{country}/{page_number}");
}
/// <summary>
/// Method to make HTTP calls to TradingEconomics API
/// </summary>
/// <param name="url">The URL to fetch</param>
/// <param name="baseURL">The base path, the default is 'https://api.tradingeconomics.com/'</param>
/// <returns>A task tha will be resolved in a string with the content of the response</returns>
public async static Task<string> HttpRequester(string url, string baseURL = "https://api.tradingeconomics.com/")
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseURL);
client.DefaultRequestHeaders.Clear();
//ADD Acept Header to tell the server what data type you want
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
//ADD Authorization
AuthenticationHeaderValue auth = new AuthenticationHeaderValue("Client", _clientKey);
client.DefaultRequestHeaders.Authorization = auth;
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
catch (Exception e)
{
return $"Error at HttpRequester with msg: {e}";
}
}
}
}
You can’t perform that action at this time.