| | 1 | | namespace ValidateLib.Metadata.MetdataLocation |
| | 2 | | { |
| | 3 | | internal class SiteWideLocator |
| | 4 | | { |
| 1 | 5 | | static string DEFAULT_LOCATION_SUFFIX = "-metadata.json"; |
| 1 | 6 | | static string DEFAULT_NAME = "csv-metadata.json"; |
| 1 | 7 | | static string WELL_KNOWN_ADDRESS = "/.well-known/csvm"; |
| | 8 | |
|
| | 9 | | public static List<string> GetMetadataLocationFromSiteWideConfigAsync(string normalizedTabularIRI, string baseIR |
| | 10 | | { |
| 1 | 11 | | List<string> possibleLocations = new List<string>(); |
| | 12 | |
|
| 1 | 13 | | using (HttpClient httpClient = new HttpClient()) |
| | 14 | | { |
| | 15 | | try |
| | 16 | | { |
| 1 | 17 | | GetMetadataLocationFromSiteWideConfigUnsafe(normalizedTabularIRI, possibleLocations, httpClient); |
| 1 | 18 | | if (possibleLocations.Count == 0) |
| | 19 | | { |
| 1 | 20 | | possibleLocations.Add(normalizedTabularIRI + DEFAULT_LOCATION_SUFFIX); |
| 1 | 21 | | possibleLocations.Add(baseIRI + DEFAULT_NAME); |
| | 22 | | } |
| 1 | 23 | | } |
| 0 | 24 | | catch (Exception) |
| | 25 | | { |
| 0 | 26 | | possibleLocations.Add(normalizedTabularIRI + DEFAULT_LOCATION_SUFFIX); |
| 0 | 27 | | possibleLocations.Add(baseIRI + DEFAULT_NAME); |
| 0 | 28 | | } |
| | 29 | | } |
| | 30 | |
|
| 1 | 31 | | return possibleLocations; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | static void GetMetadataLocationFromSiteWideConfigUnsafe(string normalizedTabularIRI, List<string> possibleLocati |
| | 35 | | { |
| 1 | 36 | | Uri uri = new Uri(normalizedTabularIRI); |
| 1 | 37 | | string wellKnownAdrress = $"{uri.Scheme}://{uri.Host}{WELL_KNOWN_ADDRESS}"; |
| | 38 | |
|
| | 39 | | // Download the file and read it line by line |
| 1 | 40 | | using (HttpResponseMessage response = httpClient.GetAsync(wellKnownAdrress).Result) |
| 1 | 41 | | using (HttpContent content = response.Content) |
| | 42 | | { |
| 1 | 43 | | if (response.IsSuccessStatusCode) |
| | 44 | | { |
| 0 | 45 | | string data = content.ReadAsStringAsync().Result; |
| | 46 | |
|
| | 47 | | // Split the data into lines |
| 0 | 48 | | string[] lines = data.Split('\n'); |
| | 49 | |
|
| 0 | 50 | | foreach (string line in lines) |
| | 51 | | { |
| 0 | 52 | | string? resolvedLine = ProcessLine(line, normalizedTabularIRI); |
| 0 | 53 | | if (resolvedLine is not null) |
| 0 | 54 | | possibleLocations.Add(resolvedLine); |
| | 55 | |
|
| | 56 | | } |
| | 57 | | } |
| 1 | 58 | | } |
| 1 | 59 | | } |
| | 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 | | { |
| 0 | 65 | | Tavis.UriTemplates.UriTemplate template = new Tavis.UriTemplates.UriTemplate(line); |
| 0 | 66 | | template.SetParameter("url", tabularDataUrl); |
| 0 | 67 | | return template.Resolve(); |
| | 68 | | } |
| 0 | 69 | | catch (Exception) |
| | 70 | | { |
| 0 | 71 | | return null; |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | } |
| | 77 | | } |