Timer
Table of Contents |
---|
Overview
The Tsplice .NET API can be used in a client defines classes that allow a .NET application to interact with a Tsplice host. This API consists of the following:Generally speaking, the flow of code is as follows:
Create an instance of a TPRI.Tsplice.Client - This class can be used to send a request to the Tsplice host and parse its response
TPRI.Tsplice.Models - The classes in this namespace represent the different types of data sent to or received from the Tsplice host
ApiResponse and derived classes - These classes represent the responses returned from the Tsplice host, and typically wrap a model object containing the specific response data
TPRI.Tsplice.Client
Overview
Properties
There are two types of timers covered by these libraries: wall clock timers and set interval timers.
Methods
The Tsplice Client methods are grouped below by function.
Authentication & Authorization
The following functions can be used to authenticate and authorize a user via the Tsplice API
Expand | ||
---|---|---|
| ||
|
Expand | ||
---|---|---|
| ||
|
Expand | ||
---|---|---|
| ||
|
System Status
The following function can be used to get the current status of the Tsplice host system.
Expand | ||
---|---|---|
| ||
|
Shared Files and Folders
The following functions can be used to interact with shared folders and files on the Tsplice host system.
...
title | Get File from Host |
---|
...
class and set the URI of the Tsplice host.
Call member functions of the client class to send to or retrieve data from a Tsplice host.
Process the response data objects and/or handle exceptions thrown by the client method.
Response Data
Every Tsplice client method returns a TPRI.Tsplice.Models.ApiResponse object:
For methods that return data, the data itself is contained in the Value property of the ApiResponse.
For methods that retrieve a file from the host, the returned object is a FileResponse object (derived from ApiResponse) with properties such as LocalPath indicating where the file has been downloaded locally.
Error Handling
Any error that occurs during a Tsplice client method will throw an exception:
If the exception is specific to the Tsplice interface, a TPRI.Tsplice.ClientException will be thrown with an ErrorCode property indicating the type of error and a Message property providing some description of the problem.
Other exceptions may be thrown depending on the type of error e.g. local file I/O exception, JSON serialization exception, etc.
If a Tsplice client method returns without an exception, then the call completed successfully.
Async vs. Sync
All Tsplice client calls are async calls intended to be awaited.
Code Block |
---|
// Request the system status
var response = await client.GetSystemStatus(); |
If async calls are not possible in the calling application, the Tsplice client class offers a Sync(..) method that can be used to wrap any client async method:
Code Block |
---|
// Request the system status
var response = TPRI.Tsplice.Client.Sync(() => client.GetSystemStatus()); |
Cancelling Long-Running Requests
Each client method includes an optional cancelToken parameter that can be used to interrupt and cancel a long-running call.
Code Block |
---|
// Create a cancellation source CancelSource = new CancellationTokenSource(); //... try { // Download a bunch of files from the Tsplice host var response = await client.GetSharedFiles("SharedFolder", "*",CancellationToken cancelToken = default) ///----------------------------------------------------------------- /// <summary> /// Download a shared file into the specified location /// </summary>
/// <param name="sharePath">Shared folder path</param>
/// <param name="localPath">
/// Target file path on local system, or null to use the default
/// download location
/// </param>
/// <param name="cancelToken">Cancellation token</param>
/// <returns>FileResponse object</returns>
///-----------------------------------------------------------------
public async Task<FileResponse> GetSharedFile("D:\LocalFolder\",string sharePath, string localPath = null,CancelSource.Token); }CancellationToken cancelToken = default) ///----------------------------------------------------------------- /// <summary> /// Download files matching a filter from a shared folder into /// the specified folder /// </summary> /// <param name="sharePath">Shared folder path</param> /// <param name="search"> /// Search string, or null to match all files /// </param> /// <param name="localRoot"> /// Root folder for local files, or null to use the default /// download folder /// </param> /// <param name="cancelToken">Cancellation token</param> /// <returns>FileResponse object</returns> ///----------------------------------------------------------------- public async Task<List<FileResponse>> GetSharedFiles( string sharePath, string search = null, string localRoot = null, CancellationToken cancelToken = default) |
Expand | ||
---|---|---|
| ||
|
Expand | ||
---|---|---|
| ||
Code Block | catch (TaskCanceledException)
{
// Someone called CancelSource.Cancel() before all of
// the files were downloaded
} |
Example
Following is an example usage of the Tsplice client:
Code Block | ||
---|---|---|
var client = new TPRI.Tsplice.Client(); client.BaseUri = new Uri("https://www.somewhere.com"); try { // Request a shared file fromone location the host to another
/// location on the host
/// </summary>
/// <param name="srcSharePath">Source shared file path</param>
/// <param name="dstSharePath">Destination shared file path</param>
/// <param name="cancelToken">Cancellation token</param>
/// <returns>ApiResponse object</returns>
///-----------------------------------------------------------------
public async Task<ApiResponse> CopySharedFile(
string srcSharePath, string dstSharePath,
CancellationToken cancelToken = default)
///-----------------------------------------------------------------
/// <summary>
/// Rename or move a shared file on the host
/// </summary>
/// <param name="srcSharePath">Source shared file path</param>
/// <param name="dstSharePath">Destination shared file path</param>
/// <param name="cancelToken">Cancellation token</param>
/// <returns>ApiResponse object</returns>
///-----------------------------------------------------------------
public async Task<ApiResponse> MoveSharedFile(
string srcSharePath, string dstSharePath,
CancellationToken cancelToken = default) | ||
Expand | ||
| ||
Code Block | Tsplice host
var response = await client.GetSharedFile(sharePath, localPath);
// ...
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} |