コード例 #1
0
        /// <summary>
        /// Discovers schemas located in the Salesforce 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();

            // handle query based schema
            try
            {
                if (request.Mode == DiscoverSchemasRequest.Types.Mode.Refresh && request.ToRefresh.Count == 1 &&
                    !string.IsNullOrWhiteSpace(request.ToRefresh.First().Query))
                {
                    discoverSchemasResponse.Schemas.Add(
                        await Discover.GetSchemaForQuery(_client, request.ToRefresh.First(), request.SampleSize));
                    return(discoverSchemasResponse);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new DiscoverSchemasResponse());
            }

            List <TabObject> tabsResponse;

            // get the tabs present in Salesforce
            try
            {
                Logger.Debug("Getting tabs...");
                var response = await _client.GetAsync("/tabs");

                response.EnsureSuccessStatusCode();

                tabsResponse =
                    JsonConvert.DeserializeObject <List <TabObject> >(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new DiscoverSchemasResponse());
            }

            // attempt to get a schema for each tab found
            try
            {
                Logger.Info($"Schemas attempted: {tabsResponse.Count}");

                var tasks = tabsResponse
                            .Select(t => Discover.GetSchemaForTab(_client, _fieldObjectsDictionary, t))
                            .ToArray();

                await Task.WhenAll(tasks);

                discoverSchemasResponse.Schemas.AddRange(tasks.Where(x => x.Result != null).Select(x => x.Result));
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
                return(new DiscoverSchemasResponse());
            }

            Logger.Info($"Schemas found: {discoverSchemasResponse.Schemas.Count}");

            // only return requested schemas if refresh mode selected
            if (request.Mode == DiscoverSchemasRequest.Types.Mode.Refresh)
            {
                var refreshSchemas = request.ToRefresh;
                var schemas        =
                    JsonConvert.DeserializeObject <Schema[]>(
                        JsonConvert.SerializeObject(discoverSchemasResponse.Schemas));
                discoverSchemasResponse.Schemas.Clear();
                discoverSchemasResponse.Schemas.AddRange(schemas.Join(refreshSchemas, schema => schema.Id,
                                                                      refreshSchema => refreshSchema.Id,
                                                                      (schema, refresh) => schema));

                Logger.Debug($"Schemas found: {JsonConvert.SerializeObject(schemas)}");
                Logger.Debug($"Refresh requested on schemas: {JsonConvert.SerializeObject(refreshSchemas)}");

                Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}");
                return(discoverSchemasResponse);
            }

            // return all schemas otherwise
            Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}");
            return(discoverSchemasResponse);
        }