Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a generic method executeAPI which will take my httpMethod, my request and basepath. But I have having some issue on how to set this up. But problem is the request from IRestRequest does not contain any method like request.get() ???

Here is what I have

private static IRestResponse executeAPI(string httpMethod, IRestRequest request, string basePath)
       {
           IRestResponse response = null;


           //if its GET it will call the GET, if its POST it will call the POST...and so on
           switch (httpMethod)
           {
               case "GET":
                   response = request.get(basePath); //returns response
                   break;
               case "POST":
                   response = request.post(basePath); //returns response
                   break;
               case "PUT":
                   response = request.put(basePath); //returns response
                   break;
               case "DELETE":
                   response = request.delete(basePath); //returns response
                   break;

               default:
                   Console.WriteLine("Please pass the correct http Method...");
                   break;
           }

           return response;
       }


What I have tried:

I know their is
request.Method = Method.GET;
but how can I use this in my switch case with the basePath
Posted
Updated 12-May-20 1:04am
v2
Comments
F-ES Sitecore 12-May-20 7:01am    
Never used RestSharp but it seems you use the Execute method

https://visualstudiomagazine.com/articles/2015/10/01/consume-a-webapi.aspx

1 solution

The IRestRequest doesn't contain any methods for executing a request. You will need an IRestClient instance for that.
C#
private static IRestResponse executeAPI(IRestClient client, string httpMethod, IRestRequest request, string basePath)
{
    Method method;
    if (!Enum.TryParse(httpMethod, out method))
    {
        throw new ArgumentException("Invalid HTTP method.", nameof(httpMethod));
    }
    
    request.Method = method;
    request.Resource = basePath;
    return client.Execute(request);
}
 
Share this answer
 
Comments
Member 14779968 12-May-20 23:46pm    
okay I have question since I am returning the executeAPI method in another method called getResponse() which I used to get my response in my get and post call will I have to add the IRestClient instance to do my getResponse() and my GET call as well. Just wondering how will I set the client and the httpMethod when I call my doGet method since that is string

Here is my getResponse method

private static IRestResponse getResponse(string httpMethod, IRestRequest request, IRestClient client, string basePath)
{
return executeAPI(httpMethod, request, client, basePath);
}

and here is my Get method

public static IRestResponse doGet(string contentType, string baseURI, string basePath,
string token, Dictionary<string, string=""> paramsMap)
{
if (setBaseURI(baseURI))
{
IRestRequest request = sendRequest(contentType, token, paramsMap);
return getResponse("GET", request, basePath);
}
return null;

}


//Currently calling my GET Method like this
[Test]
public void getAccountImageTest2()
{
IRestResponse response = BaseServiceCall.doGet(baseURL, basePath,token, null);
int statusCode = (int)response.StatusCode;
Assert.AreEqual(response.StatusCode, statusCode);
}
Richard Deeming 13-May-20 6:02am    
If you want to execute the request, you have to have an IRestClient object. If it's not a field or property of the object which contains the method, then you'll have to pass it in as a parameter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900