Get data from external API with a asp.net core API










4















I'm learning to create APIs using ASP.NET core, in this I came up with a problem, I'm trying to execute a request for an external API using my API, but I do not know how to execute the request and return the JSON of the request, any help?



The flow of the application looks something like this:



SPA -> AspNet Core WEB API -> External API



What I've done so far:



[Route("api/[Controller]")]
public class RankingsController : Controller

private readonly IRankingRepository _rankingRepository;

public RankingsController(IRankingRepository rankingRepo)

_rankingRepository = rankingRepo;


[HttpGet("id", Name = "GetRanking")]
public IActionResult GetById(long id)
//Here is where I want to make the requisition


}


I need to make the request for this API:



http://api.football-data.org/v1/competitions/id/leagueTable



In the ID location, I need to pass a parameter that comes from the request made in my API;



Any help for this problem?



Sorry for not being such a complex question.



Thanks!!










share|improve this question




























    4















    I'm learning to create APIs using ASP.NET core, in this I came up with a problem, I'm trying to execute a request for an external API using my API, but I do not know how to execute the request and return the JSON of the request, any help?



    The flow of the application looks something like this:



    SPA -> AspNet Core WEB API -> External API



    What I've done so far:



    [Route("api/[Controller]")]
    public class RankingsController : Controller

    private readonly IRankingRepository _rankingRepository;

    public RankingsController(IRankingRepository rankingRepo)

    _rankingRepository = rankingRepo;


    [HttpGet("id", Name = "GetRanking")]
    public IActionResult GetById(long id)
    //Here is where I want to make the requisition


    }


    I need to make the request for this API:



    http://api.football-data.org/v1/competitions/id/leagueTable



    In the ID location, I need to pass a parameter that comes from the request made in my API;



    Any help for this problem?



    Sorry for not being such a complex question.



    Thanks!!










    share|improve this question


























      4












      4








      4








      I'm learning to create APIs using ASP.NET core, in this I came up with a problem, I'm trying to execute a request for an external API using my API, but I do not know how to execute the request and return the JSON of the request, any help?



      The flow of the application looks something like this:



      SPA -> AspNet Core WEB API -> External API



      What I've done so far:



      [Route("api/[Controller]")]
      public class RankingsController : Controller

      private readonly IRankingRepository _rankingRepository;

      public RankingsController(IRankingRepository rankingRepo)

      _rankingRepository = rankingRepo;


      [HttpGet("id", Name = "GetRanking")]
      public IActionResult GetById(long id)
      //Here is where I want to make the requisition


      }


      I need to make the request for this API:



      http://api.football-data.org/v1/competitions/id/leagueTable



      In the ID location, I need to pass a parameter that comes from the request made in my API;



      Any help for this problem?



      Sorry for not being such a complex question.



      Thanks!!










      share|improve this question
















      I'm learning to create APIs using ASP.NET core, in this I came up with a problem, I'm trying to execute a request for an external API using my API, but I do not know how to execute the request and return the JSON of the request, any help?



      The flow of the application looks something like this:



      SPA -> AspNet Core WEB API -> External API



      What I've done so far:



      [Route("api/[Controller]")]
      public class RankingsController : Controller

      private readonly IRankingRepository _rankingRepository;

      public RankingsController(IRankingRepository rankingRepo)

      _rankingRepository = rankingRepo;


      [HttpGet("id", Name = "GetRanking")]
      public IActionResult GetById(long id)
      //Here is where I want to make the requisition


      }


      I need to make the request for this API:



      http://api.football-data.org/v1/competitions/id/leagueTable



      In the ID location, I need to pass a parameter that comes from the request made in my API;



      Any help for this problem?



      Sorry for not being such a complex question.



      Thanks!!







      asp.net api asp.net-web-api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 19:30







      Jefferson Bruchado

















      asked Nov 15 '18 at 18:50









      Jefferson BruchadoJefferson Bruchado

      389314




      389314






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You could use an HttpClient instance to achieve what you want. However, I always find easier to use RestSharp though.



          That, of course will depend on your constraints but assuming you have none for this case, you could use RestSharp to make the call to the external API:



          Install it:



          Install-Package RestSharp



          Usage:



          using RestSharp;

          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)

          var client = new RestClient("http://api.football-
          data.org/v1/competitions/445/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);

          //TODO: transform the response here to suit your needs

          return Ok(data);



          To consume the rest response from RestSharp you must use the response.Content property.



          You could, for example, deserialize it to Json, manipulate it to fit your needs and return the required data to your API caller.



          Example:



          Let's say I'd like to get the rankings for the Premier League 2017/18 (Id = 445):



          I'll get a lot of help below from the legendary Newtonsoft.Json package and a little of jpath syntax but I'll assume you've already used both :)



          Create a couple models to hold the values to be returned to the API caller:



          public class LeagueTableModel

          public string LeagueCaption get; set;

          public IEnumerable<StandingModel> Standings get; set;

          public class StandingModel

          public string TeamName get; set;

          public int Position get; set;



          Implement the below method in the service class, injected to your controller through DI/IoC to avoid coupling and increase testability (as we all know we should do right?). I'm assuming this class is RankingRepository in your sample:



          public RankingRepository: IRankingRepository 

          public LeagueTableModel GetRankings(long id)

          var client = new RestClient($"http://api.football-data.org/v1/competitions/id/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);
          if (response.IsSuccessful)

          var content = JsonConvert.DeserializeObject<JToken>(response.Content);

          //Get the league caption
          var leagueCaption = content["leagueCaption"].Value<string>();

          //Get the standings for the league.
          var rankings = content.SelectTokens("standing[*]")
          .Select(team => new StandingModel

          TeamName = (string)team["teamName"],
          Position = (int)team["position"]
          )
          .ToList();

          //return the model to my caller.
          return new LeagueTableModel

          LeagueCaption = leagueCaption,
          Standings = rankings
          ;


          //TODO: log error, throw exception or do other stuffs for failed requests here.
          Console.WriteLine(response.Content);

          return null;




          Use it from the controller:



          [Route("api/[Controller]")]
          public class RankingsController : Controller

          private readonly IRankingRepository _rankingRepository;

          public RankingsController(IRankingRepository rankingRepo)

          _rankingRepository = rankingRepo;


          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)
          //Here is where I want to make the requisition
          var model = _rankingRepository.GetRankings(id);

          //Validate if null
          if (model == null)
          return NotFound(); //or any other error code accordingly. Bad request is a strong candidate also.

          return Ok(model);

          }


          Hope this helps!






          share|improve this answer




















          • 1





            I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id) IRestResponse response = await client.ExecuteAsync(request);

            – Mohsin Mehmood
            Nov 15 '18 at 21:57







          • 1





            Thank you very much for sharing your knowledge, it worked perfectly!

            – Jefferson Bruchado
            Nov 15 '18 at 22:20









          share|improve this answer




















          • 1





            I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id)  











          3














          You could use an HttpClient instance to achieve what you want. However, I always find easier to use RestSharp though.



          That, of course will depend on your constraints but assuming you have none for this case, you could use RestSharp to make the call to the external API:



          Install it:



          Install-Package RestSharp



          Usage:



          using RestSharp;

          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)

          var client = new RestClient("http://api.football-
          data.org/v1/competitions/445/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);

          //TODO: transform the response here to suit your needs

          return Ok(data);



          To consume the rest response from RestSharp you must use the response.Content property.



          You could, for example, deserialize it to Json, manipulate it to fit your needs and return the required data to your API caller.



          Example:



          Let's say I'd like to get the rankings for the Premier League 2017/18 (Id = 445):



          I'll get a lot of help below from the legendary Newtonsoft.Json package and a little of jpath syntax but I'll assume you've already used both :)



          Create a couple models to hold the values to be returned to the API caller:



          public class LeagueTableModel

          public string LeagueCaption get; set;

          public IEnumerable<StandingModel> Standings get; set;

          public class StandingModel

          public string TeamName get; set;

          public int Position get; set;



          Implement the below method in the service class, injected to your controller through DI/IoC to avoid coupling and increase testability (as we all know we should do right?). I'm assuming this class is RankingRepository in your sample:



          public RankingRepository: IRankingRepository 

          public LeagueTableModel GetRankings(long id)

          var client = new RestClient($"http://api.football-data.org/v1/competitions/id/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);
          if (response.IsSuccessful)

          var content = JsonConvert.DeserializeObject<JToken>(response.Content);

          //Get the league caption
          var leagueCaption = content["leagueCaption"].Value<string>();

          //Get the standings for the league.
          var rankings = content.SelectTokens("standing[*]")
          .Select(team => new StandingModel

          TeamName = (string)team["teamName"],
          Position = (int)team["position"]
          )
          .ToList();

          //return the model to my caller.
          return new LeagueTableModel

          LeagueCaption = leagueCaption,
          Standings = rankings
          ;


          //TODO: log error, throw exception or do other stuffs for failed requests here.
          Console.WriteLine(response.Content);

          return null;




          Use it from the controller:



          [Route("api/[Controller]")]
          public class RankingsController : Controller

          private readonly IRankingRepository _rankingRepository;

          public RankingsController(IRankingRepository rankingRepo)

          _rankingRepository = rankingRepo;


          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)
          //Here is where I want to make the requisition
          var model = _rankingRepository.GetRankings(id);

          //Validate if null
          if (model == null)
          return NotFound(); //or any other error code accordingly. Bad request is a strong candidate also.

          return Ok(model);




          Hope this helps!






          share|improve this answer




















          • 1





            I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id)  









          3












          3








          3







          You could use an HttpClient instance to achieve what you want. However, I always find easier to use RestSharp though.



          That, of course will depend on your constraints but assuming you have none for this case, you could use RestSharp to make the call to the external API:



          Install it:



          Install-Package RestSharp



          Usage:



          using RestSharp;

          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)

          var client = new RestClient("http://api.football-
          data.org/v1/competitions/445/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);

          //TODO: transform the response here to suit your needs

          return Ok(data);



          To consume the rest response from RestSharp you must use the response.Content property.



          You could, for example, deserialize it to Json, manipulate it to fit your needs and return the required data to your API caller.



          Example:



          Let's say I'd like to get the rankings for the Premier League 2017/18 (Id = 445):



          I'll get a lot of help below from the legendary Newtonsoft.Json package and a little of jpath syntax but I'll assume you've already used both :)



          Create a couple models to hold the values to be returned to the API caller:



          public class LeagueTableModel

          public string LeagueCaption get; set;

          public IEnumerable<StandingModel> Standings get; set;

          public class StandingModel

          public string TeamName get; set;

          public int Position get; set;



          Implement the below method in the service class, injected to your controller through DI/IoC to avoid coupling and increase testability (as we all know we should do right?). I'm assuming this class is RankingRepository in your sample:



          public RankingRepository: IRankingRepository 

          public LeagueTableModel GetRankings(long id)

          var client = new RestClient($"http://api.football-data.org/v1/competitions/id/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);
          if (response.IsSuccessful)

          var content = JsonConvert.DeserializeObject<JToken>(response.Content);

          //Get the league caption
          var leagueCaption = content["leagueCaption"].Value<string>();

          //Get the standings for the league.
          var rankings = content.SelectTokens("standing[*]")
          .Select(team => new StandingModel

          TeamName = (string)team["teamName"],
          Position = (int)team["position"]
          )
          .ToList();

          //return the model to my caller.
          return new LeagueTableModel

          LeagueCaption = leagueCaption,
          Standings = rankings
          ;


          //TODO: log error, throw exception or do other stuffs for failed requests here.
          Console.WriteLine(response.Content);

          return null;




          Use it from the controller:



          [Route("api/[Controller]")]
          public class RankingsController : Controller

          private readonly IRankingRepository _rankingRepository;

          public RankingsController(IRankingRepository rankingRepo)

          _rankingRepository = rankingRepo;


          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)
          //Here is where I want to make the requisition
          var model = _rankingRepository.GetRankings(id);

          //Validate if null
          if (model == null)
          return NotFound(); //or any other error code accordingly. Bad request is a strong candidate also.

          return Ok(model);




          Hope this helps!






          share|improve this answer















          You could use an HttpClient instance to achieve what you want. However, I always find easier to use RestSharp though.



          That, of course will depend on your constraints but assuming you have none for this case, you could use RestSharp to make the call to the external API:



          Install it:



          Install-Package RestSharp



          Usage:



          using RestSharp;

          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)

          var client = new RestClient("http://api.football-
          data.org/v1/competitions/445/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);

          //TODO: transform the response here to suit your needs

          return Ok(data);



          To consume the rest response from RestSharp you must use the response.Content property.



          You could, for example, deserialize it to Json, manipulate it to fit your needs and return the required data to your API caller.



          Example:



          Let's say I'd like to get the rankings for the Premier League 2017/18 (Id = 445):



          I'll get a lot of help below from the legendary Newtonsoft.Json package and a little of jpath syntax but I'll assume you've already used both :)



          Create a couple models to hold the values to be returned to the API caller:



          public class LeagueTableModel

          public string LeagueCaption get; set;

          public IEnumerable<StandingModel> Standings get; set;

          public class StandingModel

          public string TeamName get; set;

          public int Position get; set;



          Implement the below method in the service class, injected to your controller through DI/IoC to avoid coupling and increase testability (as we all know we should do right?). I'm assuming this class is RankingRepository in your sample:



          public RankingRepository: IRankingRepository 

          public LeagueTableModel GetRankings(long id)

          var client = new RestClient($"http://api.football-data.org/v1/competitions/id/leagueTable");
          var request = new RestRequest(Method.GET);
          IRestResponse response = client.Execute(request);
          if (response.IsSuccessful)

          var content = JsonConvert.DeserializeObject<JToken>(response.Content);

          //Get the league caption
          var leagueCaption = content["leagueCaption"].Value<string>();

          //Get the standings for the league.
          var rankings = content.SelectTokens("standing[*]")
          .Select(team => new StandingModel

          TeamName = (string)team["teamName"],
          Position = (int)team["position"]
          )
          .ToList();

          //return the model to my caller.
          return new LeagueTableModel

          LeagueCaption = leagueCaption,
          Standings = rankings
          ;


          //TODO: log error, throw exception or do other stuffs for failed requests here.
          Console.WriteLine(response.Content);

          return null;




          Use it from the controller:



          [Route("api/[Controller]")]
          public class RankingsController : Controller

          private readonly IRankingRepository _rankingRepository;

          public RankingsController(IRankingRepository rankingRepo)

          _rankingRepository = rankingRepo;


          [HttpGet("id", Name = "GetRanking")]
          public IActionResult GetById(long id)
          //Here is where I want to make the requisition
          var model = _rankingRepository.GetRankings(id);

          //Validate if null
          if (model == null)
          return NotFound(); //or any other error code accordingly. Bad request is a strong candidate also.

          return Ok(model);

          }


          Hope this helps!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 21:10

























          answered Nov 15 '18 at 19:46









          Karel TamayoKarel Tamayo

          2,80721623




          2,80721623







          • 1





            I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id) { IRestResponse response = await client.ExecuteAsync(request);

            – Mohsin Mehmood
            Nov 15 '18 at 21:57







          • 1





            Thank you very much for sharing your knowledge, it worked perfectly!

            – Jefferson Bruchado
            Nov 15 '18 at 22:20












          • 1





            I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id) { IRestResponse response = await client.ExecuteAsync(request);

            – Mohsin Mehmood
            Nov 15 '18 at 21:57







          • 1





            Thank you very much for sharing your knowledge, it worked perfectly!

            – Jefferson Bruchado
            Nov 15 '18 at 22:20







          1




          1





          I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id) { IRestResponse response = await client.ExecuteAsync(request);

          – Mohsin Mehmood
          Nov 15 '18 at 21:57






          I suggest to use async programming model based on async and await keywords to improve responsiveness. Something like this: public async Task<IActionResult> GetById(long id) { IRestResponse response = await client.ExecuteAsync(request);

          – Mohsin Mehmood
          Nov 15 '18 at 21:57





          1




          1





          Thank you very much for sharing your knowledge, it worked perfectly!

          – Jefferson Bruchado
          Nov 15 '18 at 22:20





          Thank you very much for sharing your knowledge, it worked perfectly!

          – Jefferson Bruchado
          Nov 15 '18 at 22:20



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326123%2fget-data-from-external-api-with-a-asp-net-core-api%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto