public async Task <SpacesEnumerationList> GetSpacesListAsync(CancellationToken cancellationToken) { return(await Wrapped(async (token) => { var apiResult = await _lowLevelApiClient.SpacesGetListAsync(token); return MapOrFail(apiResult, (dto) => SpacesEnumerationMapper.MapFromDto(dto)); }, cancellationToken, OperationType.SessionOpenAndRelated)); }
public async Task <SpacesEnumerationList> GetSpacesListAsync(CancellationToken cancellationToken) { return(await GetDataWithCancelAfter(async (token) => { var nvc = new NameValueCollection(); nvc.Add("_", DateTime.Now.Ticks.ToString()); var url = "spaces/list" + nvc.ToQueryString(); using (var response = await GetHttpClient().GetAsync(url, token)) { var dto = await HandleResponse <SpacesEnumerationDto>(response); return SpacesEnumerationMapper.MapFromDto(dto); } }, TimeSpan.FromSeconds(20), cancellationToken)); }
/// <summary> /// Opens session based on required authentication mechanism /// </summary> /// <param name="openSessionRequest"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <ApiSession> OpenSessionAsync(OpenSessionRequest openSessionRequest, CancellationToken ct) { if (openSessionRequest == null) { throw new ArgumentNullException(nameof(openSessionRequest)); } if (string.IsNullOrWhiteSpace(openSessionRequest.SpaceName)) { throw new ArgumentException("Space name is not set.", nameof(openSessionRequest.SpaceName)); } using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct)) { var timeout = clientConfiguration.SessionOpenTimeout; linkedTokenSource.CancelAfter(timeout); var cancellationToken = linkedTokenSource.Token; try { // tring to resolve space and space auth method. // Method 1. using get all spaces method var spacesListApiResult = await _lowLevelApiClient.SpacesGetListAsync(cancellationToken); SpaceEnumerationItem desiredSpace = null; if (!spacesListApiResult.IsSucceed && spacesListApiResult.Error is MorphApiForbiddenException) { // space listing disabled has been disabled be server admin. // Method 2. Using spaces lookup (new endpoint since next version of EM Server 4.3) var lookupApiResult = await _lowLevelApiClient.SpacesLookupAsync(new SpacesLookupRequestDto() { SpaceNames = { openSessionRequest.SpaceName } }, cancellationToken); desiredSpace = MapOrFail(lookupApiResult, (dto) => { // response have at least 1 element with requested space. var lookup = dto.Values.First(); if (lookup.Error != null) { // seems that space not found. throw new Exception($"Unable to open session. {lookup.Error.message}"); } else { // otherwise return space return(SpacesEnumerationMapper.MapItemFromDto(lookup.Data)); } } ); } else { var spacesListResult = MapOrFail(spacesListApiResult, (dto) => SpacesEnumerationMapper.MapFromDto(dto)); desiredSpace = spacesListResult.Items.FirstOrDefault(x => x.SpaceName.Equals(openSessionRequest.SpaceName, StringComparison.OrdinalIgnoreCase)); } if (desiredSpace == null) { throw new Exception($"Unable to open session. Server has no space '{openSessionRequest.SpaceName}'"); } var session = await MorphServerAuthenticator.OpenSessionMultiplexedAsync(desiredSpace, new OpenSessionAuthenticatorContext( _lowLevelApiClient, this as ICanCloseSession, (handler) => ConstructRestApiClient(BuildHttpClient(clientConfiguration, handler))), openSessionRequest, cancellationToken); return(session); } catch (OperationCanceledException) when(!ct.IsCancellationRequested && linkedTokenSource.IsCancellationRequested) { throw new Exception($"Can't connect to host {clientConfiguration.ApiUri}. Operation timeout ({timeout})"); } } }