using System.Threading.Tasks; using System.Collections.Generic; using Unity.Services.Relay.Models; using Unity.Services.Relay.Http; using TaskScheduler = Unity.Services.Relay.Scheduler.TaskScheduler; using Unity.Services.Authentication.Internal; using Unity.Services.Relay.Allocations; namespace Unity.Services.Relay.Apis.Allocations { public interface IAllocationsApiClient { /// /// Async Operation. /// Create Allocation /// /// Request object for CreateAllocation /// Configuration for CreateAllocation /// Task for a Response object containing status code, headers, and AllocateResponseBody object /// An exception containing the HttpClientResponse with headers, response code, and string of error. Task> CreateAllocationAsync(CreateAllocationRequest request, Configuration operationConfiguration = null); /// /// Async Operation. /// Create Join Code /// /// Request object for CreateJoincode /// Configuration for CreateJoincode /// Task for a Response object containing status code, headers, and JoinCodeResponseBody object /// An exception containing the HttpClientResponse with headers, response code, and string of error. Task> CreateJoincodeAsync(CreateJoincodeRequest request, Configuration operationConfiguration = null); /// /// Async Operation. /// Join Relay /// /// Request object for JoinRelay /// Configuration for JoinRelay /// Task for a Response object containing status code, headers, and JoinResponseBody object /// An exception containing the HttpClientResponse with headers, response code, and string of error. Task> JoinRelayAsync(JoinRelayRequest request, Configuration operationConfiguration = null); /// /// Async Operation. /// List relay regions /// /// Request object for ListRegions /// Configuration for ListRegions /// Task for a Response object containing status code, headers, and RegionsResponseBody object /// An exception containing the HttpClientResponse with headers, response code, and string of error. Task> ListRegionsAsync(ListRegionsRequest request, Configuration operationConfiguration = null); } /// public class AllocationsApiClient : BaseApiClient, IAllocationsApiClient { private IAccessToken _accessToken; private const int _baseTimeout = 10; private Configuration _configuration; public Configuration Configuration { get { // We return a merge between the current configuration and the // global configuration to ensure we have the correct // combination of headers and a base path (if it is set). return Configuration.MergeConfigurations(_configuration, RelayService.Configuration); } } public AllocationsApiClient(IHttpClient httpClient, IAccessToken accessToken, Configuration configuration = null) : base(httpClient) { // We don't need to worry about the configuration being null at // this stage, we will check this in the accessor. _configuration = configuration; _accessToken = accessToken; } public async Task> CreateAllocationAsync(CreateAllocationRequest request, Configuration operationConfiguration = null) { var statusCodeToTypeMap = new Dictionary() { { "201", typeof(AllocateResponseBody) },{ "400", typeof(ErrorResponseBody) },{ "401", typeof(ErrorResponseBody) },{ "403", typeof(ErrorResponseBody) },{ "500", typeof(ErrorResponseBody) } }; // Merge the operation/request level configuration with the client level configuration. var finalConfiguration = Configuration.MergeConfigurations(operationConfiguration, Configuration); var response = await HttpClient.MakeRequestAsync("POST", request.ConstructUrl(finalConfiguration.BasePath), request.ConstructBody(), request.ConstructHeaders(_accessToken, finalConfiguration), finalConfiguration.RequestTimeout ?? _baseTimeout); var handledResponse = ResponseHandler.HandleAsyncResponse(response, statusCodeToTypeMap); return new Response(response, handledResponse); } public async Task> CreateJoincodeAsync(CreateJoincodeRequest request, Configuration operationConfiguration = null) { var statusCodeToTypeMap = new Dictionary() { { "200", typeof(JoinCodeResponseBody) },{ "201", typeof(JoinCodeResponseBody) },{ "400", typeof(ErrorResponseBody) },{ "401", typeof(ErrorResponseBody) },{ "403", typeof(ErrorResponseBody) },{ "500", typeof(ErrorResponseBody) } }; // Merge the operation/request level configuration with the client level configuration. var finalConfiguration = Configuration.MergeConfigurations(operationConfiguration, Configuration); var response = await HttpClient.MakeRequestAsync("POST", request.ConstructUrl(finalConfiguration.BasePath), request.ConstructBody(), request.ConstructHeaders(_accessToken, finalConfiguration), finalConfiguration.RequestTimeout ?? _baseTimeout); var handledResponse = ResponseHandler.HandleAsyncResponse(response, statusCodeToTypeMap); return new Response(response, handledResponse); } public async Task> JoinRelayAsync(JoinRelayRequest request, Configuration operationConfiguration = null) { var statusCodeToTypeMap = new Dictionary() { { "200", typeof(JoinResponseBody) },{ "400", typeof(ErrorResponseBody) },{ "401", typeof(ErrorResponseBody) },{ "403", typeof(ErrorResponseBody) },{ "404", typeof(ErrorResponseBody) },{ "500", typeof(ErrorResponseBody) } }; // Merge the operation/request level configuration with the client level configuration. var finalConfiguration = Configuration.MergeConfigurations(operationConfiguration, Configuration); var response = await HttpClient.MakeRequestAsync("POST", request.ConstructUrl(finalConfiguration.BasePath), request.ConstructBody(), request.ConstructHeaders(_accessToken, finalConfiguration), finalConfiguration.RequestTimeout ?? _baseTimeout); var handledResponse = ResponseHandler.HandleAsyncResponse(response, statusCodeToTypeMap); return new Response(response, handledResponse); } public async Task> ListRegionsAsync(ListRegionsRequest request, Configuration operationConfiguration = null) { var statusCodeToTypeMap = new Dictionary() { { "200", typeof(RegionsResponseBody) },{ "400", typeof(ErrorResponseBody) },{ "401", typeof(ErrorResponseBody) },{ "403", typeof(ErrorResponseBody) },{ "500", typeof(ErrorResponseBody) } }; // Merge the operation/request level configuration with the client level configuration. var finalConfiguration = Configuration.MergeConfigurations(operationConfiguration, Configuration); var response = await HttpClient.MakeRequestAsync("GET", request.ConstructUrl(finalConfiguration.BasePath), request.ConstructBody(), request.ConstructHeaders(_accessToken, finalConfiguration), finalConfiguration.RequestTimeout ?? _baseTimeout); var handledResponse = ResponseHandler.HandleAsyncResponse(response, statusCodeToTypeMap); return new Response(response, handledResponse); } } }