示例#1
0
 internal Cursor(ApiObject apiObject, EncompassRestClient client, string cursorId, int count, IEnumerable <string> fields)
 {
     _apiObject = apiObject;
     Client     = client;
     CursorId   = cursorId;
     Count      = count;
     Fields     = fields ?? Array <string> .Empty;
 }
示例#2
0
 internal Task TryInitializeAsync(EncompassRestClient client, ClientParameters parameters, CancellationToken cancellationToken)
 {
     if (parameters.CustomFieldsCacheInitialization != CacheInitialization.Never && !((DateTime.UtcNow - CustomFieldsLastRefreshedUtc)?.TotalMinutes < (int)parameters.CustomFieldsCacheInitialization))
     {
         return(RefreshCustomFieldsAsync(client, cancellationToken));
     }
     return(TaskHelper.CompletedTask);
 }
示例#3
0
        public static async Task <EncompassRestClient> CreateAsync(ClientParameters parameters, Func <TokenCreator, Task <string> > tokenInitializer, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(parameters, nameof(parameters));

            var client      = new EncompassRestClient(parameters, tokenInitializer);
            var accessToken = await tokenInitializer(new TokenCreator(client, cancellationToken)).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#4
0
        public static EncompassRestClient CreateFromAccessToken(ClientParameters parameters, string accessToken)
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(accessToken, nameof(accessToken));

            var client = new EncompassRestClient(parameters);

            client.AccessToken.Token = accessToken;
            return(client);
        }
        public static EncompassRestClient CreateFromAccessToken(string apiClientId, string apiClientSecret, string accessToken)
        {
            Preconditions.NotNullOrEmpty(apiClientId, nameof(apiClientId));
            Preconditions.NotNullOrEmpty(apiClientSecret, nameof(apiClientSecret));
            Preconditions.NotNullOrEmpty(accessToken, nameof(accessToken));

            var client = new EncompassRestClient(apiClientId, apiClientSecret);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#6
0
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
        public static EncompassRestClient CreateFromAccessToken(ClientParameters parameters, string accessToken)
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(accessToken, nameof(accessToken));

            var client = new EncompassRestClient(parameters);

            client.AccessToken.Token = accessToken;
            return(client);
        }
        public static async Task <EncompassRestClient> CreateFromAuthorizationCodeAsync(string apiClientId, string apiClientSecret, string redirectUri, string authorizationCode, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNullOrEmpty(apiClientId, nameof(apiClientId));
            Preconditions.NotNullOrEmpty(apiClientSecret, nameof(apiClientSecret));
            Preconditions.NotNullOrEmpty(redirectUri, nameof(redirectUri));
            Preconditions.NotNullOrEmpty(authorizationCode, nameof(authorizationCode));

            var client = new EncompassRestClient(apiClientId, apiClientSecret);
            await client.AccessToken.SetTokenWithAuthorizationCodeAsync(redirectUri, authorizationCode, cancellationToken).ConfigureAwait(false);

            return(client);
        }
示例#8
0
        public static async Task <EncompassRestClient> CreateFromAuthorizationCodeAsync(ClientParameters parameters, string redirectUri, string authorizationCode, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(redirectUri, nameof(redirectUri));
            Preconditions.NotNullOrEmpty(authorizationCode, nameof(authorizationCode));

            var client      = new EncompassRestClient(parameters);
            var accessToken = await client.AccessToken.GetTokenFromAuthorizationCodeAsync(redirectUri, authorizationCode, nameof(CreateFromAuthorizationCodeAsync), cancellationToken).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#9
0
        /// <summary>
        /// Creates a client object from an existing access token.
        /// </summary>
        /// <param name="parameters">The parameters to initialize the client object with.</param>
        /// <param name="accessToken">The access token to use.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns></returns>
        public static async Task <EncompassRestClient> CreateFromAccessTokenAsync(ClientParameters parameters, string accessToken, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(accessToken, nameof(accessToken));

            var client = new EncompassRestClient(parameters);

            client.AccessToken.Token = accessToken;
            await client.CommonCache.TryInitializeAsync(client, parameters, cancellationToken).ConfigureAwait(false);

            return(client);
        }
        public static async Task <EncompassRestClient> CreateAsync(string apiClientId, string apiClientSecret, string instanceId, Func <TokenCreator, CancellationToken, Task <string> > tokenInitializer, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNullOrEmpty(apiClientId, nameof(apiClientId));
            Preconditions.NotNullOrEmpty(apiClientSecret, nameof(apiClientSecret));
            Preconditions.NotNullOrEmpty(instanceId, nameof(instanceId));
            Preconditions.NotNull(tokenInitializer, nameof(tokenInitializer));

            var client      = new EncompassRestClient(apiClientId, apiClientSecret, instanceId, tokenInitializer);
            var accessToken = await tokenInitializer(new TokenCreator(client), cancellationToken).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#11
0
        public static async Task <EncompassRestClient> CreateFromUserCredentialsAsync(ClientParameters parameters, string instanceId, string userId, string password, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(instanceId, nameof(instanceId));
            Preconditions.NotNullOrEmpty(userId, nameof(userId));
            Preconditions.NotNullOrEmpty(password, nameof(password));

            var client      = new EncompassRestClient(parameters);
            var accessToken = await client.AccessToken.GetTokenFromUserCredentialsAsync(instanceId, userId, password, nameof(CreateFromUserCredentialsAsync), cancellationToken).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#12
0
        public static async Task <EncompassRestClient> CreateFromClientCredentialsAsync(ClientParameters parameters, string instanceId, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(parameters, nameof(parameters));
            Preconditions.NotNullOrEmpty(instanceId, nameof(instanceId));

            var client      = new EncompassRestClient(parameters);
            var accessToken = await client.AccessToken.GetTokenFromClientCredentialsAsync(instanceId, nameof(CreateFromClientCredentialsAsync), cancellationToken).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            await parameters.TryInitializeAsync(client, client.CommonCache, cancellationToken).ConfigureAwait(false);

            return(client);
        }
        public static async Task <EncompassRestClient> CreateFromUserCredentialsAsync(string apiClientId, string apiClientSecret, string instanceId, string userId, string password, TokenExpirationHandling tokenExpirationHandling, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNullOrEmpty(userId, nameof(userId));
            Preconditions.NotNullOrEmpty(password, nameof(password));

            if (tokenExpirationHandling == TokenExpirationHandling.RetrieveNewToken)
            {
                return(await CreateAsync(apiClientId, apiClientSecret, instanceId, (tokenCreator, ct) => tokenCreator.FromUserCredentialsAsync(userId, password, ct), cancellationToken).ConfigureAwait(false));
            }

            Preconditions.NotNullOrEmpty(apiClientId, nameof(apiClientId));
            Preconditions.NotNullOrEmpty(apiClientSecret, nameof(apiClientSecret));
            Preconditions.NotNullOrEmpty(instanceId, nameof(instanceId));

            var client      = new EncompassRestClient(apiClientId, apiClientSecret, instanceId, null);
            var accessToken = await client.AccessToken.GetTokenFromUserCredentialsAsync(userId, password, nameof(CreateFromUserCredentialsAsync), cancellationToken).ConfigureAwait(false);

            client.AccessToken.Token = accessToken;
            return(client);
        }
示例#14
0
        /// <summary>
        /// Refreshes the custom fields cache.
        /// </summary>
        /// <param name="client">The client to use to retrieve the custom fields.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns></returns>
        public async Task RefreshCustomFieldsAsync(EncompassRestClient client, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(client, nameof(client));

            var retrievedCustomFields = new Dictionary <string, FieldDescriptor>(StringComparer.OrdinalIgnoreCase);

            var customFieldsList = await client.Settings.Loan.CustomFields.GetCustomFieldsAsync(cancellationToken).ConfigureAwait(false);

            foreach (var customField in customFieldsList)
            {
                var modelPath = $"Loan.CustomFields[(FieldName == '{customField.Id}')].StringValue";
                retrievedCustomFields[customField.Id] = new NonStandardFieldDescriptor(customField.Id, LoanFieldDescriptors.CreateModelPath(modelPath), modelPath, customField.Description, customField.Format, customField.Options?.Select(o => new FieldOption(o)).ToList(), false);
            }

            var customFields = _customFields;

            foreach (var pair in customFields)
            {
                if (retrievedCustomFields.TryGetValue(pair.Key, out var descriptor))
                {
                    customFields[pair.Key] = descriptor;
                    retrievedCustomFields.Remove(pair.Key);
                }
                else
                {
                    customFields.TryRemove(pair.Key, out _);
                }
            }

            foreach (var pair in retrievedCustomFields)
            {
                customFields[pair.Key] = pair.Value;
            }

            CustomFieldsLastRefreshedUtc = DateTime.UtcNow;
        }
示例#15
0
 internal TokenCreator(EncompassRestClient client)
 {
     _client = client;
 }
示例#16
0
 internal BaseApiClient(EncompassRestClient client)
     : base(client, null)
 {
 }
示例#17
0
 internal ApiObject(EncompassRestClient client, string?baseApiPath)
 {
     Client       = client;
     _baseApiPath = baseApiPath;
 }
示例#18
0
 internal TokenCreator(EncompassRestClient client, CancellationToken requestCancellationToken)
 {
     _client = client;
     RequestCancellationToken = requestCancellationToken;
 }
示例#19
0
        public static async Task GenerateClassFilesFromSchemaAsync(EncompassRestClient client, string destinationPath, string @namespace)
        {
            Directory.CreateDirectory(destinationPath);
            var supportedEntities = new HashSet <string>((await client.Loans.GetSupportedEntitiesAsync().ConfigureAwait(false)).Select(e => e.Value))
            {
                "NonVol"
            };
            var exceptions = new List <Exception>();

            foreach (var entity in supportedEntities)
            {
                Exception exception;
                var       tryCount = 0;
                do
                {
                    exception = null;
                    try
                    {
                        var loanSchema = await client.Schema.GetLoanSchemaAsync(true, new[] { entity }).ConfigureAwait(false);

                        if (loanSchema.EntityTypes.TryGetValue(entity, out var entitySchema))
                        {
                            await GenerateClassFileFromSchemaAsync(destinationPath, @namespace, entity, entitySchema).ConfigureAwait(false);

                            if (s_missingSchemaEntities.Contains(entity))
                            {
                                Console.WriteLine($"Schema for {entity} can now be retrieved");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"Failed to retrieve entity of type {entity}");
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!s_missingSchemaEntities.Contains(entity))
                        {
                            exception = new Exception(entity, ex);
                        }
                    }
                    ++tryCount;
                } while (exception != null && tryCount < 3);
                if (exception != null)
                {
                    exceptions.Add(exception);
                }
            }
            foreach (var enumPair in s_sharedEnums.Concat(s_otherEnums))
            {
                foreach (var innerEnumPair in s_otherEnums)
                {
                    if (enumPair.Key != innerEnumPair.Key && innerEnumPair.Value.IsSubsetOf(enumPair.Value))
                    {
                        if (innerEnumPair.Value.SetEquals(enumPair.Value))
                        {
                            Console.WriteLine($"{enumPair.Key} and {innerEnumPair.Key} are equal");
                        }
                        else
                        {
                            var diff = enumPair.Value.Except(innerEnumPair.Value).ToList();
                            if (diff.Count <= 2)
                            {
                                Console.WriteLine($"{enumPair.Key} contains all members of {innerEnumPair.Key} but adds {string.Join(", ", diff)}");
                            }
                        }
                    }
                }
            }
            var enumsPath = $"{destinationPath}\\Enums";

            Directory.CreateDirectory(enumsPath);
            foreach (var pair in s_sharedEnums.Concat(s_otherEnums))
            {
                await GenerateEnumFileFromOptions(enumsPath, $"{@namespace}.Enums", pair.Key, pair.Value).ConfigureAwait(false);
            }
            if (exceptions.Count > 0)
            {
                throw new AggregateException(exceptions);
            }
        }