Kate Lastimosa
Kate Lastimosa

Reputation: 189

Should I use synchronous/asynchronous API calls when creating an API that calls 2 other APIs?

I’m creating an API that serves as the bridge between the app and 2 other APIs. I want to know if what is the best way to do this. I’m using HttpClient. The app has almost a thousand users so if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests? Is there a better way of doing an API like this?

Here is a sample of my code using synchronous:

[HttpGet]
[Route("api/apiname")]     
public String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){          

    var request = JsonConvert.SerializeObject(getNPRequest);
    string errorMessage = "";           

    try{              
         httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());

         var response = httpClient.GetAsync("api/MobileApp/GetNumberP?"
                          + "strCardNumber=" + getNPRequest.strCardNumber
                         + "&strDateOfBirth=" + getNPRequest.strDateOfBirth).Result;
         return response;
    }            
    catch (Exception e){                
        throw utils.ReturnException("GetNumberofP", e, errorMessage);           
    }      
}

Upvotes: 2

Views: 4769

Answers (3)

Enigmativity
Enigmativity

Reputation: 117174

t's always easy to turn a synchronous call into an asynchronous one, but the other way around is fraught with danger. You should make your API asynchronous.

[HttpGet]
[Route("api/apiname")]
public Task<string> GetNumberofP([FromUri]GetNumberofPRequest getNPRequest)
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.gettoken());
    return httpClient.GetAsync($"api/MobileApp/GetNumberP?strCardNumber={getNPRequest.strCardNumber}&strDateOfBirth={getNPRequest.strDateOfBirth}");
}

You should also consider creating a new httpClient for each call.

Upvotes: 1

ProgrammerAl
ProgrammerAl

Reputation: 787

if I use synchronous calls does that mean that if a user calls the API, then the other users have to wait until the 1st user gets the response before it proceeds to the other API requests

No. When a request comes into the pipeline, a new thread is spawned by the framework. So if 1,000 requests come in at the same time, the 1,000th user will not have to wait for the other 999 requests to finish.

You are better off using async code for this anyway. For any I/O like network requests, you're usually better off for performance letting a background thread do the waiting. Side note, you never want to call .Result because that forces the async code to become blocking and effectively becomes synchronous.

Upvotes: 3

tmaj
tmaj

Reputation: 35135

It seems you're missing the async and await keywords.

public async String GetNumberofP([FromUri]GetNumberofPRequest getNPRequest){
            
(...)

         var response = await httpClient.GetAsync();

Upvotes: -2

Related Questions