示例#1
0
        public static void Main(string[] args)
        {
            if (5 != args.Length)
            {
                Console.WriteLine($"Usage: {System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name} <apidomain> <httpbasicauthstring> <servicetype> <realm> <advancedsearchdescriptionfilename>");
            }
            else
            {
                string apiDomain           = args[0];
                string httpBasicAuthString = args[1];
                string serviceType         = args[2];
                string realm = args[3];
                string advancedSearchDescriptionFileName = args[4];

                if (File.Exists(advancedSearchDescriptionFileName))
                {
                    Uri upstreamServerUrl = new Uri($"https://{apiDomain}");

                    using (CtmsRegistryClient registryClient = new CtmsRegistryClient(new OAuth2AuthorizationConnection(upstreamServerUrl, httpBasicAuthString)))
                    {
                        const string registeredLinkRelSearches = "search:searches";
                        Searches     searchesResource          = PlatformTools.PlatformToolsSDK.FindInRegistry <Searches>(registryClient, serviceType, realm, registeredLinkRelSearches);

                        if (null != searchesResource)
                        {
                            const string registeredLinkRelAdvancedSearch = "search:advanced-search";
                            Link         advancedSearchLink = searchesResource.DiscoverLink(registeredLinkRelAdvancedSearch);
                            /// Check, whether simple search is supported:
                            if (null != advancedSearchLink)
                            {
                                UriTemplate advancedSearchUriTemplate = new UriTemplate(advancedSearchLink.Href);
                                advancedSearchUriTemplate.SetParameter("offset", 0);
                                advancedSearchUriTemplate.SetParameter("limit", 50);

                                string advancedSearchUri = advancedSearchUriTemplate.Resolve();

                                string advancedSearchDescription = File.ReadAllText(advancedSearchDescriptionFileName);

                                /// Doing the search and write the results to stdout:
                                // The search description must be passed as content type "application/json":
                                using (HttpContent content = new StringContent(advancedSearchDescription, Encoding.UTF8, "application/json"))
                                {
                                    using (HttpResponseMessage response = registryClient.HttpClient.PostAsync(advancedSearchUri, content).Result)
                                    {
                                        AdvancedSearch searchResult = response.Content.ReadAsAsyncHal <AdvancedSearch>().Result;

                                        int assetNo = 0;
                                        int pageNo  = 0;
                                        // Page through the result:
                                        StringBuilder sb = new StringBuilder();
                                        sb.AppendLine(DateTime.Now.ToString());
                                        do
                                        {
                                            if (searchResult.AssetList.Any())
                                            {
                                                sb.AppendLine($"Page#: {++pageNo}, search description from file '{advancedSearchDescriptionFileName}'");
                                                foreach (Asset asset in searchResult.AssetList)
                                                {
                                                    BaseInfo         baseInfo         = asset.Base;
                                                    CommonAttributes commonAttributes = asset.Common;

                                                    sb.AppendLine($"Asset#: {++assetNo}, id: {asset.Base.Id}, name: '{asset.Common.Name}'");
                                                }
                                            }

                                            // If we have more results, follow the next link and get the next page:
                                            searchResult = registryClient.GetHalResource <SimpleSearch>(searchResult.GetUri("next", Enumerable.Empty <EmbedResource>()));
                                        }while (searchResult != null);
                                        Console.WriteLine(sb);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Advanced search not supported.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Advanced search not supported.");
                        }

                        Console.WriteLine("End");
                    }
                }
            }
        }
示例#2
0
        public static void Main(string[] args)
        {
            if (5 != args.Length || "'".Equals(args[4]) || !args[4].StartsWith("'") || !args[4].EndsWith("'"))
            {
                Console.WriteLine($"Usage: {System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name} <apidomain> <httpBasicAuthString> <servicetype> <realm> '<simplesearchexpression>'");
            }
            else
            {
                string apiDomain           = args[0];
                string httpBasicAuthString = args[1];
                string serviceType         = args[2];
                string realm = args[3];
                string rawSearchExpression = args[4].Trim('\'');

                Uri upstreamServerUrl = new Uri($"https://{apiDomain}");

                using (CtmsRegistryClient registryClient = new CtmsRegistryClient(new OAuth2AuthorizationConnection(upstreamServerUrl, httpBasicAuthString)))
                {
                    const string registeredLinkRelSearches = "search:searches";
                    Searches     searchesResource          = PlatformTools.PlatformToolsSDK.FindInRegistry <Searches>(registryClient, serviceType, realm, registeredLinkRelSearches);

                    if (null != searchesResource)
                    {
                        const string registeredLinkRelSimpleSearch = "search:simple-search";
                        Link         simpleSearchLink = searchesResource.DiscoverLink(registeredLinkRelSimpleSearch);
                        /// Check, whether simple search is supported:
                        if (null != simpleSearchLink)
                        {
                            UriTemplate simpleSearchUrlTemplate = new UriTemplate(simpleSearchLink.Href);
                            simpleSearchUrlTemplate.SetParameter("search", rawSearchExpression);
                            Uri simpleSearchResultPageUrl = new Uri(simpleSearchUrlTemplate.Resolve());

                            /// Doing the search and write the results to stdout:
                            SimpleSearch searchResult = registryClient.GetHalResource <SimpleSearch>(simpleSearchResultPageUrl);

                            int assetNo = 0;
                            int pageNo  = 0;
                            // Page through the result:
                            StringBuilder sb = new StringBuilder();
                            do
                            {
                                if (searchResult.AssetList.Any())
                                {
                                    sb.AppendLine($"Page#: {++pageNo}, search expression: '{rawSearchExpression}'");
                                    foreach (Asset asset in searchResult.AssetList)
                                    {
                                        BaseInfo         baseInfo         = asset.Base;
                                        CommonAttributes commonAttributes = asset.Common;

                                        sb.AppendLine($"Asset#: {++assetNo}, id: {asset.Base.Id}, name: '{asset.Common.Name}'");
                                    }
                                }

                                // If we have more results, follow the next link and get the next page:
                                searchResult = registryClient.GetHalResource <SimpleSearch>(searchResult.GetUri("next", Enumerable.Empty <EmbedResource>()));
                            }while (searchResult != null);
                            Console.WriteLine(sb);
                        }
                    }
                }

                Console.WriteLine("End");
            }
        }