Skip to content

alico/utils-apicaller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Caller

A RESTful API caller for .NET projects

Versions:
Framework .NET 5
Language C# 9

Test API

Endpoints

    private static string url = "https://reqres.in/api/";
    private static string registerEndpoint = "register";
    private static string userEndpoint = "users";

HTTPGET

    public static async Task<Root> TestGetMethod()
    {
        var requestModel = new APICallRequestModel()
        {
            ContentType = ContentType.ApplicationJson,
            Endpoint = userEndpoint,
            Url = url,
            RequestObject = new UserListRequestModel()
            {
                Page = 2
            }
        };
       
        var response = await ApiCaller<Root>.GetAsync(requestModel);
        return response.Body;
    }

HTTPPOST

    public static async Task<UserRequestModel> TestPostMethod()
    {
       var requestModel = new APICallRequestModel()
       {
          ContentType = ContentType.ApplicationJson,
          Endpoint = userEndpoint,
          Url = url,
          RequestObject = new UserRequestModel()
          {
              Name = "John",
              Job = "Team Leader"
          }
       };
        var response = await ApiCaller<UserRequestModel>.PostAsync(requestModel);
        return response.Body;
    }

HTTPPUT

    public static async Task<UserRequestModel> TestPutMethod()
    {
        var requestModel = new APICallRequestModel()
        {
           ContentType = ContentType.ApplicationJson,
           Endpoint = userEndpoint,
           Url = url,
           RequestObject = new UserRequestModel()
           {
              Name = "John",
              Job = "Team Leader"
           };
        };

        var response = await ApiCaller<UserRequestModel>.PutAsync(requestModel);
        return response.Body;
    }

HTTPDELETE

    public static async Task TestDeleteMethod()
    {
        var requestModel = new APICallRequestModel()
        {
           ContentType = ContentType.ApplicationJson,
           Endpoint = $"{requestModel.Endpoint}/2";
           Url = url
        };
        var response = await ApiCaller<object>.DeleteAsync(requestModel);
    }

Error State

    public static async Task TestErrorRespnse()
    {
        var requestModel = new APICallRequestModel()
        {
           ContentType = ContentType.ApplicationJson,
           Endpoint = registerEndpoint,
           Url = url,
           RequestObject = new SignIn()
           {
               Email = "test@test"
           };
        };
       
        var response = await ApiCaller<object>.PostAsync(requestModel);
    }