< Summary

Information
Class: ValidateLib.Metadata.MetdataLocation.SiteWideLocator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\MetadataLocation\SiteWideLocator.cs
Line coverage
52%
Covered lines: 18
Uncovered lines: 16
Coverable lines: 34
Total lines: 77
Line coverage: 52.9%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\MetadataLocation\SiteWideLocator.cs

#LineLine coverage
 1namespace ValidateLib.Metadata.MetdataLocation
 2{
 3    internal class SiteWideLocator
 4    {
 15        static string DEFAULT_LOCATION_SUFFIX = "-metadata.json";
 16        static string DEFAULT_NAME = "csv-metadata.json";
 17        static string WELL_KNOWN_ADDRESS = "/.well-known/csvm";
 8
 9        public static List<string> GetMetadataLocationFromSiteWideConfigAsync(string normalizedTabularIRI, string baseIR
 10        {
 111            List<string> possibleLocations = new List<string>();
 12
 113            using (HttpClient httpClient = new HttpClient())
 14            {
 15                try
 16                {
 117                    GetMetadataLocationFromSiteWideConfigUnsafe(normalizedTabularIRI, possibleLocations, httpClient);
 118                    if (possibleLocations.Count == 0)
 19                    {
 120                        possibleLocations.Add(normalizedTabularIRI + DEFAULT_LOCATION_SUFFIX);
 121                        possibleLocations.Add(baseIRI + DEFAULT_NAME);
 22                    }
 123                }
 024                catch (Exception)
 25                {
 026                    possibleLocations.Add(normalizedTabularIRI + DEFAULT_LOCATION_SUFFIX);
 027                    possibleLocations.Add(baseIRI + DEFAULT_NAME);
 028                }
 29            }
 30
 131            return possibleLocations;
 32        }
 33
 34        static void GetMetadataLocationFromSiteWideConfigUnsafe(string normalizedTabularIRI, List<string> possibleLocati
 35        {
 136            Uri uri = new Uri(normalizedTabularIRI);
 137            string wellKnownAdrress = $"{uri.Scheme}://{uri.Host}{WELL_KNOWN_ADDRESS}";
 38
 39            // Download the file and read it line by line
 140            using (HttpResponseMessage response = httpClient.GetAsync(wellKnownAdrress).Result)
 141            using (HttpContent content = response.Content)
 42            {
 143                if (response.IsSuccessStatusCode)
 44                {
 045                    string data = content.ReadAsStringAsync().Result;
 46
 47                    // Split the data into lines
 048                    string[] lines = data.Split('\n');
 49
 050                    foreach (string line in lines)
 51                    {
 052                        string? resolvedLine = ProcessLine(line, normalizedTabularIRI);
 053                        if (resolvedLine is not null)
 054                            possibleLocations.Add(resolvedLine);
 55
 56                    }
 57                }
 158            }
 159        }
 60        static string? ProcessLine(string line, string tabularDataUrl)
 61        {
 62            // this need to be try catch because the file can contain random things and does not have to be valid
 63            try
 64            {
 065                Tavis.UriTemplates.UriTemplate template = new Tavis.UriTemplates.UriTemplate(line);
 066                template.SetParameter("url", tabularDataUrl);
 067                return template.Resolve();
 68            }
 069            catch (Exception)
 70            {
 071                return null;
 72            }
 73
 074        }
 75
 76    }
 77}