The Kanda CloudClient provides a convenient way to interact with Kanda Cloud services. It handles authentication, manages API requests, and provides access to various endpoints for different cloud functionalities.
Obtaining the CloudClient
The CloudClient can be obtained using the AppServiceLocator. This ensures that you're using a properly configured instance of the client throughout your application.
using Kanda.Foundation.DependencyInjection;
Provides a way to connect and interact with Kanda Cloud services.
Definition CloudClient.cs:11
Definition CloudAuthentication.cs:19
Authentication
The CloudClient supports two main authentication flows: Saved Credentials Login and Device Code Login.
Saved Credentials Login
This method attempts to log in using previously saved credentials.
bool loginSuccess = await cloudClient.
Auth.TryLoginWithSavedCredentials(CancellationToken.None);
if (loginSuccess)
{
KandaLog.Write("Successfully logged in with saved credentials");
}
else
{
KandaLog.Write("No valid saved credentials found");
}
ICloudAuthentication Auth
Used for login and credentials management.
Definition CloudClient.cs:15
Device Code Login
This method initiates a device code login flow, which is useful for devices without easy input methods.
var deviceCodeData = await cloudClient.
Auth.StartLoginWithDeviceCode(CancellationToken.None);
if (deviceCodeData.Succeeded)
{
KandaLog.Write($"Please enter this code: {deviceCodeData.UserCode}");
KandaLog.Write($"At this URL: {deviceCodeData.ActivationUrl}");
}
else
{
KandaLog.Write($"Failed to start device code flow: {deviceCodeData.Message}");
}
cloudClient.Auth.OnLogin += (LoginType loginType) =>
{
KandaLog.Write($"Successfully logged in via {loginType}");
};
cloudClient.Auth.OnLoginError += (string errorMessage) =>
{
KandaLog.Write($"Login failed: {errorMessage}");
};
Using Endpoints
Once authenticated, you can use various endpoints provided by the CloudClient.
Check-Ins
Check-ins are used to test connectivity and track active users.
var checkInRequest = new CheckInPostRequest { Origin = "unity3d" };
var response = await cloudClient.
CheckIns.CheckIn(checkInRequest, CancellationToken.None);
if (response.Succeeded)
{
KandaLog.Write($"Check-in successful. ID: {response.Data.Id}");
}
else
{
KandaLog.Write($"Check-in failed. Status code: {response.StatusCode}");
}
ICheckInEndpoint CheckIns
CheckIns are a way of testing connectivity and saying "hello" to Kanda Cloud services....
Definition CloudClient.cs:21
Room Servers
Room servers provide functionality for allocating and managing multiplayer server instances.
var joinRequest = new JoinRoomServerRequest { RoomInstanceId = "room-123" };
var joinResponse = await cloudClient.
RoomServers.Join(joinRequest, CancellationToken.None);
if (joinResponse.Succeeded)
{
KandaLog.Write($"Joined room. Status: {joinResponse.Data.Status}");
}
var leaveRequest = new LeaveRoomServerRequest { RoomInstanceId = "room-123" };
var leaveResponse = await cloudClient.
RoomServers.Leave(leaveRequest, CancellationToken.None);
if (leaveResponse.Succeeded)
{
KandaLog.Write($"Left room. Status: {leaveResponse.Data.Status}");
}
IRoomServerEndpoint RoomServers
Room servers provide a way to allocate multiplayer servers dedicated to room instances.
Definition CloudClient.cs:26
Error Handling
All endpoint methods return a CloudApiResponse<T> object, which includes information about the success of the request, the status code, and any error messages.
var response = await cloudClient.
CheckIns.CheckIn(checkInRequest, CancellationToken.None);
if (!response.Succeeded)
{
KandaLog.WriteError($"Request failed. Status code: {response.StatusCode}");
KandaLog.WriteError($"Error message: {response.ErrorMessage}");
}
Remember to handle potential exceptions that may be thrown during network operations or when the client is not properly initialized.
This documentation provides a coarse overview of how to use the Kanda CloudClient. Always refer to the latest API documentation for the most up-to-date information on available endpoints and their usage.