コード例 #1
0
        /// <summary>
        /// Discovers schemas located in the users Sage instance
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns>Discovered schemas</returns>
        public override async Task <DiscoverSchemasResponse> DiscoverSchemas(DiscoverSchemasRequest request,
                                                                             ServerCallContext context)
        {
            Logger.SetLogPrefix("discover");
            Logger.Info("Discovering Schemas...");

            DiscoverSchemasResponse discoverSchemasResponse = new DiscoverSchemasResponse();

            // only return requested schemas if refresh mode selected
            if (request.Mode == DiscoverSchemasRequest.Types.Mode.Refresh)
            {
                try
                {
                    var refreshSchemas = request.ToRefresh;

                    Logger.Info($"Refresh schemas attempted: {refreshSchemas.Count}");

                    var tasks = refreshSchemas.Select((s) =>
                    {
                        var metaJsonObject = JsonConvert.DeserializeObject <PublisherMetaJson>(s.PublisherMetaJson);

                        // add insert schema
                        if (metaJsonObject.Module.Contains("Insert"))
                        {
                            return(Task.FromResult(Discover.GetInsertSchemaForModule(metaJsonObject.Module)));
                        }
                        // add discover schema
                        return(GetSchemaForModule(metaJsonObject.Module));
                    })
                                .ToArray();

                    await Task.WhenAll(tasks);

                    discoverSchemasResponse.Schemas.AddRange(tasks.Where(x => x.Result != null).Select(x => x.Result));

                    // return all schemas
                    Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}");
                    return(discoverSchemasResponse);
                }
                catch (Exception e)
                {
                    Logger.Error(e, e.Message, context);
                    return(new DiscoverSchemasResponse());
                }
            }

            // attempt to get a schema for each module requested
            try
            {
                Logger.Info($"Schemas attempted: {_server.Settings.ModulesList.Length + Discover.GetTotalInsertSchemas()}");

                var tasks = _server.Settings.ModulesList.Select(GetSchemaForModule)
                            .ToArray();

                await Task.WhenAll(tasks);

                discoverSchemasResponse.Schemas.AddRange(tasks.Where(x => x.Result != null).Select(x => x.Result));

                // add insert schemas
                discoverSchemasResponse.Schemas.AddRange(Discover.GetAllInsertSchemas());

                // return all schema
                Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}");
                return(discoverSchemasResponse);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new DiscoverSchemasResponse());
            }
        }