Admin API
Caution
Sample code provided in here is for demonstration and educational purposes only which provides customers with programming information regarding the products and is not intended for use in a production environment. All production code should always follow development best practices. All sample code here is supplied "AS IS" without any warranties or support.
This section describes how to use the admin API, how to retrieve a access token and how to call the API.
The provided C# code samples below uses HttpClient to make the REST calls.
Warning
A common mistake when using HttpClient is to use multiple instances of the
HttpClient. You should reuse the same instance within an application.
Authenticate
The admin API authentication is not based on a user token that expires after a certain amount of time. The authorization header for all admin API calls has a bearer token that is created using the symmetric key (admin, auth, snapshot) and the relative path.
In C# a way is to extend AuthenticationHeaderValue, below is an example of how
this can be done:
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
namespace Demo.AdminApi;
public class BearerAuthenticationHeaderValue : AuthenticationHeaderValue
{
public BearerAuthenticationHeaderValue(string symmetricKey, string relativeUri)
: base("Bearer", GetBearerToken(symmetricKey, relativeUri))
{
}
private static string GetBearerToken(string symmetricKey, string relativeUri)
{
byte[] encodedPath = Encoding.ASCII.GetBytes(relativeUri);
byte[] byteKey = Convert.FromBase64String(symmetricKey);
HMACSHA512 sha = new HMACSHA512(byteKey);
byte[] hash = sha.ComputeHash(encodedPath);
string hashString = Convert.ToBase64String(hash);
string bearerToken = hashString.Split('=')[0];
bearerToken = bearerToken.Replace('+', '-');
bearerToken = bearerToken.Replace('/', '_');
return bearerToken;
}
}
In PowerShell an authentication header can be created with a function that looks like the one below:
Download get-authorization-header.ps1
function GetAuthorizationHeader ($key, $path){
$encodedPath = [Text.Encoding]::ASCII.GetBytes($path)
$sha = New-Object System.Security.Cryptography.HMACSHA512
$sha.key = [Convert]::FromBase64String($key)
$hash = $sha.ComputeHash($encodedPath)
$hashString = [Convert]::ToBase64String($hash)
$bearerToken = $hashString.Split('=')[0]
$bearerToken = $bearerToken.Replace('+', '-')
$bearerToken = $bearerToken.Replace('/', '_')
return @{"Authorization" = ("Bearer", $bearerToken -join " ")}
}
Other C-sharp examples
Below is a collection of examples of using the admin API.
ReIndex The re-index function will do a re-indexation of all indexes in a certain space of ShareAspace Nova, the symmetric key used is the administration symmetric key.
public static async Task ReIndexAsync(string baseAddress, string adminApiKey, string spaceId)
{
string relativeUri = $"/admin/reindex/{spaceId}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Authorization = new BearerAuthenticationHeaderValue(adminApiKey, relativeUri);
HttpResponseMessage response = await client.PostAsync(relativeUri, null);
response.EnsureSuccessStatusCode();
client.Dispose();
}
RebuildAnIndex The re-index of one index will only re-index a specific index for a specific space, the symmetric key used is the administration symmetric key.
public static async Task RebuildAnIndexAsync(string baseAddress, string adminApiKey, string spaceId, string indexId)
{
string relativeUri = $"/admin/reindex/{spaceId}/{indexId}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Authorization = new BearerAuthenticationHeaderValue(adminApiKey, relativeUri);
HttpResponseMessage response = await client.PostAsync(relativeUri, null);
response.EnsureSuccessStatusCode();
client.Dispose();
}
Upload It is possible to upload files to the collection using the upload api call. The symmetric key used is the task symmetric key.
public static async Task UploadAsync(string baseAddress, string taskApiKey, string path, string filePath)
{
string relativeUri = $"/system/upload/{path}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Authorization = new BearerAuthenticationHeaderValue(taskApiKey, relativeUri);
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamContent content = new StreamContent(file);
HttpResponseMessage response = await client.PostAsync(relativeUri, content);
response.EnsureSuccessStatusCode();
}
client.Dispose();
}
Download To download the files from collection use the download API call. The symmetric key used is the task symmetric key.
public static async Task DownloadAsync(string baseAddress, string taskApiKey, string path, string filePath)
{
string relativeUri = $"/system/download/{path}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Authorization = new BearerAuthenticationHeaderValue(taskApiKey, relativeUri);
Stream response = await client.GetStreamAsync(relativeUri);
using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
response.CopyTo(file);
}
client.Dispose();
}
Download sample
The examples above are collected in a project that can be downloaded.