1

Is it practical to create multiple function with same RestSharp parameters but different return statements?

For example:

I'll get the IP Address

    public string getIP()
        {
            RestClient client = new RestClient("http://ip-api.com/json/?fields=9009");
            var request = new RestRequest(Method.GET);
            IRestResponse response = client.Execute(request);
            string source = (response.Content);
            dynamic data = JObject.Parse(source);
            string IPAddress = data.query;
            return IPAddress;
            
        }

Is it good to use another like getCity() then using

string City = data.city;
return City;

and so on?

aiof
  • 11
  • 5
  • Is there a purpose to performing the same call over and over to only retrieve one field at a time? Or is this done because you don't quite know how to call the API once and then use the returned data without needing to do the call again? – Flater Jan 14 '22 at 11:05
  • It's because I wanted to use a function on another cs file and return statement can only return one out of 5 variable response and it's tuple that i needed. – aiof Jan 14 '22 at 21:20

2 Answers2

2

If each API URL is the same, it would be better to call the API only once and parse the data multiple times, once for each field.

If the URL changes (for instance, http://ip-api.com/json/?fields=9009 for IP, http://city-api.com/json/?fields=9009 for city, etc.), then you would write multiple functions.

You should be able to return multiple values using output parameters: https://www.c-sharpcorner.com/UploadFile/9b86d4/how-to-return-multiple-values-from-a-function-in-C-Sharp/

Deep in the Code
  • 572
  • 1
  • 6
  • 20
  • 1
    The url are still the same, what do you mean by parse the data multiple times? you mean using regex? Also for using multiple functions, does it affect the performance somehow? – aiof Jan 13 '22 at 20:33
  • No, parse might have been the wrong word there. The value of data.query has the IP Address. If the city is in data.city, you would just set the City variable in the next line, and do that for each field. Generally, you don't want to call the same API with the same parameters multiple times because that creates needless traffic. Call once and use the single response to get each field instead. – Deep in the Code Jan 13 '22 at 20:37
  • 1
    yeah I've tried that one but I've been using return statement to get the variable from this cs file to form1. Multiple return statement aren't possible. The only thing I could think of is get all of them in one variable using concatenation then using regex to separate them again. I'm not sure if it's the best method tho. https://stackoverflow.com/questions/70701766/getting-restsharp-response-from-another-cs-file/70702063#comment124988968_70701766 – aiof Jan 13 '22 at 20:41
  • You should be able to return multiple values using output parameters: https://www.c-sharpcorner.com/UploadFile/9b86d4/how-to-return-multiple-values-from-a-function-in-C-Sharp/. – Deep in the Code Jan 13 '22 at 20:48
  • 1
    Thank you! It was tuple that I needed. I never heard that one and it's completely foreign to me. – aiof Jan 13 '22 at 23:12
  • Glad to help! Please mark my answer as accepted. – Deep in the Code Jan 13 '22 at 23:25
0

The main advantage of using RestSharp is its built-in serialization capabilities and easy parameter handling. Therefore, the best way in this particular case is to create a small DTO to represent the response.

public record IpApiResponse(
    string Country, string City, string Zip,
    string Timezone, string Isp, string Query
);

Then, you can just call

var response = await client.GetJsonAsync<IpApiResponse>();

The sample shows RestSharp v107 API. Remember not to instantiate a new RestClient instance for each call.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83