Home > Net >  Can we call API function from another API function?
Can we call API function from another API function?

Time:10-14

    [HttpPost]
    public async Task<IActionResult> GetData(string requestString)
    {
        DetailsReq req = new DetailsReq();

        // Some logic to extract parameters from request string

        IActionResult res = await GetDetails(req);
        return Ok(res);
    }

    [HttpPost]
    public async Task<IActionResult> GetDetails([FromBody] DetailsReq req)
    { 
         DetailsRes res = new DetailsRes();
         return Ok(res);
    }

Requirement is to add new function GetData() to Dot Net core API. There is a existing POST method GetDetails() which gives the similar response.

I have two options.

  1. Copy logic from GetDetails() and use in GetData().
  2. Call GetDetails() in GetData()

My question is can we call API method from another method? Is it good practice?

CodePudding user response:

Can we call API method from another method?

To answer this, yes you can call one Method/Function from another Method/Function within the same Controller Class. you could see following example:

Method: GetDataFromAnotherAPI

public async Task<ActionResult> GetDataFromAnotherAPI()
        {
            
                var response = new MultipleDBQueryExecutionModel();
                var handler = new HttpClientHandler();
                var data = "";
                var baseUrl = "https://localhost:44361/UserLog/";
                var methodName = "GetData";

                handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
                var client = new HttpClient(handler);
                client.DefaultRequestHeaders.Accept.Clear();

                var responseFromApi = await client.GetAsync(baseUrl   methodName);
                if (responseFromApi.IsSuccessStatusCode)
                {

                    data = await responseFromApi.Content.ReadAsStringAsync();


                }

                return Ok(data);
          
            


        }

Note: HttpClientHandler is not related to your issue. I have used that to handle https certificate issue.

Method: GetData: Which I am calling within the Same Controller Class

 public ActionResult GetData()
        {
            var data = _context.multipleDBQueryExecutionModels.ToList();
            return Ok(data);
        }

Controller Class:

public class UserLogController : Controller
    {
        private readonly ApplicationDbContext _context;

        public UserLogController(ApplicationDbContext context)
        {
            _context = context;
        }
    }

Note: Above two method belongs to UserLogController controller class. and I am calling GetData method within GetDataFromAnotherAPI method.

Output:

enter image description here

Is it good practice?

Now let's discuss your point "Copy logic from GetDetails() and use in GetData().". No this doesn't considered as good practice on Object Oriented Programming as it doesn't encourage to write redundant code.

Here SOLID principle comes to rescue. Which tell make your repository/business logic in a seperate class/Interface as per SOLID its called Interface Segregation Principle. You could have look on SOLID, your time would be worth using.

  • Related