예제 #1
0
        public async Task <string> GetDatasetIdAsync()
        {
            var response = await _httpclient.GetAsync($"{_baseUrl}/datasetId");

            DatasetResponse dto = await response.Content.ReadAsAsync <DatasetResponse>();

            return(dto.datasetId);
        }
        // TODO: De fra data.norge.no har flere format på en distribution?? Skal vi støtte det? :o
        // RART: Finner ikke kategori kobling i rdfene
        // Import dataset from link containing rdf schema.
        public async Task <DatasetResponse> import(String url, int categoryId)
        {
            Graph g;

            try
            {
                // Guess the url is actually an url.
                if (url.Contains("/"))
                {
                    // If it is in fellesdatakatalog API we need to request with headers to get on rdf format
                    if (url.Contains("fellesdatakatalog.digdir.no"))
                    {
                        g = NetworkHandling.LoadFromUriWithHeadersTurtle(url);
                    }
                    // If the url is directly to the page on data.norge.no fetch with the id
                    else if (url.Contains("data.norge.no"))
                    {
                        g = NetworkHandling.LoadFromUriWithHeadersTurtle("https://fellesdatakatalog.digdir.no/api/datasets/" + url.Substring(url.LastIndexOf("/") + 1));
                    }
                    // Otherwise hope the url is directly to a rdf file location on XML format
                    else
                    {
                        g = NetworkHandling.LoadFromUriXml(url);
                    }
                }
                // Guess it is not an url, and instead ID to some dataset in data.norge.no
                else
                {
                    g = NetworkHandling.LoadFromUriWithHeadersTurtle("https://fellesdatakatalog.digdir.no/api/datasets/" + url);
                }
            }
            catch (Exception ex)
            {
                return(new DatasetResponse($"Invalid import url. {ex.Message}"));
            }

            // Try to parse the dataset and save it in the database
            DatasetResponse dataset = await _graphService.AddDataset(g, categoryId);

            return(dataset);
        }
        // Populate the database with datasets from fellesdatakatalog
        public async Task <DatasetResponse> populate(int numberOfDatasets)
        {
            List <string>   urls    = findUrlsFromFellesKatalogen(numberOfDatasets);
            DatasetResponse dataset = new DatasetResponse("");

            List <Category> categories = (List <Category>) await _categoryRepository.FlatListAsync();

            // Parse the content in the urls and add them to the database
            foreach (string url in urls)
            {
                // A small part of the datasets in the fellesdatakatalog does not have a rdf version
                try
                {
                    Random rnd = new Random();
                    int    randomCategoryId = categories.ElementAt(rnd.Next(categories.Count)).Id;
                    dataset = await import(url, randomCategoryId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return(dataset);
        }