Call the Librarian API

BarTender Cloud exposes a REST API. Typically, interaction with a REST API requires a request/response call structure. While some requests can be sent to the REST API by a correctly formatted URI string, others require a more structured payload.

When you use C# as a programming environment, a request payload is typically implemented by using a C# class that contains properties whose values are configured to reflect the request that is being made. This payload is then serialized and sent over the network to the target REST API endpoint. After the REST endpoint processes the message, a response payload is constructed, serialized, sent, and received as the result of the initial REST API call.

The API call in this example involves sending a query whose response will list the Librarian spaces that are available to the authenticated user. Librarian is the document management component of BarTender Cloud, and it supports and exposes concepts such as folders, files, and file attachments. Librarian consists of one or more spaces under which you can build a hierarchy of folders that contain files. An analogy for a Librarian space would be a named hard disk partition, such as "C drive."

To query the list of available spaces, you need definitions of the models that are used to describe a space object, as follows:

  • The first model represents a user, such as the user who created the space. Note that the CreatedBy user field can be null for the initial spaces that are included by default with a new tenant.

  • The second model represents the space object itself.

To complete the following procedure, you will need the following files:

  • User.cs

  • Space.cs

For more information, refer to the "Example Requirements" section in C# Web-Based Application.

Add the User and Space models

  1. In the project solution in Visual Studio, locate the Models folder.

  2. Copy the User.cs and Space.cs files into the Models folder.

After you add these files, your project folder hierarchy should resemble the following image.

Next, you will add a controller to interact (via REST API calls) with BarTender Cloud and to provide a user interface module that will drive the controller.

To do this, you will need the following files:

  • LibrarianController.cs

  • Spaces.cshtml

For more information, refer to the "Example Requirements" section in C# Web-Based Application.

The controller exposes a "spaces" endpoint that, when combined with the "spaces" view, renders information about the Librarian space objects that the user can access.

Add the controller and user interface module

  1. In the project, copy the LibrarianController.cs file to the Controllers folder.

  2. In the Views folder, create a subfolder that is named Librarian.

  3. Copy the Spaces.cshtml file to the Librarian folder.

After you add the controller and user interface module, you must connect them to the existing user interface. To do this, open the _Layout.cshtml file, and then add the following code to the file. 

Copy
                  @if (User.Identity.IsAuthenticated)
                      {
                          <li class="nav-item">
                           <a class="nav-link text-dark" asp-area="" asp-controller="Librarian" asp-action="Spaces" class="navbar-brand">Spaces</a>
                          </li>
                        }

After you complete these steps and then compile and run the application, a "Spaces" item is displayed at the top of the web page. When you click this item, the LibrarianController’s "spaces" endpoint is invoked, which results in a query that displays a list of the spaces that are available to the user.