Version v14 (and Earlier)

Learn How to Install, Initialize and Create Classes for Version 14 of the SDK

Introduction

The basic concepts of the SDK are documented at the root page, linked below. Please note, they are the same for all versions of this package.

page.NET

This document focuses on the initialization of the SDK and how to use the concrete client classes.

Install the SDK

The SDK is available on nuget.org. You can install it with:

dotnet add package Squidex.ClientLibrary

Manually Creating Classes

The main entry class is SquidexClientManager, which handles authentication and creates the actual client classes, where each client is used for one endpoint such as assets, schemas and so on.

The client will create an access token using the App client credentials and cache this token in the memory for 30 days. When the token expires, it is recreated automatically. The cache is not shared between the instances of your application and not needed.

Read more about the authentication flow and best practices below:

pageAuthentication

To instantiate the client manager, you need the App Name, the Client Id and Client Secret. For self-hosted installations, the URL is also needed. For Squidex Cloud it is https://cloud.squidex.io.

ISquidexClientManager clientManager =
    new SquidexClientManager(
        new SquidexOptions
        {
            AppName = "app",
            ClientId = "id",
            ClientSecret = "secret",
            Url = "https://cloud.squidex.io"
        });

Configure Multiple Apps

The SDK supports multiple Apps using the normal options. When you create a request using an end point client you have to define the App Name, and the client manager picks the correct credentials. When you create a content client, you can also specify the App Name.

ISquidexClientManager clientManager =
    new SquidexClientManager(
        new SquidexOptions
        {
            AppName = "app",
            ClientId = "id",
            ClientSecret = "secret",
            Url = "https://cloud.squidex.io",
            AppCredentials = new Dictionary<string, AppCredentials>
            {
               ["other-website"] = new AppCredentials
               {
                   ClientId = "...",
                   ClientSecret = "...",
               }
            }
        });

Creating Classes with Dependency Injection

If you use Dependency Injection (especially in ASP.NET Core) you can use the following package:

dotnet add package Squidex.ClientLibrary.ServiceExtensions

This package provides extension methods to register the client manager at the service collection.

services
    .AddSquidexClient(options =>
    {
        options.AppName = "app";
        options.ClientId = "id";
        options.ClientSecret = "secret";
        options.Url = "https://cloud.squidex.io";
    });

You can inject ISquidexClientManager to your other classes.

This configuration uses the Options Pattern, so it can also be configured the following way:

services.AddSquidexClient()
    .Configure<SquidexServiceOptions>(options =>
    {
        options.AppName = "app";
        options.ClientId = "id";
        options.ClientSecret = "secret";
        options.Url = "https://cloud.squidex.io";
    });

Another option is to bind it to a configuration section as follows:

services.AddSquidexClient();
services.Configure<SquidexServiceOptions>(
    configuration.GetSection("squidex"));

Configure Multiple Apps

You can also configure multiple Apps using the normal options as follows:

services.AddSquidexClient(options =>
{
    options.AppName = "app";
    options.ClientId = "id";
    options.ClientSecret = "secret";
    options.Url = "https://cloud.squidex.io";
    options.AppCredentials = new Dictionary<string, AppCredentials>
    {
       ["other-app"] = new AppCredentials
       {
           ClientId = "...",
           ClientSecret = "...",
       }
    };
})

Configure the HTTP pipeline

The package also integrates the HttpClientFactory to implement resilient HTTP requests. For example, this can be used to enable logging or to integrate Polly, a resilience and transient-fault-handling library.

You can make changes to the HTTP pipeline using the following method:

serviceCollection.AddSquidexHttpClient()
   .AddHttpMessageHandler(() =>
   {
       // YOUR CODE
   }).Services

Use Concrete Clients

The classes for concrete endpoints can be created with the client manager. These instances are not cached and a new instance is returned for each call. Therefore, you should keep the instance as a local variable and field, and use them as often as possible.

var assetsClient = client.CreateAssetsClient();

var assets1 = await assetsClient.GetAssetsAsync();

// Just use the client directly.
var assets2 = await client.Assets.GetAssetsAsync();

The content clients are also not cached. They can be created using the following method:

var blog1 = client.CreateContentsClient<BlogPost, BlogPostData>("blog1");
var blog1 = client.CreateContentsClient<BlogPost, BlogPostData>("blog1");

ReferenceEquals(blog1, blog2) == false;

Using Dependency Injection

The endpoint clients are registered in the service locator. Therefore, you can also inject endpoint classes to your service class MyService.

{
    public MyService(IAssetsClient assetsClient)
    {
    }
}

The content clients need parameters to be created. Therefore, you have to register them manually.

services.AddSquidexClient(
    cm => cm.CreateContentsClient<BlogPost, BlogPost>("blog");

class MyService
{
    public MyService(IContentsClient<BlogPost, BlogPost> blog)
    {
    }
}

Last updated