|
| 1 | +using Hmcr.Chris.Models; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Hmcr.Chris |
| 9 | +{ |
| 10 | + public interface IInventoryApi |
| 11 | + { |
| 12 | + Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates); |
| 13 | + Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates); |
| 14 | + |
| 15 | + //TODO: implement MC calls to CHRIS |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + public class InventoryApi : IInventoryApi |
| 20 | + { |
| 21 | + private HttpClient _client; |
| 22 | + private InventoryQueries _queries; |
| 23 | + private IApi _api; |
| 24 | + private string _path; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Chris Query typeName values, used to call the Get Inventory |
| 28 | + /// Assoc with Work Activity Query. |
| 29 | + /// </summary> |
| 30 | + public static class InventoryQueryTypeName |
| 31 | + { |
| 32 | + public const string SURF_ASSOC_WITH_LINE = "SURF_ASSOCIATED_WITH_LINE"; |
| 33 | + public const string SURF_ASSOC_WITH_POINT = "SURF_ASSOCIATED_WITH_POINT"; |
| 34 | + public const string MC_ASSOC_WITH_LINE = "MC_ASSOCIATED_WITH_LINE"; |
| 35 | + public const string MC_ASSOC_WITH_POINT = "MC_ASSOCIATED_WITH_POINT"; |
| 36 | + } |
| 37 | + |
| 38 | + public InventoryApi(HttpClient client, IApi api, IConfiguration config) |
| 39 | + { |
| 40 | + _client = client; |
| 41 | + _queries = new InventoryQueries(); |
| 42 | + _api = api; |
| 43 | + _path = config.GetValue<string>("CHRIS:OASPath"); |
| 44 | + } |
| 45 | + |
| 46 | + public async Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates) |
| 47 | + { |
| 48 | + var body = string.Format(_queries.SurfaceTypeAssocWithPointQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_POINT); |
| 49 | + |
| 50 | + var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync(); |
| 51 | + |
| 52 | + var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents); |
| 53 | + |
| 54 | + SurfaceType surfaceType = new SurfaceType(); |
| 55 | + if (results.features.Length > 0) |
| 56 | + { |
| 57 | + surfaceType.Type = results.features[0].properties.SURFACE_TYPE; |
| 58 | + } |
| 59 | + |
| 60 | + return surfaceType; |
| 61 | + } |
| 62 | + |
| 63 | + public async Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates) |
| 64 | + { |
| 65 | + var body = string.Format(_queries.SurfaceTypeAssocWithLineQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_LINE); |
| 66 | + |
| 67 | + var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync(); |
| 68 | + |
| 69 | + var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents); |
| 70 | + |
| 71 | + var surfaceTypes = new List<SurfaceType>(); |
| 72 | + |
| 73 | + foreach (var feature in results.features) |
| 74 | + { |
| 75 | + SurfaceType surfaceType = new SurfaceType(); |
| 76 | + surfaceType.Length = feature.properties.CLIPPED_LENGTH_KM; |
| 77 | + surfaceType.Type = feature.properties.SURFACE_TYPE; |
| 78 | + |
| 79 | + surfaceTypes.Add(surfaceType); |
| 80 | + } |
| 81 | + |
| 82 | + return surfaceTypes; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments