简体   繁体   中英

Call and get response from web api in a winform c# application

I am making a simple WinForm Application in Windows and I want to get some data about foreign exchange rates. So I decided to call an API from Oanda. I tried several things around but nothing worked. It gives the response in CSV as well as JSON format. I don't know which will be easier to handle.

Also for this type of response, I am unable to create its model class. Response:

JSON:

{
  "meta": {
    "effective_params": {
      "data_set": "OANDA",
      "base_currencies": [
        "EUR"
      ],
      "quote_currencies": [
        "USD"
      ]
    },
    "endpoint": "spot",
    "request_time": "2019-06-08T12:05:23+00:00",
    "skipped_currency_pairs": []
  },
  "quotes": [
    {
      "base_currency": "EUR",
      "quote_currency": "USD",
      "bid": "1.13287",
      "ask": "1.13384",
      "midpoint": "1.13336"
    }
  ]
}

CSV:

base_currency,quote_currency,bid,ask,midpoint
EUR,USD,1.13287,1.13384,1.13336

I just need those three numbers so, which method will be helpful and how.

This code I already tried:

var client = new HttpClient();
client.BaseAddress = new Uri("https://www1.oanda.com/rates/api/v2/rates/");
HttpResponseMessage response = await client.GetAsync("spot.csv?api_key=<myapikey>&base=EUR&quote=USD");
string result = await response.Content.ReadAsStringAsync();
textBox1.Text = result;

Edit: I need the result of this call for my further processing so I must need this method to complete its execution before proceeding further

First creating model from Json:

  1. use a online model generator like Json2C# , for the Json that you have posted, following is the model generated:

     public class EffectiveParams { public string data_set { get; set; } public List<string> base_currencies { get; set; } public List<string> quote_currencies { get; set; } } public class Meta { public EffectiveParams effective_params { get; set; } public string endpoint { get; set; } public DateTime request_time { get; set; } public List<object> skipped_currency_pairs { get; set; } } public class Quote { public string base_currency { get; set; } public string quote_currency { get; set; } public string bid { get; set; } public string ask { get; set; } public string midpoint { get; set; } } public class RootObject { public Meta meta { get; set; } public List<Quote> quotes { get; set; } } 
  2. Now connecting to the WebAPI using HttpClient , which has the option to return both Json and CSV , I would prefer JSON being standard, which can also be consumed easily by variety of clients, use the following simple generic methods:

Assuming it is GET only call, just supply the Host and API details to the generic Process method underneath:

public async Task<TResponse> Process<TResponse>(string host,string api)
{
   // Execute Api call Async
   var httpResponseMessage = await MakeApiCall(host,api);

   // Process Json string result to fetch final deserialized model
   return await FetchResult<TResponse>(httpResponseMessage);
} 

public async Task<HttpResponseMessage> MakeApiCall(string host,string api)

{   
    // Create HttpClient
    var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }) { BaseAddress = new Uri(host) };
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // Make an API call and receive HttpResponseMessage
    HttpResponseMessage responseMessage = await client.GetAsync(api, HttpCompletionOption.ResponseContentRead);

    return responseMessage;
}

public async Task<T> FetchResult<T>(HttpResponseMessage result)
{
    if (result.IsSuccessStatusCode)
    {
        // Convert the HttpResponseMessage to string
        var resultArray = await result.Content.ReadAsStringAsync();

        // Json.Net Deserialization
        var final = JsonConvert.DeserializeObject<T>(resultArray);

        return final;
    }
    return default(T);
}

How to use:

  • Simply call:

     var rootObj = await Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/"); 
  • You receive the deserialized RootObject as shown in the model above

  • For anything further complex processing like sending input to the call with http body, above generic code needs further modification, it is currently only specific to your requirement

Edit 1: (Making the entry call Synchronous)

  • To make the overall call synchronous, use the GetAwaiter().GetResult() at the topmost level, Main method will be converted to, rest all will remain same as in the sample (async methods)

      void Main() { var rootObj = Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/").GetAwaiter().GetResult(); } 

Edit 2: (Making complete code Synchronous)

void Main()
{
    var rootObj = Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/");
}


public TResponse Process<TResponse>(string host, string api)
{
    // Execute Api call
    var httpResponseMessage = MakeApiCall(host, api);

    // Process Json string result to fetch final deserialized model
    return FetchResult<TResponse>(httpResponseMessage);
}

public HttpResponseMessage MakeApiCall(string host, string api)

{
    // Create HttpClient
    var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }) { BaseAddress = new Uri(host) };
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // Make an API call and receive HttpResponseMessage
    HttpResponseMessage responseMessage = client.GetAsync(api, HttpCompletionOption.ResponseContentRead).GetAwaiter().GetResult();

    return responseMessage;
}

public T FetchResult<T>(HttpResponseMessage result)
{
    if (result.IsSuccessStatusCode)
    {
        // Convert the HttpResponseMessage to string
        var resultArray = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

        // Json.Net Deserialization
        var final = JsonConvert.DeserializeObject<T>(resultArray);

        return final;
    }
    return default(T);
}

You can use an online service such as json2csharp to get your json model and Json.Net to serialise and deserialise the json string. The following models your json.

public class EffectiveParams
{
    public string data_set { get; set; }
    public List<string> base_currencies { get; set; }
    public List<string> quote_currencies { get; set; }
}

public class Meta
{
    public EffectiveParams effective_params { get; set; }
    public string endpoint { get; set; }
    public DateTime request_time { get; set; }
    public List<object> skipped_currency_pairs { get; set; }
}

public class Quote
{
    public string base_currency { get; set; }
    public string quote_currency { get; set; }
    public string bid { get; set; }
    public string ask { get; set; }
    public string midpoint { get; set; }
}

public class RootObject
{
    public Meta meta { get; set; }
    public List<Quote> quotes { get; set; }
}

Note that you can change RootOject to a more descriptive name.

So, for example, to get the bid, ask and midpoint value for each quotes , you can simply do this:

RootObject rootObj=JsonConvert.DeserializeObject(jsonString);

//Get the required values.
foreach(var quote in rootObj.quotes)
{
Console.WriteLine($"Bid : {quote.bid} Ask: {quote.ask} MidPoint: {quote.midpoint}");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM