Skip to content

Getting Started

Hassan F edited this page Feb 6, 2021 · 7 revisions

This page provides samples and info about how to get started with ARH.

Current Version: 1.3.0

Installation

Before we start requesting a service, we need to install the package, or consume it somehow, and I just happen to create a Nuget package for this case. You can install using following command or by using other command provided by Nuget, or even consume it just by using the released file in Github.

  • just search for AdvancedRestHandler in NuGet Package Manager UI
  • using Package Manager Console command: Install-Package AdvancedRestHandler -Version 1.3.0
  • using .Net CLI command: dotnet add package AdvancedRestHandler --version 1.3.0

First Request

To make your first request you will need to create an instance of AdvancedRestHandler class, or it's wrappers.

The classes that currently can be used are:

  • AdvancedRestHandler: Uses any case you use, but since C# programming standard defines properties in PascalCase so we can say, its PascalCase name handling while processing JSON data, e.g.: HelloWorld
  • ArhCamelCaseWrapper: Uses CamelCase name handling while processing JSON data, e.g.: helloWorld
  • ArhSnakeCaseWrapper: Uses SnakeCase name handling while processing JSON data, e.g.: hello_world

So simply check what version of naming the desired service uses the most, and choose the corresponding wrapper to keep your manual name handling at minimum.

The simplest call you can make can be like this:

namespace ArhExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            var response = new AdvancedRestHandler().GetData<GettingStartedResponse>(
                "https://6016529c55dfbd00174cad29.mockapi.io/api/getting-started");
        }
    }

    public class GettingStartedResponse
    {
        public string Data { get; set; }
        public bool Succeed { get; set; }
    }
}

I create an online mock service for you, and this request works in real life

Make It Reusable

ARH is created to simplify request as much as possible, leaving the complexity at its own side; so you can create a single class to centralize your request without putting too much code and complexity on one place.

Here is an example of that, I create a MockAPI using https://mockapi.io

namespace ArhExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use our custom created class, and initialize it using our Base-URL
            var client = new ContentClient("https://6016529c55dfbd00174cad29.mockapi.io/api/");

            // Create new Item
            var newContent = client.CreateContent(new CreateContentReq
            {
                Content = "this is a new content",
                Creator = "Hassan Faghihi"
            });

            // Get Item by ID
            var content = client.GetContent(2);
            Console.WriteLine("2nd Content: " + content.Content);

            // Put (Update/Replace) Item
            client.UpdateContent(2, new UpdateContentReq
            {
                Content = "This is the updated version of second content using PUT request" + DateTime.Now.ToString("G")
            });

            // Get All Items
            var allContents = client.GetAllContents();
            Console.WriteLine("Count: "+ allContents.Count);
            Console.WriteLine("First: "+ allContents.FirstOrDefault()?.Content);

            // Patch (Partial Update) Item
            client.UpdatePartial(2, new UpdateContentReq
            {
                Content = "This is the updated version of second content using PATCH request" + DateTime.Now.ToString("G")
            });

            // Delete Item
            client.DeleteContent(allContents.LastOrDefault().Id);
        }
    }


    public class ContentClient
    {
        private readonly AdvancedRestHandler _arh;

        public ContentClient(string baseUrl)
        {
            _arh = new ArhCamelCaseWrapper(baseUrl);
        }

        public List<GetAllContentsRes> GetAllContents()
        {
            var result = _arh.GetData<List<GetAllContentsRes>>("content");
            return result;
        }

        public GetContentRes GetContent(long id)
        {
            var result = _arh.GetData<GetContentRes>($"content/{id}");
            return result;
        }

        public CreateContentRes CreateContent(CreateContentReq req)
        {
            var result = _arh.PostData<CreateContentRes, CreateContentReq>("content", req);
            return result;
        }

        public void UpdateContent(long id, UpdateContentReq req)
        {
            var result = _arh.PutData<object, UpdateContentReq>($"content/{id}", req);
        }

        public void UpdatePartial(long id, UpdateContentReq req)
        {
            throw new Exception("https://mockapi.io/ does not support patch request");
            var result = _arh.PatchData<object, UpdateContentReq>($"content/update-partial/{id}", req);
        }

        public void DeleteContent(long id)
        {
            var result = _arh.DeleteData<object>($"content/{id}");
        }
    }

    public class UpdateContentReq
    {
        public long Id { get; set; }
        public string Creator { get; set; }
        public string Content { get; set; }
    }

    public class CreateContentReq
    {
        public string Creator { get; set; }
        public string Content { get; set; }
    }

    public class CreateContentRes
    {
        public long Id { get; set; }
        public string Creator { get; set; }
        public string Content { get; set; }
        public string CreatedAt { get; set; }
    }

    public class GetContentRes
    {
        public long Id { get; set; }
        public string Creator { get; set; }
        public string Content { get; set; }
        public string CreatedAt { get; set; }
    }

    public class GetAllContentsRes
    {
        public long Id { get; set; }
        public string Creator { get; set; }
        public string Content { get; set; }
        public string CreatedAt { get; set; }
    }
}
Clone this wiki locally