# IRestClient interface

## Overview

The REST SDK works with the `IRestClient` interface and can be used through dependency injection. The `IRestClient` interface has the following primary methods for working with data:

* `GetItem`
* `GetItems`
* `CreateItem`
* `UpdateItem`
* `DeleteItem`

## Use in Sitefinity ASP.NET Core Renderer projects

The `IRestClient` interface is automatically registered and is initialized for each request.

To use it, you need to inject it in the constructor through dependency injection (DI), like in the following example:

```C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.AspNetCore.Web;
using Progress.Sitefinity.RestSdk;

namespace Renderer.ViewComponents
{
    public class DemoViewComponent : ViewComponent
    {
        private IRestClient restClient;
        private IRenderContext renderContext;

        /// Initializes a new instance of the DemoViewComponent class
        public DemoViewComponent(IRestClient restClient, IRenderContext renderContext)
        {
            this.restClient = restClient;
            this.renderContext = renderContext;
        }
    }
}

```

## Use in custom implementations in Sitefinity ASP.NET Renderer projects

To use the `IRestClient` interface in custom controllers, you need to manually initialize the interface, like in the following example:

```C#
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk;
using Microsoft.Net.Http.Headers;

namespace Renderer.Controllers
{
    public class TestController : Controller
    {
        private IRestClient restClient;

        public TestController(IRestClient restClient)
        {
            this.restClient = restClient;
        }

        [HttpGet]
        public async Task<IActionResult> GetTags()
        {
            var taxonType = RestClientContentTypes.GetTaxonType("Tags");

            // necessary to initialize this. automatically done for page requests
            // adds the current cookies, if the user is logged-in, so that the request is authenticated
            var args = new RequestArgs();
            var requestCookie = this.HttpContext.Request.Headers[HeaderNames.Cookie];
            if (!string.IsNullOrEmpty(requestCookie))
                args.AdditionalHeaders.Add(HeaderNames.Cookie, requestCookie);

            await this.restClient.Init(args);
            var result = await this.restClient.GetItems<TaxonDto>(new GetAllArgs()
            {
                Type = taxonType,
                Fields = new[] { "Id", "Title" }
            });

            return this.Json(result);
        }
    }
}
```

## Use in custom implementations in external ASP.NET Core projects

To use the `IRestClient` interface in external projects, which do not use Sitefinity ASP.NET Core Renderer, you need to manually:

* Register the `IRestClient` interface on startup by adding the following services:

  ```C#
  // add the following services

  services.AddHttpClient("sfservice", (servicesProvider, client) =>
  {
      // mandatory: it is important not to skip the last backslash
      client.BaseAddress = new Uri("http://your.sitefinity.site/api/default/");

      // optional: the key is used for restriction of the Web Service. it is not used for managing content
      client.DefaultRequestHeaders.Add("X-SF-APIKEY", "your api key");

  }).ConfigurePrimaryHttpMessageHandler(configure =>
  {
      return new HttpClientHandler
      {
          AllowAutoRedirect = false,
          UseCookies = false,
      };
  })

  services.AddScoped<IRestClient>((x) =>
  {
      var factory = x.GetRequiredService<IHttpClientFactory>();
      var httpClient = factory.CreateClient("sfservice");

      var restClient = new RestClient(httpClient);
      return restClient;
  });
  ```
* Initialize the `IRestClient` interface for custom implementations, like in the example above.

## Examples

The following examples demonstrate how to use the `IRestClient` interface:

* [Sample: Migrate data to Sitefinity CMS](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/tree/master/src/migrate-data-to-cms)
* [Sample: Share data between widgets](https://www.progress.com/documentation/sitefinity-cms/sample--share-data-between-widgets)
* [Sample: Mega menu](https://www.progress.com/documentation/sitefinity-cms/sample--mega-menu)
